1 19 20 package com.sslexplorer.notification.smtp; 21 22 import java.io.IOException ; 23 import java.io.Writer ; 24 import java.util.Iterator ; 25 import java.util.List ; 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 { 54 this.notifier = notifier; 55 } 56 57 public void stop() throws Exception { 58 } 59 60 public boolean send(Message message) throws Exception { 61 try { 62 List 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 ("SMTP server refused connection."); 78 } 79 if (log.isDebugEnabled()) 80 log.debug("Logging in"); 81 String 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 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 sender = Property.getProperty(new SystemConfigKey("smtp.senderAddress")); 95 client.setSender(sender); 96 client.addRecipient(u.getEmail()); 97 Writer 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 f) { 120 } 121 } 122 } 123 } 124 125 public String getName() { 126 return "SMTP"; 127 } 128 129 public String getShortNameKey() { 130 return "notification.smtp.shortName"; 131 } 132 133 public String getDescriptionKey() { 134 return "notification.smtp.description"; 135 } 136 137 public String getBundle() { 138 return "setup"; 139 } 140 } 141 | Popular Tags |