1 /*
2 * $Source: /usr/cvsroot/melati/melati/src/main/java/org/melati/util/Email.java,v $
3 * $Revision: 1.31 $
4 *
5 * Copyright (C) 2006 Tim Pizey
6 *
7 * Part of Melati (http://melati.org), a framework for the rapid
8 * development of clean, maintainable web applications.
9 *
10 * Melati is free software; Permission is granted to copy, distribute
11 * and/or modify this software under the terms either:
12 *
13 * a) the GNU General Public License as published by the Free Software
14 * Foundation; either version 2 of the License, or (at your option)
15 * any later version,
16 *
17 * or
18 *
19 * b) any version of the Melati Software License, as published
20 * at http://melati.org
21 *
22 * You should have received a copy of the GNU General Public License and
23 * the Melati Software License along with this program;
24 * if not, write to the Free Software Foundation, Inc.,
25 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA to obtain the
26 * GNU General Public License and visit http://melati.org to obtain the
27 * Melati Software License.
28 *
29 * Feel free to contact the Developers of Melati (http://melati.org),
30 * if you would like to work out a different arrangement than the options
31 * outlined here. It is our intention to allow Melati to be used by as
32 * wide an audience as possible.
33 *
34 * This program is distributed in the hope that it will be useful,
35 * but WITHOUT ANY WARRANTY; without even the implied warranty of
36 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
37 * GNU General Public License for more details.
38 *
39 * Contact details for copyright holder:
40 *
41 * Tim Pizey <timp At paneris.org>
42 * http://paneris.org/~timp
43 */
44 package org.melati.util;
45
46 import java.io.File;
47 import java.io.IOException;
48 import javax.mail.Address;
49 import javax.mail.Message;
50 import javax.mail.MessagingException;
51 import javax.mail.Multipart;
52 import javax.mail.Session;
53 import javax.mail.Transport;
54 import javax.mail.internet.InternetAddress;
55 import javax.mail.internet.MimeBodyPart;
56 import javax.mail.internet.MimeMessage;
57 import javax.mail.internet.MimeMultipart;
58 //import org.melati.poem.Database;
59
60 import javax.activation.DataHandler;
61 import javax.activation.FileDataSource;
62
63 /**
64 * Send an email to one or more recipients with or without attachments.
65 */
66 public final class Email {
67
68 /** Property key for the SMTP server.*/
69 public static String SMTPSERVER = "SMTPServer";
70
71 private Email() {
72 }
73
74 /**
75 * deprecated try to disentangle poem and utils
76 */
77 /*
78 public static void send(Database database, String from, String to,
79 String replyto, String subject, String text)
80 throws EmailException, IOException {
81 File[] empty = {};
82 String smtpServer = database.getSettingTable().get(SMTPSERVER);
83 sendWithAttachments(smtpServer, from, to, replyto, subject, text, empty);
84 }
85 */
86 /**
87 * Send the email.
88 * @param smtpServer name of SMTP server to use
89 * @param from email address and optionally name of sender
90 * @param to email address and optionally name of recipient
91 * @param replyto email address and optionally name to reply to
92 * @param subject subject of message
93 * @param text text body of email
94 */
95 public static void send(String smtpServer, String from, String to,
96 String replyto, String subject, String text)
97 throws EmailException, IOException {
98 File[] empty = {};
99 sendWithAttachments(smtpServer, from, to, replyto, subject, text, empty);
100 }
101
102 /*
103 public static void sendToList(Database database, String from,
104 String[] toList, String apparentlyTo, String replyto, String subject,
105 String message) throws EmailException, IOException {
106 File[] empty = {};
107
108 String smtpServer = database.getSettingTable().get(SMTPSERVER);
109 for (int i = 0; i < toList.length; i++)
110 sendWithAttachments(smtpServer, from, toList[i], replyto, subject, message, empty);
111 }
112 */
113 /**
114 * Send the email to a list of recipients.
115 *
116 * @param smtpServer name of SMTP server to use
117 * @param from email address and optionally name of sender
118 * @param toList list of email addresses and optionally names of recipients
119 * @param replyto email address and optionally name to reply to
120 * @param subject subject of message
121 * @param message text body of email
122 */
123 public static void sendToList(String smtpServer, String from,
124 String[] toList, String replyto, String subject,
125 String message) throws EmailException, IOException {
126 File[] empty = {};
127
128 for (int i = 0; i < toList.length; i++)
129 sendWithAttachments(smtpServer, from, toList[i], replyto, subject, message, empty);
130 }
131
132 /**
133 * Send message with attachments.
134 *
135 * @param smtpServer name of SMTP server to use
136 * @param from email address and optionally name of sender
137 * @param to email address and optionally name of recipient
138 * @param replyto email address and optionally name to reply to
139 * @param subject subject of message
140 * @param text text body of email
141 * @param attachments Array of files to attach
142 */
143 public static void sendWithAttachments(String smtpServer, String from,
144 String to, String replyto, String subject, String text, File[] attachments)
145 throws EmailException, IOException {
146
147 // Construct the message
148 Message message = initialiseMessage(smtpServer, from, to, replyto, subject);
149 try {
150 // create and fill the first, text message part
151 MimeBodyPart mbp1 = new MimeBodyPart();
152 mbp1.setText(text);
153 Multipart mp = new MimeMultipart();
154 mp.addBodyPart(mbp1);
155 for (int i = 0; i < attachments.length; i++) {
156 File f = attachments[i];
157 if (f != null) {
158 // create the second message part
159 MimeBodyPart mbp2 = new MimeBodyPart();
160 // attach the file to the message
161 FileDataSource fds = new FileDataSource(f);
162 mbp2.setDataHandler(new DataHandler(fds));
163 mbp2.setFileName(fds.getName());
164 mp.addBodyPart(mbp2);
165 }
166 }
167 // add the Multipart to the message
168 message.setContent(mp);
169 } catch (Exception e) {
170 e.printStackTrace();
171 throw new EmailException("Problem creating message: " + e.toString());
172 }
173 // send the message
174 post(message);
175 }
176
177 /**
178 * Send HTML message with attachments.
179 *
180 * @param smtpServer name of SMTP server to use
181 * @param from email address and optionally name of sender
182 * @param to email address and optionally name of recipient
183 * @param replyto email address and optionally name to reply to
184 * @param subject subject of message
185 * @param plainText text body of email
186 * @param htmlText HTML body of email
187 * @param referenced Array of Files referenced withing the HTML body
188 * @param attachments Array of files to attach
189 */
190 public static void sendAsHtmlWithAttachments(String smtpServer, String from,
191 String to, String replyto, String subject, String plainText,
192 String htmlText, File[] referenced, File[] attachments)
193 throws EmailException, IOException {
194
195 // Construct the message
196 Message message = initialiseMessage(smtpServer, from, to, replyto, subject);
197 try {
198 Multipart mp = new MimeMultipart("related");
199 MimeBodyPart mbp1 = new MimeBodyPart();
200 //mbp1.setText(plainText);
201 mbp1.setContent(plainText, "text/plain");
202 mp.addBodyPart(mbp1);
203 MimeBodyPart mbp2 = new MimeBodyPart();
204 mbp2.setContent(htmlText, "text/html");
205 mp.addBodyPart(mbp2);
206
207 if (referenced != null) {
208 for (int i = 0; i < referenced.length; i++) {
209 File f = referenced[i];
210 if (f != null) {
211 MimeBodyPart mbp3 = new MimeBodyPart();
212 FileDataSource fds = new FileDataSource(f);
213 mbp3.setDataHandler(new DataHandler(fds));
214 mbp3.setFileName(fds.getName());
215 mp.addBodyPart(mbp3);
216 }
217 }
218 }
219 if (attachments != null) {
220 for (int i = 0; i < attachments.length; i++) {
221 File f = attachments[i];
222 if (f != null) {
223 MimeBodyPart mbp4 = new MimeBodyPart();
224 if (f.getName() == null) {
225 System.out.println("name is null");
226 }
227 FileDataSource fds = new FileDataSource(f);
228 mbp4.setDataHandler(new DataHandler(fds));
229 mbp4.setFileName(fds.getName());
230 mp.addBodyPart(mbp4);
231 }
232 }
233 }
234 // add the Multipart to the message
235 message.setContent(mp);
236 } catch (Exception e) {
237 e.printStackTrace();
238 throw new EmailException("Problem creating message: " + e.toString());
239 }
240 // send the message
241 post(message);
242 }
243
244 private static Message initialiseMessage(String smtpServer, String from,
245 String to, String replyto, String subject) throws EmailException {
246 // Create the JavaMail session
247 // The properties for the whole system, sufficient to send a mail
248 // and much more besides.
249 java.util.Properties properties = System.getProperties();
250 properties.put("mail.smtp.host", smtpServer);
251 // this is required if InetAddress.getLocalHost().getHostName() returns null
252 // which it does nor me in Eclipse
253 properties.put("mail.smtp.localhost", smtpServer);
254 Session session = Session.getInstance(properties, null);
255 MimeMessage message = new MimeMessage(session);
256 // Set the from address
257 Address fromAddress;
258 try {
259 fromAddress = new InternetAddress(from);
260 message.setFrom(fromAddress);
261 // Parse and set the recipient addresses
262 Address[] toAddresses = InternetAddress.parse(to);
263 message.setRecipients(Message.RecipientType.TO, toAddresses);
264 /*
265 * Address[] ccAddresses = InternetAddress.parse(cc);
266 * message.setRecipients(Message.RecipientType.CC,ccAddresses);
267 *
268 * Address[] bccAddresses = InternetAddress.parse(bcc);
269 * message.setRecipients(Message.RecipientType.BCC,bccAddresses);
270 */
271 if (replyto != null) {
272 Address[] replyTos = InternetAddress.parse(replyto);
273 message.setReplyTo(replyTos);
274 }
275 message.setSubject(subject);
276 } catch (Exception e) {
277 e.printStackTrace();
278 throw new EmailException("Problem sending message: " + e.toString());
279 }
280 return message;
281 }
282
283 private static void post(Message message) throws EmailException {
284 try {
285 Transport.send(message);
286 } catch (MessagingException e) {
287 e.printStackTrace();
288 throw new EmailException("Problem sending message: " + e.toString());
289 }
290
291 }
292 /**
293 * @return a fancy email address
294 */
295 public static String mailAddress(String name, String email) {
296 return name + " <" + email + ">";
297 }
298
299 }