KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sslexplorer > notification > smtp > SMTPMessageSink


1 /*
2  * SSL-Explorer
3  *
4  * Copyright (C) 2003-2006 3SP LTD. All Rights Reserved
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2 of
9  * the License, or (at your option) any later version.
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */

19             
20 package com.sslexplorer.notification.smtp;
21
22 import java.io.IOException JavaDoc;
23 import java.io.Writer JavaDoc;
24 import java.util.Iterator JavaDoc;
25 import java.util.List JavaDoc;
26
27 import org.apache.commons.logging.Log;
28 import org.apache.commons.logging.LogFactory;
29 import org.apache.commons.net.smtp.SMTPClient;
30 import org.apache.commons.net.smtp.SMTPReply;
31 import org.apache.commons.net.smtp.SimpleSMTPHeader;
32
33 import com.sslexplorer.core.UserDatabaseManager;
34 import com.sslexplorer.notification.Message;
35 import com.sslexplorer.notification.MessageSink;
36 import com.sslexplorer.notification.Notifier;
37 import com.sslexplorer.notification.Recipient;
38 import com.sslexplorer.properties.Property;
39 import com.sslexplorer.properties.impl.systemconfig.SystemConfigKey;
40 import com.sslexplorer.security.User;
41 import com.sslexplorer.security.UserDatabase;
42
43 public class SMTPMessageSink implements MessageSink {
44     final static Log log = LogFactory.getLog(SMTPMessageSink.class);
45     private SMTPClient client;
46     private Notifier notifier;
47
48     public SMTPMessageSink() {
49         super();
50         client = new SMTPClient();
51     }
52
53     public void start(Notifier notifier) throws Exception JavaDoc {
54         this.notifier = notifier;
55     }
56
57     public void stop() throws Exception JavaDoc {
58     }
59
60     public boolean send(Message message) throws Exception JavaDoc {
61         try {
62             //
63
List JavaDoc l = notifier.getFullRecipientListAsUsers(message.getRecipients());
64             int reply;
65
66             if (log.isDebugEnabled()) {
67                 log.debug("Setting timeout");
68                 log.debug("Connecting to SMTP server");
69             }
70             client.connect(Property.getProperty(new SystemConfigKey("smtp.hostname")),
71                Property.getPropertyInt(new SystemConfigKey("smtp.port")));
72             if (log.isDebugEnabled())
73                 log.debug("Getting reply");
74             reply = client.getReplyCode();
75             if (!SMTPReply.isPositiveCompletion(reply)) {
76                 client.disconnect();
77                 throw new Exception JavaDoc("SMTP server refused connection.");
78             }
79             if (log.isDebugEnabled())
80                 log.debug("Logging in");
81             String JavaDoc helo = Property.getProperty(new SystemConfigKey("smtp.login"));
82             if(helo.equals("")) {
83                 client.login();
84             }
85             else {
86                 client.login(helo);
87             }
88             int idx = 0;
89             for (Iterator JavaDoc i = l.iterator(); i.hasNext();) {
90                 Recipient r = (Recipient) i.next();
91                 UserDatabase udb = UserDatabaseManager.getInstance().getUserDatabase(r.getRealmName());
92                 User u = udb.getAccount(r.getRecipientAlias());
93                 
94                 String JavaDoc sender = Property.getProperty(new SystemConfigKey("smtp.senderAddress"));
95                 client.setSender(sender);
96                 client.addRecipient(u.getEmail());
97                 Writer JavaDoc writer = client.sendMessageData();
98                 if (writer == null) {
99                     if(log.isInfoEnabled())
100                         log.info("Failed to send message data to " + u.getEmail());
101                     continue;
102                 }
103                 SimpleSMTPHeader header = new SimpleSMTPHeader(sender, u.getEmail(), message.getSubject());
104                 writer.write(header.toString());
105                 writer.write(message.getContent());
106                 writer.close();
107                 if (!client.completePendingCommand()) {
108                     if(log.isInfoEnabled())
109                         log.info("Failed to send message data to " + u.getEmail());
110                     continue;
111                 }
112             }
113
114             return true;
115         } finally {
116             if (client.isConnected()) {
117                 try {
118                     client.disconnect();
119                 } catch (IOException JavaDoc f) {
120                 }
121             }
122         }
123     }
124
125     public String JavaDoc getName() {
126         return "SMTP";
127     }
128
129     public String JavaDoc getShortNameKey() {
130         return "notification.smtp.shortName";
131     }
132
133     public String JavaDoc getDescriptionKey() {
134         return "notification.smtp.description";
135     }
136
137     public String JavaDoc getBundle() {
138         return "setup";
139     }
140 }
141
Popular Tags