1 7 package org.jzonic.jlo.handler; 8 9 import org.jzonic.jlo.LogRecord; 10 import org.jzonic.jlo.error.ErrorHandler; 11 12 import javax.mail.*; 13 import javax.mail.internet.*; 14 import java.util.Date ; 15 import java.util.Map ; 16 import java.util.Properties ; 17 import java.util.StringTokenizer ; 18 19 38 public class MailHandler extends AbstractHandler { 39 40 private String smtpHost; 41 private String popHost; 42 private String username; 43 private String pwd; 44 private boolean authentification = false; 45 private String from; 46 private String to; 47 private String subject; 48 49 public MailHandler(String configName) { 50 super(configName); 51 } 52 53 56 public void publish(String msg) { 57 sendEmail(from,to,null,subject,msg); 58 59 } 60 61 64 public void publish(LogRecord lr) { 65 publish(lr.getMessage()); 66 67 } 68 69 72 public void setParameter(Map parameters) { 73 if ( parameters.containsKey("smtphost")) { 75 smtpHost = (String )parameters.get("smtphost"); 76 } 77 if ( parameters.containsKey("sender")) { 79 from = (String )parameters.get("sender"); 80 } 81 if ( parameters.containsKey("recipient")) { 83 to = (String )parameters.get("recipient"); 84 } 85 if ( parameters.containsKey("subject")) { 87 subject = (String )parameters.get("subject"); 88 } 89 else { 90 subject = "No subject"; 91 } 92 if ( parameters.containsKey("authentification")) { 94 String tmp = (String )parameters.get("authentification"); 95 if ( tmp.equalsIgnoreCase("true")) { 96 authentification = true; 97 } 98 } 99 if ( parameters.containsKey("user")) { 101 username = (String )parameters.get("user"); 102 } 103 if ( parameters.containsKey("pwd")) { 105 pwd = (String )parameters.get("pwd"); 106 } 107 if ( parameters.containsKey("popserver")) { 109 popHost = (String )parameters.get("popserver"); 110 } 111 112 } 113 114 private void sendEmail(String from, String recipients, String copies, String subject, String bodyText) { 115 try { 116 Properties props = System.getProperties(); 117 props.put("mail.smtp.host", smtpHost); 118 if (authentification) { 119 props.put("mail.smtp.auth", "true"); 120 } 121 Session session = Session.getDefaultInstance(props, null); 122 if (authentification) { 123 Session pSession = Session.getInstance(props, null); 125 Store store = pSession.getStore("pop3"); 126 store.connect(popHost, username, pwd); 127 store.close(); 128 } 129 MimeMessage mail = new MimeMessage(session); 131 mail.setFrom(new InternetAddress(from)); 132 133 StringTokenizer st = new StringTokenizer (recipients, ","); 134 InternetAddress[] recList = new InternetAddress[st.countTokens()]; 135 for (int r = 0; st.hasMoreTokens(); r++) 136 recList[r] = new InternetAddress(st.nextToken().trim()); 137 mail.setRecipients(Message.RecipientType.TO, recList); 138 139 if (copies != null) { 140 st = new StringTokenizer (copies, ","); 141 InternetAddress[] copyList = new InternetAddress[st.countTokens()]; 142 for (int c = 0; st.hasMoreTokens(); c++) { 143 copyList[c] = new InternetAddress(st.nextToken().trim()); 144 } 145 mail.setRecipients(Message.RecipientType.CC, copyList); 146 } 147 mail.setSubject(subject); 148 mail.setText(bodyText); 149 mail.setSentDate(new Date ()); 150 151 Transport trans = session.getTransport("smtp"); 153 trans.connect(smtpHost, username, pwd); 154 trans.sendMessage(mail, mail.getAllRecipients()); 155 trans.close(); 156 } catch (Exception e) { 157 ErrorHandler.reportError("Cannot send email", e); 158 } 159 } 160 } 161 | Popular Tags |