001: package velosurf.util;
002:
003: import java.util.LinkedList;
004:
005: import org.apache.commons.net.smtp.SimpleSMTPHeader;
006: import org.apache.commons.net.smtp.SMTPClient;
007: import org.apache.commons.net.smtp.SMTPReply;
008:
009: public class MailNotifier implements Runnable {
010:
011: private String host;
012: private String sender;
013: private String recipient;
014: private LinkedList<Notification> queue = new LinkedList<Notification>();
015: private boolean running = false;
016:
017: class Notification {
018: String subject;
019: String body;
020:
021: Notification(String subject, String body) {
022: this .subject = subject;
023: this .body = body;
024: }
025: }
026:
027: public MailNotifier(String host, String sender, String recipient) {
028: this .host = host;
029: this .sender = sender;
030: this .recipient = recipient;
031: }
032:
033: public void start() {
034: new Thread(this , "email notifications").start();
035: }
036:
037: public void stop() {
038: running = false;
039: synchronized (this ) {
040: notify();
041: }
042: }
043:
044: public void sendNotification(String subject, String body) {
045: synchronized (this ) {
046: queue.add(new Notification(subject, body));
047: notify();
048: }
049: }
050:
051: public void run() {
052: Notification notif;
053: SMTPClient client = null;
054: try {
055: running = true;
056: while (running) {
057: synchronized (this ) {
058: if (queue.size() == 0) {
059: wait();
060: }
061: notif = queue.removeFirst();
062: }
063: if (notif == null) {
064: continue;
065: }
066: String header = new SimpleSMTPHeader(sender, recipient,
067: notif.subject).toString();
068: client = new SMTPClient();
069: client.connect(host);
070: if (!SMTPReply.isPositiveCompletion(client
071: .getReplyCode())) {
072: throw new Exception("SMTP server " + host
073: + " refused connection.");
074: }
075: if (!client.login()) {
076: throw new Exception(
077: "SMTP: Problem logging in: error #"
078: + client.getReplyCode() + " "
079: + client.getReplyString());
080: }
081: if (!client.setSender(sender)) {
082: throw new Exception(
083: "SMTP: Problem setting sender to " + sender
084: + ": error #"
085: + client.getReplyCode() + " "
086: + client.getReplyString());
087: }
088: if (!client.addRecipient(recipient)) {
089: throw new Exception(
090: "SMTP: Problem adding recipient "
091: + recipient + ": error #"
092: + client.getReplyCode() + " "
093: + client.getReplyString());
094: }
095: if (!client.sendShortMessageData(header + notif.body)) {
096: throw new Exception(
097: "Problem sending notification : error #"
098: + client.getReplyCode() + " "
099: + client.getReplyString());
100: }
101: try {
102: client.logout();
103: client.disconnect();
104: } catch (Exception e) {
105: }
106: }
107: } catch (Exception e) {
108: try {
109: if (client != null) {
110: client.logout();
111: client.disconnect();
112: }
113: } catch (Exception f) {
114: }
115: Logger.enableNotifications(false);
116: Logger.log(e);
117: }
118: }
119: }
|