KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > za > org > coefficient > util > common > MailUtil


1 /*
2  * Coefficient - facilitates project based collaboration
3  * Copyright (C) 2003, Dylan Etkin, CSIR icomtek
4  * PO Box 395
5  * Pretoria 0001, RSA
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  */

19
20 package za.org.coefficient.util.common;
21
22 import za.org.coefficient.core.Constants;
23
24 import java.util.Properties JavaDoc;
25
26 import javax.mail.*;
27 import javax.mail.internet.*;
28
29 /**
30  * This is a utility class that allows the system to send email notifications
31  *
32  */

33 public class MailUtil {
34     //~ Static fields/initializers =============================================
35

36     private static Session session = null;
37
38     static {
39         Properties JavaDoc props = new Properties JavaDoc();
40         props.put("mail.smtp.host", Constants.MAIL_SMTP_HOST);
41         props.put("mail.from", Constants.MAIL_SENDER_ADDRESS);
42         props.put("mail.transport.protocol", "smtp");
43         session = Session.getDefaultInstance(props, null);
44     }
45
46     //~ Methods ================================================================
47

48     public static boolean sendEmail(String JavaDoc message, String JavaDoc subject,
49         String JavaDoc recipient, String JavaDoc sender) {
50         if (session == null) {
51             return false;
52         }
53         boolean retVal = true;
54         try {
55             MimeMessage msg = new MimeMessage(session);
56             msg.setContent(message, "text/html");
57             Address recp = new InternetAddress(recipient);
58             if (sender != null) {
59                 Address snd = new InternetAddress(sender);
60                 msg.setFrom(snd);
61             }
62             msg.addRecipient(Message.RecipientType.TO, recp);
63             msg.setSubject(subject);
64             Transport.send(msg);
65         } catch (MessagingException e) {
66             e.printStackTrace();
67             retVal = false;
68         }
69
70         return retVal;
71     }
72 }
73
Popular Tags