View Javadoc

1   /* $Id: EmailMessenger.java,v 1.2 2004/06/02 20:10:05 johndavidtaylor Exp $
2    * Created on Mar 14, 2004 by jdt
3    * The alipes project
4    * (c) 2004 
5    *
6    */
7   package org.abraracourcix.alipes.common.messaging;
8   import java.util.Date;
9   
10  import javax.mail.Message;
11  import javax.mail.MessagingException;
12  import javax.mail.Session;
13  import javax.mail.Transport;
14  import javax.mail.internet.InternetAddress;
15  import javax.mail.internet.MimeMessage;
16  /***
17   * Messenger that dispatches emails
18   * 
19   * @author jdt
20   */
21  public class EmailMessenger implements Messenger {
22      /***
23       * List of recipients, comma separated
24       */
25      private String recipients;
26      /***
27       * Mail session
28       */
29      private Session  session;
30      /***
31       * Constructor - used by EmailMessengerFactoryImpl
32       * @param session an email session
33       * @param recipients comma separated list of recipients
34       * 
35       */
36       EmailMessenger(final Session session, final String recipients) {
37          this.recipients=recipients;
38          this.session=session;
39      }
40      /***
41       * Does the donkey work of sending an email 
42       *  
43       * @param subject subject line
44       * @param message what you want to say
45       * @throws MessengerException if the email fails
46       */
47      public void sendMessage(final String subject, final String message)
48          throws MessengerException {
49          try {
50              final Message mess = new MimeMessage(session);
51              mess.setFrom(InternetAddress.getLocalAddress(session));
52              mess.setRecipients(
53                      Message.RecipientType.TO,
54                      InternetAddress.parse(recipients));
55              mess.setSubject(subject);
56              mess.setSentDate(new Date());
57              mess.setHeader("X-Mailer", "Maven Auto Build");
58              mess.setContent(message, "text/plain");
59              Transport.send(mess);
60          } catch (MessagingException me){
61              throw new MessengerException("Problem sending message", me);
62          }
63      }
64      /***
65       * @see org.abraracourcix.alipes.common.messaging.Messenger#getRecipient()
66       * @return The recipients of this message
67       */
68      public String getRecipient() {
69          return recipients;
70      }
71      
72  }
73  
74  
75  /*
76   *  $Log: EmailMessenger.java,v $
77   *  Revision 1.2  2004/06/02 20:10:05  johndavidtaylor
78   *  Refactoring to allow email templates.
79   *
80   *  Revision 1.1  2004/03/18 15:21:54  johndavidtaylor
81   *  Copied across from incubation in beanpeeler
82   *
83   *  Revision 1.1  2004/03/14 16:26:49  johndavidtaylor
84   *  refactored email
85   *
86   */