KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > j2ee > blueprints > opc > mailer > MailHelper


1 /*
2 * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 *
8 * - Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 *
11 * - Redistribution in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in
13 * the documentation and/or other materials provided with the
14 * distribution.
15 *
16 * Neither the name of Sun Microsystems, Inc. or the names of
17 * contributors may be used to endorse or promote products derived
18 * from this software without specific prior written permission.
19 *
20 * This software is provided "AS IS," without a warranty of any
21 * kind. ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND
22 * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,
23 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY
24 * EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES
25 * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
26 * DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN
27 * OR ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR
28 * FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR
29 * PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF
30 * LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE SOFTWARE,
31 * EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
32 *
33 * You acknowledge that Software is not designed, licensed or intended
34 * for use in the design, construction, operation or maintenance of
35 * any nuclear facility.
36 */

37
38 package com.sun.j2ee.blueprints.opc.mailer;
39
40 import java.util.*;
41
42 import javax.naming.InitialContext JavaDoc;
43 import javax.activation.DataHandler JavaDoc;
44 import javax.mail.*;
45 import javax.mail.internet.*;
46
47
48 /**
49  * A helper class to create and send mail.
50  */

51 public class MailHelper {
52
53     /**
54      * This method creates an email message and sends it using the
55      * J2EE mail services
56      * @param mailContent contains the message contents to send
57      */

58     public void createAndSendMail(String JavaDoc emailAddress, String JavaDoc subject, String JavaDoc mailContent, Locale locale) throws MailerException {
59         try {
60             InitialContext JavaDoc ic = new InitialContext JavaDoc();
61             Session session = (Session) ic.lookup(JNDINames.MAIL_SESSION);
62             Message msg = new MimeMessage(session);
63             msg.setFrom();
64             msg.setRecipients(Message.RecipientType.TO,
65                      InternetAddress.parse(emailAddress, false));
66             msg.setSubject(subject);
67             String JavaDoc contentType = "text/html";
68             StringBuffer JavaDoc sb = new StringBuffer JavaDoc(mailContent);
69             msg.setDataHandler(new DataHandler JavaDoc(
70                               new ByteArrayDataSource(sb.toString(), contentType)));
71             msg.setHeader("X-Mailer", "JavaMailer");
72             msg.setSentDate(new Date());
73             Transport.send(msg);
74         } catch (Exception JavaDoc e) {
75             System.err.println("MailHelper caught: " + e);
76             e.printStackTrace();
77             throw new MailerException("Failure while sending mail:" + e);
78         }
79     }
80 }
81
82
Popular Tags