1 package org.apache.turbine.util.mail;
2
3 /* ====================================================================
4 * The Apache Software License, Version 1.1
5 *
6 * Copyright (c) 2001 The Apache Software Foundation. All rights
7 * reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 *
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 *
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in
18 * the documentation and/or other materials provided with the
19 * distribution.
20 *
21 * 3. The end-user documentation included with the redistribution,
22 * if any, must include the following acknowledgment:
23 * "This product includes software developed by the
24 * Apache Software Foundation (http://www.apache.org/)."
25 * Alternately, this acknowledgment may appear in the software itself,
26 * if and wherever such third-party acknowledgments normally appear.
27 *
28 * 4. The names "Apache" and "Apache Software Foundation" and
29 * "Apache Turbine" must not be used to endorse or promote products
30 * derived from this software without prior written permission. For
31 * written permission, please contact apache@apache.org.
32 *
33 * 5. Products derived from this software may not be called "Apache",
34 * "Apache Turbine", nor may "Apache" appear in their name, without
35 * prior written permission of the Apache Software Foundation.
36 *
37 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
38 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
39 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
40 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
41 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
42 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
43 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
44 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
45 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
46 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
47 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
48 * SUCH DAMAGE.
49 * ====================================================================
50 *
51 * This software consists of voluntary contributions made by many
52 * individuals on behalf of the Apache Software Foundation. For more
53 * information on the Apache Software Foundation, please see
54 * <http://www.apache.org/>.
55 */
56
57 import java.util.Date;
58 import java.util.Properties;
59 import java.util.Vector;
60 import javax.mail.Message;
61 import javax.mail.MessagingException;
62 import javax.mail.Session;
63 import javax.mail.Transport;
64 import javax.mail.internet.InternetAddress;
65 import javax.mail.internet.MimeMessage;
66 import org.apache.torque.util.Criteria;
67 import org.apache.turbine.services.resources.TurbineResources;
68
69 /***
70 * The base class for all email messages. This class sets the
71 * sender's email & name, receiver's email & name, subject, and the
72 * sent date. Subclasses are responsible for setting the message
73 * body.
74 *
75 * @author <a href="mailto:jon@latchkey.com">Jon S. Stevens</a>
76 * @author <a href="mailto:frank.kim@clearink.com">Frank Y. Kim</a>
77 * @author <a href="mailto:bmclaugh@algx.net">Brett McLaughlin</a>
78 * @author <a href="mailto:greg@shwoop.com">Greg Ritter</a>
79 * @author <a href="mailto:unknown">Regis Koenig</a>
80 * @version $Id: Email.java,v 1.4 2002/07/11 16:53:20 mpoeschl Exp $
81 */
82 public abstract class Email
83 {
84 /*** Constants used to Email classes. */
85 public static final String SENDER_EMAIL = "sender.email";
86 public static final String SENDER_NAME = "sender.name";
87 public static final String RECEIVER_EMAIL = "receiver.email";
88 public static final String RECEIVER_NAME = "receiver.name";
89 public static final String EMAIL_SUBJECT = "email.subject";
90 public static final String EMAIL_BODY = "email.body";
91 public static final String CONTENT_TYPE = "content.type";
92
93 public static final String MAIL_SERVER = "mail.server";
94 public static final String MAIL_HOST = "mail.host";
95 public static final String MAIL_SMTP_FROM = "mail.smtp.from";
96 public static final String MAIL_TRANSPORT_PROTOCOL = "mail.transport.protocol";
97 public static final String SMTP = "SMTP";
98 public static final String TEXT_HTML = "text/html";
99 public static final String TEXT_PLAIN = "text/plain";
100 public static final String ATTACHMENTS = "attachments";
101 public static final String FILE_SERVER = "file.server";
102
103 public static final String KOI8_R = "koi8-r";
104 public static final String ISO_8859_1 = "iso-8859-1";
105 public static final String US_ASCII = "us-ascii";
106
107 /*** The email message to send. */
108 protected MimeMessage message;
109
110 /*** The charset to use for this message */
111 protected String charset = null;
112
113 /*** Lists of related email adresses */
114 private Vector toList;
115 private Vector ccList;
116 private Vector bccList;
117 private Vector replyList;
118
119 /***
120 * Set the charset of the message.
121 *
122 * @param charset A String.
123 */
124 public void setCharset(String charset)
125 {
126 this.charset = charset;
127 }
128
129
130 /***
131 * TODO: Document.
132 *
133 * @return A Session.
134 */
135 private Session getMailSession()
136 {
137 Properties properties = System.getProperties();
138 properties.put(MAIL_TRANSPORT_PROTOCOL, SMTP);
139 properties.put(MAIL_HOST, TurbineResources.getString(MAIL_SERVER));
140 String mailSMTPFrom = TurbineResources.getString(MAIL_SMTP_FROM);
141 if(mailSMTPFrom!=null && !mailSMTPFrom.equals(""))
142 {
143 properties.put(MAIL_SMTP_FROM, mailSMTPFrom);
144 }
145 return Session.getDefaultInstance(properties, null);
146 }
147
148 /***
149 * Initializes the mail.
150 *
151 * Deprecated.
152 *
153 * @param criteria A Criteria.
154 * @exception MessagingException.
155 * @see #init() init.
156 */
157 protected void initialize(Criteria criteria) throws MessagingException
158 {
159 init();
160 initCriteria(criteria);
161 }
162
163 /***
164 * Initializes the mail.
165 *
166 * <p>This is the first method that should be called by a subclass
167 * in its constructor.
168 *
169 * @exception MessagingException.
170 */
171 protected void init() throws MessagingException
172 {
173
174 // Create the message.
175 message = new MimeMessage(getMailSession());
176
177 toList = new Vector();
178 ccList = new Vector();
179 bccList = new Vector();
180 replyList = new Vector();
181
182 // Set the sent date.
183 setSentDate(new Date());
184 }
185
186 /***
187 * Initialize the mail according to the Criteria.
188 *
189 * <p>This method uses the criteria parameter to set the from, to
190 * and subject fields of the email.
191 *
192 * Deprecated; one should use the setFrom, addTo, etc. methods.
193 *
194 * @param criteria A Criteria.
195 * @exception MessagingException.
196 */
197 protected void initCriteria(Criteria criteria) throws MessagingException
198 {
199 // Set the FROM field.
200 if (criteria.containsKey(SENDER_EMAIL)
201 && criteria.containsKey(SENDER_NAME))
202 {
203 setFrom(criteria.getString(SENDER_EMAIL),
204 criteria.getString(SENDER_NAME));
205 }
206
207 // Set the TO field.
208 if (criteria.containsKey(RECEIVER_EMAIL)
209 && criteria.containsKey(RECEIVER_NAME))
210 {
211 addTo(criteria.getString(RECEIVER_EMAIL),
212 criteria.getString(RECEIVER_NAME));
213 }
214
215 // Set the SUBJECT field.
216 if (criteria.containsKey(EMAIL_SUBJECT))
217 {
218 setSubject(criteria.getString(EMAIL_SUBJECT));
219 }
220 else
221 {
222 setSubject("no subject available");
223 }
224 }
225
226 /***
227 * Set the FROM field of the email.
228 *
229 * @param email A String.
230 * @param name A String.
231 * @return An Email.
232 * @exception MessagingException.
233 */
234 public Email setFrom(String email, String name) throws MessagingException
235 {
236 try
237 {
238 if (name == null || name.trim().equals(""))
239 {
240 name = email;
241 }
242 message.setFrom(new InternetAddress(email, name));
243 }
244 catch(Exception e)
245 {
246 throw new MessagingException("cannot set from", e);
247 }
248 return this;
249 }
250
251 /***
252 * Add a recipient TO to the email.
253 *
254 * @param email A String.
255 * @param name A String.
256 * @return An Email.
257 * @exception MessagingException.
258 */
259 public Email addTo(String email, String name) throws MessagingException
260 {
261 try
262 {
263 if (name == null || name.trim().equals(""))
264 {
265 name = email;
266 }
267 toList.addElement(new InternetAddress(email, name));
268 }
269 catch (Exception e)
270 {
271 throw new MessagingException("cannot add to", e);
272 }
273 return this;
274 }
275
276 /***
277 * Add a recipient CC to the email.
278 *
279 * @param email A String.
280 * @param name A String.
281 * @return An Email.
282 * @exception MessagingException.
283 */
284 public Email addCc(String email, String name) throws MessagingException
285 {
286
287 try
288 {
289 if (name == null || name.trim().equals(""))
290 {
291 name = email;
292 }
293 ccList.addElement(new InternetAddress(email, name));
294 }
295 catch (Exception e)
296 {
297 throw new MessagingException("cannot add cc", e);
298 }
299
300 return this;
301 }
302
303 /***
304 * Add a blind BCC recipient to the email.
305 *
306 * @param email A String.
307 * @param name A String.
308 * @return An Email.
309 * @exception MessagingException.
310 */
311 public Email addBcc(String email, String name)
312 throws MessagingException
313 {
314 try
315 {
316 if (name == null || name.trim().equals(""))
317 {
318 name = email;
319 }
320 bccList.addElement(new InternetAddress(email, name));
321 }
322 catch (Exception e)
323 {
324 throw new MessagingException("cannot add bcc", e);
325 }
326
327 return this;
328 }
329
330 /***
331 * Add a reply to address to the email.
332 *
333 * @param email A String.
334 * @param name A String.
335 * @return An Email.
336 * @exception MessagingException.
337 */
338 public Email addReplyTo(String email, String name)
339 throws MessagingException
340 {
341 try
342 {
343 if (name == null || name.trim().equals(""))
344 {
345 name = email;
346 }
347 replyList.addElement(new InternetAddress(email, name));
348 }
349 catch (Exception e)
350 {
351 throw new MessagingException("cannot add replyTo", e);
352 }
353 return this;
354 }
355
356 /***
357 * Set the email subject.
358 *
359 * @param subject A String.
360 * @return An Email.
361 * @exception MessagingException.
362 */
363 public Email setSubject(String subject)
364 throws MessagingException
365 {
366 if (subject != null)
367 {
368 if (charset != null)
369 {
370 message.setSubject(subject, charset);
371 }
372 else
373 {
374 message.setSubject(subject);
375 }
376 }
377 return this;
378 }
379
380 /***
381 * Set the sent date field.
382 *
383 * @param date A Date.
384 * @return An Email.
385 * @exception MessagingException.
386 */
387 public Email setSentDate(Date date)
388 throws MessagingException
389 {
390 if (date != null)
391 {
392 message.setSentDate(date);
393 }
394 return this;
395 }
396
397 /***
398 * Define the content of the mail. It should be overidden by the
399 * subclasses.
400 *
401 * @param msg A String.
402 * @return An Email.
403 * @exception MessagingException.
404 */
405 public abstract Email setMsg(String msg)
406 throws MessagingException;
407
408 /***
409 * Does the work of actually sending the email.
410 *
411 * @exception MessagingException, if there was an error.
412 */
413 public void send()
414 throws MessagingException
415 {
416 InternetAddress[] foo = new InternetAddress[0];
417 message.setRecipients(Message.RecipientType.TO,
418 toInternetAddressArray(toList));
419 message.setRecipients(Message.RecipientType.CC,
420 toInternetAddressArray(ccList));
421 message.setRecipients(Message.RecipientType.BCC,
422 toInternetAddressArray(bccList));
423 message.setReplyTo(toInternetAddressArray(replyList));
424 Transport.send( message );
425 }
426
427 /***
428 * Utility to copy Vector of known InternetAddress objects into an
429 * array.
430 *
431 * @param v A Vector.
432 * @return An InternetAddress[].
433 */
434 private InternetAddress[] toInternetAddressArray(Vector v)
435 {
436 int size = v.size();
437 InternetAddress[] ia = new InternetAddress[size];
438 for (int i = 0; i < size; i++)
439 {
440 ia[i] = (InternetAddress) v.elementAt(i);
441 }
442 return ia;
443 }
444 }
This page was automatically generated by Maven