001: /*
002: * Created on 21.07.2003
003: *
004: * To change the template for this generated file go to
005: * Window - Preferences - Java - Code Generation - Code and Comments
006: */
007: package org.jzonic.jlo.handler;
008:
009: import org.jzonic.jlo.LogRecord;
010: import org.jzonic.jlo.error.ErrorHandler;
011:
012: import javax.mail.*;
013: import javax.mail.internet.*;
014: import java.util.Date;
015: import java.util.Map;
016: import java.util.Properties;
017: import java.util.StringTokenizer;
018:
019: /**
020: * @author Andreas Mecky andreas.mecky@xcom.de
021: * @author Terry Dye terry.dye@xcom.de
022: *
023: * This handler can be used to send the log message as email.
024: * The following parameters are required:<br/>
025: * <parameter name="smtphost" value="name of the smtp server"/><br/>
026: * <parameter name="sender" value="the mail address of the sender"/><br/>
027: * <parameter name="recipient" value="list of recievers"/><br/>
028: * this one is optional:<br/>
029: * <parameter name="subject" value="the subject of the mail"/><br/>
030: * If not set the mail will be send with the subject "no subject".<br/>
031: * These parameters are required if the smtp server requires "smtp after pop"<br/>
032: * <parameter name="user" value="the user name"/><br/>
033: * <parameter name="pwd" value="the password for this user"/><br/>
034: * <parameter name="popserver" value="the name of the pop3 server"/><br/>
035: * <parameter name="authentification" value="true"/><br/>
036: *
037: */
038: public class MailHandler extends AbstractHandler {
039:
040: private String smtpHost;
041: private String popHost;
042: private String username;
043: private String pwd;
044: private boolean authentification = false;
045: private String from;
046: private String to;
047: private String subject;
048:
049: public MailHandler(String configName) {
050: super (configName);
051: }
052:
053: /* (non-Javadoc)
054: * @see org.jzonic.jlo.handler.Handler#publish(java.lang.String)
055: */
056: public void publish(String msg) {
057: sendEmail(from, to, null, subject, msg);
058:
059: }
060:
061: /* (non-Javadoc)
062: * @see org.jzonic.jlo.handler.Handler#publish(org.jzonic.jlo.LogRecord)
063: */
064: public void publish(LogRecord lr) {
065: publish(lr.getMessage());
066:
067: }
068:
069: /* (non-Javadoc)
070: * @see org.jzonic.jlo.handler.Handler#setParameter(java.util.Hashtable)
071: */
072: public void setParameter(Map parameters) {
073: // smtphost
074: if (parameters.containsKey("smtphost")) {
075: smtpHost = (String) parameters.get("smtphost");
076: }
077: // from
078: if (parameters.containsKey("sender")) {
079: from = (String) parameters.get("sender");
080: }
081: // to
082: if (parameters.containsKey("recipient")) {
083: to = (String) parameters.get("recipient");
084: }
085: // subject
086: if (parameters.containsKey("subject")) {
087: subject = (String) parameters.get("subject");
088: } else {
089: subject = "No subject";
090: }
091: // authentification
092: if (parameters.containsKey("authentification")) {
093: String tmp = (String) parameters.get("authentification");
094: if (tmp.equalsIgnoreCase("true")) {
095: authentification = true;
096: }
097: }
098: // user
099: if (parameters.containsKey("user")) {
100: username = (String) parameters.get("user");
101: }
102: // pwd
103: if (parameters.containsKey("pwd")) {
104: pwd = (String) parameters.get("pwd");
105: }
106: // popserver
107: if (parameters.containsKey("popserver")) {
108: popHost = (String) parameters.get("popserver");
109: }
110:
111: }
112:
113: private void sendEmail(String from, String recipients,
114: 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: // Log onto pop server first as security protocol may require this
124: Session pSession = Session.getInstance(props, null);
125: Store store = pSession.getStore("pop3");
126: store.connect(popHost, username, pwd);
127: store.close();
128: }
129: // Create a mime message
130: MimeMessage mail = new MimeMessage(session);
131: mail.setFrom(new InternetAddress(from));
132:
133: StringTokenizer st = new StringTokenizer(recipients, ",");
134: InternetAddress[] recList = new InternetAddress[st
135: .countTokens()];
136: for (int r = 0; st.hasMoreTokens(); r++)
137: recList[r] = new InternetAddress(st.nextToken().trim());
138: mail.setRecipients(Message.RecipientType.TO, recList);
139:
140: if (copies != null) {
141: st = new StringTokenizer(copies, ",");
142: InternetAddress[] copyList = new InternetAddress[st
143: .countTokens()];
144: for (int c = 0; st.hasMoreTokens(); c++) {
145: copyList[c] = new InternetAddress(st.nextToken()
146: .trim());
147: }
148: mail.setRecipients(Message.RecipientType.CC, copyList);
149: }
150: mail.setSubject(subject);
151: mail.setText(bodyText);
152: mail.setSentDate(new Date());
153:
154: // Send the message
155: Transport trans = session.getTransport("smtp");
156: trans.connect(smtpHost, username, pwd);
157: trans.sendMessage(mail, mail.getAllRecipients());
158: trans.close();
159: } catch (Exception e) {
160: ErrorHandler.reportError("Cannot send email", e);
161: }
162: }
163: }
|