KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jresearch > gossip > mail > MailProcessor


1 /*
2  * $$Id: MailProcessor.java,v 1.4 2005/06/09 08:27:45 bel70 Exp $$
3  *
4  * ***** BEGIN LICENSE BLOCK *****
5  * The contents of this file are subject to the Mozilla Public License
6  * Version 1.1 (the "License"); you may not use this file except in
7  * compliance with the License. You may obtain a copy of the License
8  * at http://www.mozilla.org/MPL/
9  *
10  * Software distributed under the License is distributed on an "AS IS"
11  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
12  * the License for the specific language governing rights and
13  * limitations under the License.
14  *
15  * The Original Code is JGossip forum code.
16  *
17  * The Initial Developer of the Original Code is the JResearch, Org.
18  * Portions created by the Initial Developer are Copyright (C) 2004
19  * the Initial Developer. All Rights Reserved.
20  *
21  * Contributor(s):
22  * Dmitry Belov <bel@jresearch.org>
23  * Simone Chiaretta <simo@users.sourceforge.net>
24  *
25  * ***** END LICENSE BLOCK ***** */

26 /*
27  * Created on Oct 28, 2003
28  *
29  */

30 package org.jresearch.gossip.mail;
31
32 import java.util.Properties JavaDoc;
33
34 import javax.mail.Address JavaDoc;
35 import javax.mail.Message JavaDoc;
36 import javax.mail.Session JavaDoc;
37 import javax.mail.Transport JavaDoc;
38 import javax.mail.internet.InternetAddress JavaDoc;
39 import javax.mail.internet.MimeMessage JavaDoc;
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 /**
47  * DOCUMENT ME!
48  *
49  * @author dbelov
50  */

51 public class MailProcessor implements
52         com.evelopers.common.concurrent.QueueMessageProcessor {
53     public static Session JavaDoc _mailSession;
54
55     private static SmtpAuthenticator _authenticator;
56
57     /*
58      * (non-Javadoc)
59      *
60      * @see com.evelopers.common.concurrent.QueueMessageProcessor#processMessage(java.lang.Object)
61      */

62     public void processMessage(Object JavaDoc mess) {
63         MailMessage mailmess = (MailMessage) mess;
64
65         // Get (or create and cache) mail session
66
Session JavaDoc s = null;
67         Transport JavaDoc trans = null;
68         try {
69             s = getMailSession();
70
71             // Create new message
72
MimeMessage JavaDoc msg = new MimeMessage JavaDoc(s);
73             Configurator config = Configurator.getInstance();
74             // Put data from request into message
75
msg.setText(mailmess.getMessagetext(),config.get(IConst.CONFIG.CHARSET));
76             msg.setSubject(mailmess.getSubject(),config.get(IConst.CONFIG.CHARSET));
77
78             Address JavaDoc fromAddr = new InternetAddress JavaDoc(mailmess.getAddrfrom(),
79                     mailmess.getNamefrom());
80             msg.setFrom(fromAddr);
81
82             Address JavaDoc toAddr = new InternetAddress JavaDoc(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             // Send the message
88
trans = s.getTransport("smtp");
89             trans.connect();
90             trans.sendMessage(msg, new Address JavaDoc[] { toAddr });
91             trans.close();
92         } catch (Exception JavaDoc e) {
93             JGossipLog.audit(LogLevel.ERROR, e.getMessage(), e);
94             e.printStackTrace();
95         }
96     }
97
98     // Open the mail session if it isn't already open.
99
protected Session JavaDoc getMailSession() throws Exception JavaDoc {
100         // Create mail session if it doesn't exist
101
if (_mailSession == null) {
102             _authenticator = new SmtpAuthenticator();
103
104             Configurator config = Configurator.getInstance();
105             Properties JavaDoc props = new Properties JavaDoc();
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 JavaDoc 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