Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: com/clra/util/MailHelper.java


1   /*
2    * Copyright (c) Carnegie Lake Rowing Association 2002. All rights reserved.
3    * Distributed under the GPL license. See doc/COPYING.
4    * $RCSfile: MailHelper.java,v $
5    * $Date: 2003/02/26 03:38:45 $
6    * $Revision: 1.5 $
7    */
8   
9   package com.clra.util;
10  
11  import java.util.Date;
12  import java.util.Locale;
13  import java.util.Properties;
14  
15  import javax.naming.InitialContext;
16  import java.io.InputStream;
17  import javax.activation.DataHandler;
18  import java.net.URL;
19  import javax.mail.Message;
20  import javax.mail.MessagingException;
21  import javax.mail.Transport;
22  import javax.mail.Session;
23  import javax.mail.Multipart;
24  import javax.mail.internet.MimeMultipart;
25  import javax.mail.internet.MimeBodyPart;
26  import javax.mail.internet.MimeMessage;
27  import javax.mail.internet.InternetAddress;
28  import org.apache.log4j.Category;
29  import org.apache.log4j.helpers.Loader;
30  
31  /**
32   * A helper class to create and send mail.
33   */
34  public class MailHelper {
35  
36  
37      /**
38       * This method creates an email message and sends it using the
39       * J2EE mail services
40       * @param mailContent contains the message contents to send
41       */
42  
43    private final static String base = MailHelper.class.getName();
44  
45    private final static Category theLog = Category.getInstance( base );
46  
47    private final static String PN_MAILTYPE = "clra.mail.type";
48  
49    private final static String PN_MAILSERVER = "clra.mail.server";
50    
51    private final static String PN_MAILADDRESS = "clra.mail.admin.address";
52  
53    private final static String DEFAULTS_FILE =
54            "com/clra/util/util.properties";
55  
56    private final static Properties defaultProperties = new Properties();
57    static {
58      InputStream is = null;
59      try {
60        URL url = Loader.getResource( DEFAULTS_FILE, MailHelper.class );
61        is = url.openStream();
62        defaultProperties.load( is );
63        if ( theLog.isDebugEnabled() ) {
64          theLog.debug( "loaded properties from '" + DEFAULTS_FILE + "'" );
65          java.util.Enumeration _e = defaultProperties.propertyNames();
66          while ( _e.hasMoreElements() ) {
67            theLog.debug( "property: " + _e.nextElement() );
68          }
69        }
70      }
71      catch( Exception x ){
72        String msg = "unable to load default properties from '"
73                + DEFAULTS_FILE + "'";
74        theLog.fatal(msg,x);
75        throw new IllegalStateException( msg );
76      }
77      finally {
78        if ( is != null ) {
79          try { is.close(); } catch( Exception x ) {}
80          is = null;
81        }
82      } // finally
83    } // static
84  
85    private static String DEFAULT_MAIL_SERVER = 
86                                              defaultProperties.getProperty(PN_MAILSERVER);
87    private static String DEFAULT_MAIL_TYPE = 
88                                              defaultProperties.getProperty(PN_MAILTYPE);
89  
90    private static String DEFAULT_MAIL_ADDRESS = 
91                                              defaultProperties.getProperty(PN_MAILADDRESS);
92  
93    public void createAndSendMail(String emailAddress,
94                                  String subject,
95                                  String mailContent,
96                                  Locale locale) throws MailerAppException {
97          try {
98              Properties props = new Properties();
99              //props.put("mail.smtp.host", "smtp3.sympatico.ca");
100             props.put(DEFAULT_MAIL_TYPE, DEFAULT_MAIL_SERVER);
101             Session session = javax.mail.Session.getInstance(props);
102             
103             Message msg = new MimeMessage(session);
104             msg.setFrom();
105             msg.setRecipients(Message.RecipientType.TO,
106                      InternetAddress.parse(emailAddress, false));
107             msg.setSubject(subject);
108             String contentType = "text/html";
109                 StringBuffer sb = new StringBuffer(mailContent);
110             msg.setDataHandler(new DataHandler(
111                                   new ByteArrayDataSource(sb.toString(), contentType)));
112             msg.setHeader("X-Mailer", "JavaMailer");
113             msg.setSentDate(new Date());
114             Transport.send(msg);
115         } catch (Exception e) {
116             System.out.print("createAndSendMail exception : " + e);
117             throw new MailerAppException("Failure while sending mail");
118         }
119   }                                     
120   
121   public void createAndSendMail(String subject, 
122                                 String mailContent, 
123                                 Locale locale) throws MailerAppException {
124 
125         createAndSendMail(DEFAULT_MAIL_ADDRESS, subject, mailContent, locale);
126   }
127 }
128 
129 /*
130  * $Log: MailHelper.java,v $
131  * Revision 1.5  2003/02/26 03:38:45  rphall
132  * Added copyright and GPL license
133  *
134  * Revision 1.4  2003/02/19 22:09:17  rphall
135  * Removed gratuitous use of CLRA acronym
136  *
137  */
138