1 16 package dlog4j.plugins; 17 18 import java.io.*; 19 import java.util.*; 20 21 import javax.mail.*; 22 import javax.naming.*; 23 import javax.mail.internet.*; 24 import javax.naming.directory.*; 25 import javax.servlet.ServletException ; 26 27 import org.apache.struts.action.*; 28 import org.apache.commons.logging.*; 29 import org.apache.struts.config.ModuleConfig; 30 31 import dlog4j.util.mail.Mailer; 32 33 42 public class MailPlugin extends Mailer implements PlugIn { 43 44 private static Log log = LogFactory.getLog(MailPlugin.class); 45 46 private String sender = "dlog4j@gmail.com"; 47 private DirContext context; 48 49 52 public void init(ActionServlet servlet, ModuleConfig config) 53 throws ServletException { 54 Hashtable env = new Hashtable(); 55 env.put(Context.INITIAL_CONTEXT_FACTORY,"com.sun.jndi.dns.DnsContextFactory"); 56 env.put(Context.PROVIDER_URL, "dns://"); 57 try{ 58 context = new InitialDirContext(env); 59 }catch(NamingException e){ 60 log.fatal("Mail Messenger start failed.", e); 61 } 62 mailer = this; 63 } 64 65 72 public void send(final String sn, 73 final String [] mails, 74 final String title, 75 final String content) 76 { 77 new Thread (){ 78 public void run(){ 79 for(int i=0;i<mails.length;i++){ 80 try { 81 String domain = parseDomain(mails[i]); 82 Attributes attr = context.getAttributes(domain, new String []{"MX"}); 83 NamingEnumeration servers = attr.getAll(); 84 while (servers.hasMore()) { 86 Attribute hosts = (Attribute) servers.next(); 87 for (int j = 0; j < hosts.size(); j++) { 88 String host = (String ) hosts.get(j); 89 host = host.substring(host.indexOf(' ') + 1); 90 sendMail(sn, mails[i], title, content, host); 91 break; 92 } 93 break; 94 } 95 } catch (Exception e) { 96 log.error("Send mail to "+mails[i],e); 97 } 98 } 99 } 100 101 111 private void sendMail(String sn, String mail, String title, String content, String host) 112 throws UnsupportedEncodingException, MessagingException 113 { 114 Properties props = System.getProperties(); 115 props.put("mail.smtp.host", host); 116 props.put("mail.smtp.port", "25"); 117 props.put("mail.smtp.auth", "false"); 118 Session mailSession = Session.getInstance(props, null); 119 120 MimeMessage mailMessage = new MimeMessage(mailSession); 121 122 MimeBodyPart bodyPart = new MimeBodyPart(); 123 bodyPart.setContent(content, "text/html;charset=GB2312"); 124 Multipart multipart = new MimeMultipart("related"); 125 multipart.addBodyPart(bodyPart); 126 mailMessage.setContent(multipart); 127 128 mailMessage.setSentDate(new Date()); 129 mailMessage.setFrom(new InternetAddress(sender, sn)); 130 mailMessage.addRecipient(Message.RecipientType.TO,new InternetAddress(mail)); 131 mailMessage.setSubject(title); 132 133 Transport.send(mailMessage); 134 } 135 140 private String parseDomain(String email){ 141 String domain = null; 142 if(email!=null){ 143 int idx = email.indexOf('@'); 144 if(idx != -1){ 145 idx++; 146 if(idx<email.length()) 147 domain = email.substring(idx); 148 } 149 } 150 return domain; 151 } 152 }.start(); 153 } 154 155 159 public void destroy() { 160 try{ 161 context.close(); 162 }catch(Exception e){ 163 log.error("Mail Messenger close context failed.", e); 164 } 165 } 166 167 public String getSender() { 168 return sender; 169 } 170 public void setSender(String sender) { 171 this.sender = sender; 172 } 173 } 174 | Popular Tags |