KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jzonic > jlo > handler > MailHandler


1 /*
2  * Created on 21.07.2003
3  *
4  * To change the template for this generated file go to
5  * Window - Preferences - Java - Code Generation - Code and Comments
6  */

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 JavaDoc;
15 import java.util.Map JavaDoc;
16 import java.util.Properties JavaDoc;
17 import java.util.StringTokenizer JavaDoc;
18
19 /**
20  * @author Andreas Mecky andreas.mecky@xcom.de
21  * @author Terry Dye terry.dye@xcom.de
22  *
23  * This handler can be used to send the log message as email.
24  * The following parameters are required:<br/>
25  * <parameter name="smtphost" value="name of the smtp server"/><br/>
26  * <parameter name="sender" value="the mail address of the sender"/><br/>
27  * <parameter name="recipient" value="list of recievers"/><br/>
28  * this one is optional:<br/>
29  * <parameter name="subject" value="the subject of the mail"/><br/>
30  * If not set the mail will be send with the subject "no subject".<br/>
31  * These parameters are required if the smtp server requires "smtp after pop"<br/>
32  * <parameter name="user" value="the user name"/><br/>
33  * <parameter name="pwd" value="the password for this user"/><br/>
34  * <parameter name="popserver" value="the name of the pop3 server"/><br/>
35  * <parameter name="authentification" value="true"/><br/>
36  *
37  */

38 public class MailHandler extends AbstractHandler {
39     
40     private String JavaDoc smtpHost;
41     private String JavaDoc popHost;
42     private String JavaDoc username;
43     private String JavaDoc pwd;
44     private boolean authentification = false;
45     private String JavaDoc from;
46     private String JavaDoc to;
47     private String JavaDoc subject;
48
49     public MailHandler(String JavaDoc configName) {
50         super(configName);
51     }
52
53     /* (non-Javadoc)
54          * @see org.jzonic.jlo.handler.Handler#publish(java.lang.String)
55          */

56     public void publish(String JavaDoc msg) {
57         sendEmail(from,to,null,subject,msg);
58         
59     }
60     
61         /* (non-Javadoc)
62          * @see org.jzonic.jlo.handler.Handler#publish(org.jzonic.jlo.LogRecord)
63          */

64     public void publish(LogRecord lr) {
65         publish(lr.getMessage());
66         
67     }
68     
69         /* (non-Javadoc)
70          * @see org.jzonic.jlo.handler.Handler#setParameter(java.util.Hashtable)
71          */

72     public void setParameter(Map JavaDoc parameters) {
73         // smtphost
74
if ( parameters.containsKey("smtphost")) {
75             smtpHost = (String JavaDoc)parameters.get("smtphost");
76         }
77         // from
78
if ( parameters.containsKey("sender")) {
79             from = (String JavaDoc)parameters.get("sender");
80         }
81         // to
82
if ( parameters.containsKey("recipient")) {
83             to = (String JavaDoc)parameters.get("recipient");
84         }
85         // subject
86
if ( parameters.containsKey("subject")) {
87             subject = (String JavaDoc)parameters.get("subject");
88         }
89         else {
90             subject = "No subject";
91         }
92         // authentification
93
if ( parameters.containsKey("authentification")) {
94             String JavaDoc tmp = (String JavaDoc)parameters.get("authentification");
95             if ( tmp.equalsIgnoreCase("true")) {
96                 authentification = true;
97             }
98         }
99         // user
100
if ( parameters.containsKey("user")) {
101             username = (String JavaDoc)parameters.get("user");
102         }
103         // pwd
104
if ( parameters.containsKey("pwd")) {
105             pwd = (String JavaDoc)parameters.get("pwd");
106         }
107         // popserver
108
if ( parameters.containsKey("popserver")) {
109             popHost = (String JavaDoc)parameters.get("popserver");
110         }
111
112     }
113     
114     private void sendEmail(String JavaDoc from, String JavaDoc recipients, String JavaDoc copies, String JavaDoc subject, String JavaDoc bodyText) {
115         try {
116             Properties JavaDoc 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 JavaDoc st = new StringTokenizer JavaDoc(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 JavaDoc(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 JavaDoc());
150             
151             // Send the message
152
Transport trans = session.getTransport("smtp");
153             trans.connect(smtpHost, username, pwd);
154             trans.sendMessage(mail, mail.getAllRecipients());
155             trans.close();
156         } catch (Exception JavaDoc e) {
157             ErrorHandler.reportError("Cannot send email", e);
158         }
159     }
160 }
161
Popular Tags