1 26 30 package org.jresearch.gossip.mail; 31 32 import java.util.Properties ; 33 34 import javax.mail.Address ; 35 import javax.mail.Message ; 36 import javax.mail.Session ; 37 import javax.mail.Transport ; 38 import javax.mail.internet.InternetAddress ; 39 import javax.mail.internet.MimeMessage ; 40 41 import org.jresearch.gossip.IConst; 42 import org.jresearch.gossip.configuration.Configurator; 43 import org.jresearch.gossip.log.LogLevel; 44 import org.jresearch.gossip.log.avalon.JGossipLog; 45 46 51 public class MailProcessor implements 52 com.evelopers.common.concurrent.QueueMessageProcessor { 53 public static Session _mailSession; 54 55 private static SmtpAuthenticator _authenticator; 56 57 62 public void processMessage(Object mess) { 63 MailMessage mailmess = (MailMessage) mess; 64 65 Session s = null; 67 Transport trans = null; 68 try { 69 s = getMailSession(); 70 71 MimeMessage msg = new MimeMessage (s); 73 Configurator config = Configurator.getInstance(); 74 msg.setText(mailmess.getMessagetext(),config.get(IConst.CONFIG.CHARSET)); 76 msg.setSubject(mailmess.getSubject(),config.get(IConst.CONFIG.CHARSET)); 77 78 Address fromAddr = new InternetAddress (mailmess.getAddrfrom(), 79 mailmess.getNamefrom()); 80 msg.setFrom(fromAddr); 81 82 Address toAddr = new InternetAddress (mailmess.getAddrto(), mailmess 83 .getNameto()); 84 msg.addRecipient(Message.RecipientType.TO, toAddr); 85 msg.addHeaderLine("Content-Type: text/html; charset=\""+config.get(IConst.CONFIG.CHARSET)+"\""); 86 msg.saveChanges(); 87 trans = s.getTransport("smtp"); 89 trans.connect(); 90 trans.sendMessage(msg, new Address [] { toAddr }); 91 trans.close(); 92 } catch (Exception e) { 93 JGossipLog.audit(LogLevel.ERROR, e.getMessage(), e); 94 e.printStackTrace(); 95 } 96 } 97 98 protected Session getMailSession() throws Exception { 100 if (_mailSession == null) { 102 _authenticator = new SmtpAuthenticator(); 103 104 Configurator config = Configurator.getInstance(); 105 Properties props = new Properties (); 106 props.put("mail.smtp.user", config.get(IConst.CONFIG.MAILUSER)); 107 props.put("mail.smtp.host", config.get(IConst.CONFIG.MAILHOST)); 108 props.put("mail.smtp.port", config 109 .get(IConst.CONFIG.SMTP_SERVER_PORT)); 110 props.put("mail.mime.charset", config.get(IConst.CONFIG.CHARSET)); 111 String smtpUser = config.get(IConst.CONFIG.MAILUSER); 112 if (!smtpUser.equals("anonymous")) 113 props.put("mail.smtp.auth", "true"); 114 _mailSession = Session.getInstance(props, _authenticator); 115 } 116 117 return _mailSession; 118 } 119 } 120 | Popular Tags |