KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mmbase > applications > email > SendMail


1 /*
2
3 This software is OSI Certified Open Source Software.
4 OSI Certified is a certification mark of the Open Source Initiative.
5
6 The license (Mozilla version 1.0) can be read at the MMBase site.
7 See http://www.MMBase.org/license
8
9 */

10 package org.mmbase.applications.email;
11
12 import java.util.*;
13
14 import javax.mail.*;
15 import javax.mail.internet.*;
16 import javax.naming.*;
17
18 import org.mmbase.module.core.MMBase;
19 import org.mmbase.util.logging.*;
20
21 /**
22  * Module providing mail functionality based on JavaMail, mail-resources.
23  *
24  * @author Case Roole
25  * @author Michiel Meeuwissen
26  * @author Daniel Ockeloen
27  * @since MMBase-1.6
28  * @version $Id: SendMail.java,v 1.18.2.1 2006/10/16 15:52:12 michiel Exp $
29  */

30 public class SendMail extends AbstractSendMail implements SendMailInterface {
31     private static final Logger log = Logging.getLoggerInstance(SendMail.class);
32
33     public static final String JavaDoc DEFAULT_MAIL_ENCODING = "ISO-8859-1";
34
35     public static String JavaDoc mailEncoding = DEFAULT_MAIL_ENCODING;
36
37     public static long emailSent = 0;
38     public static long emailFailed = 0;
39
40     /**
41      */

42     public boolean sendMultiPartMail(String JavaDoc from, String JavaDoc to, Map headers, MimeMultipart mmpart) {
43         try {
44
45             MimeMessage msg = constructMessage(from, to, headers);
46
47             msg.setContent(mmpart);
48
49             Transport.send(msg);
50
51             emailSent++;
52             log.debug("JMimeSendMail done.");
53             return true;
54         } catch (javax.mail.MessagingException JavaDoc e) {
55             emailFailed++;
56             log.error("JMimeSendMail failure: " + e.getMessage());
57             log.debug(Logging.stackTrace(e));
58         }
59         return false;
60     }
61
62     /**
63      * {@inheritDoc}
64      */

65     public String JavaDoc getModuleInfo() {
66         return ("Sends mail through J2EE/JavaMail, supporting MultiPart");
67     }
68
69
70     protected Session session;
71
72     /**
73      */

74     public void reload() {
75         init();
76     }
77
78     /**
79      * {@inheritDoc}
80      */

81     public void init() {
82         try {
83             MMBase mmb = MMBase.getMMBase();
84             mailEncoding = mmb.getEncoding();
85             String JavaDoc encoding = getInitParameter("encoding");
86             if (encoding != null && !encoding.equals(""))
87                 mailEncoding = encoding;
88
89             String JavaDoc smtpHost = getInitParameter("mailhost");
90             String JavaDoc context = getInitParameter("context");
91             String JavaDoc dataSource = getInitParameter("datasource");
92             session = null;
93             if (smtpHost == null) {
94                 if (context == null) {
95                     context = "java:comp/env";
96                     log.warn("The property 'context' is missing, taking default " + context);
97                 }
98                 if (dataSource == null) {
99                     dataSource = "mail/Session";
100                     log.warn("The property 'datasource' is missing, taking default " + dataSource);
101                 }
102
103                 Context initCtx = new InitialContext();
104                 Context envCtx = (Context)initCtx.lookup(context);
105                 Object JavaDoc o = envCtx.lookup(dataSource);
106                 if (o instanceof Session) {
107                     session = (javax.mail.Session JavaDoc) o;
108                 } else {
109                     log.fatal("Configured dataSource '" + dataSource + "' of context '" + context + "' is not a Session but " + (o == null ? "NULL" : "a " + o.getClass().getName()));
110                     return;
111                 }
112                 log.info("Module SendMail started (datasource = " + dataSource + " -> " + session.getProperties() + ")");
113             } else {
114                 if (context != null) {
115                     log.error("It does not make sense to have both properties 'context' and 'mailhost' in email module");
116                 }
117                 if (dataSource != null) {
118                     log.error("It does not make sense to have both properties 'datasource' and 'mailhost' in email module");
119                 }
120                 log.info(
121                     "EMail module is configured using 'mailhost' property.\n"
122                         + "Consider using J2EE compliant 'context' and 'datasource'\n"
123                         + "Which means to put something like this in your web.xml:\n"
124                         + " <resource-ref>\n"
125                         + " <description>Email module mail resource</description>\n"
126                         + " <res-ref-name>mail/MMBase</res-ref-name>\n"
127                         + " <res-type>javax.mail.Session</res-type>\n"
128                         + " <res-auth>Container</res-auth>\n"
129                         + " </resource-ref>\n"
130                         + " + some app-server specific configuration (e.g. in orion the 'mail-session' entry in the application XML)");
131
132                 Properties prop = System.getProperties();
133                 prop.put("mail.smtp.host", smtpHost);
134                 session = Session.getInstance(prop, null);
135                 log.info("Module SendMail started (smtphost = " + smtpHost + ")");
136             }
137
138         } catch (javax.naming.NamingException JavaDoc e) {
139             log.fatal("SendMail failure: " + e.getMessage());
140             log.debug(Logging.stackTrace(e));
141         }
142     }
143
144     /**
145      * Utility method to do the generic job of creating a MimeMessage object and setting its recipients and 'from'.
146      */

147     protected MimeMessage constructMessage(String JavaDoc from, String JavaDoc to, Map headers) throws MessagingException {
148         if (log.isServiceEnabled()) {
149             log.service("SendMail sending mail to " + to);
150         }
151         // construct a message
152
MimeMessage msg = new MimeMessage(session);
153         if (from != null && !from.equals("")) {
154             msg.addFrom(InternetAddress.parse(from));
155         }
156
157         msg.addRecipients(Message.RecipientType.TO, InternetAddress.parse(to));
158
159         if (headers.get("CC") != null) {
160             msg.addRecipients(Message.RecipientType.CC, InternetAddress.parse((String JavaDoc)headers.get("CC")));
161         }
162         if (headers.get("BCC") != null) {
163             msg.addRecipients(Message.RecipientType.CC, InternetAddress.parse((String JavaDoc)headers.get("BCC")));
164         }
165
166         if (headers.get("Reply-To") != null) {
167             msg.setReplyTo(InternetAddress.parse((String JavaDoc)headers.get("Reply-To")));
168         }
169
170         msg.setSubject((String JavaDoc)headers.get("Subject"));
171
172         return msg;
173     }
174
175     /**
176      * Send mail with headers
177      */

178     public boolean sendMail(String JavaDoc from, String JavaDoc to, String JavaDoc data, Map headers) {
179         try {
180             MimeMessage msg = constructMessage(from, to, headers);
181
182             msg.setText(data, mailEncoding);
183             Transport.send(msg);
184             log.debug("SendMail done.");
185             return true;
186         } catch (MessagingException e) {
187             log.error("SendMail failure: " + e.getMessage() + "from: " + from + " to: " + to);
188             log.debug(Logging.stackTrace(e));
189         }
190         return false;
191     }
192 }
193
Popular Tags