View Javadoc

1   /* $Id: EmailMessenger.java,v 1.3 2004/03/19 13:02:25 jdt Exp $
2    * Created on Mar 14, 2004 by jdt
3    * Copyright (C) AstroGrid. All rights reserved.
4    *
5    * This software is published under the terms of the AstroGrid 
6    * Software License version 1.2, a copy of which has been included 
7    * with this distribution in the LICENSE.txt file. 
8    */
9   package org.astrogrid.portal.cocoon.messaging;
10  import java.util.Date;
11  
12  import javax.mail.Message;
13  import javax.mail.MessagingException;
14  import javax.mail.Session;
15  import javax.mail.Transport;
16  import javax.mail.internet.InternetAddress;
17  import javax.mail.internet.MimeMessage;
18  /***
19   * Messenger that dispatches emails
20   * 
21   * @author jdt
22   */
23  public final class EmailMessenger implements Messenger {
24      /***
25       * List of recipients, comma separated
26       */
27      private String recipients;
28      /***
29       * Mail session
30       */
31      private Session  session;
32      /***
33       * Constructor - used by EmailMessengerFactory
34       * @param session an email session
35       * @param recipients comma separated list of recipients
36       * 
37       */
38       EmailMessenger(final Session session, final String recipients) {
39          this.recipients=recipients;
40          this.session=session;
41      }
42      /***
43       * Does the donkey work of sending an email 
44       *  
45       * @param subject subject line
46       * @param message what you want to say
47       * @throws MessengerException if the email fails
48       */
49      public void sendMessage(final String subject, final String message)
50          throws MessengerException {
51          try {
52              final Message mess = new MimeMessage(session);
53              mess.setFrom(InternetAddress.getLocalAddress(session));
54              mess.setRecipients(
55                      Message.RecipientType.TO,
56                      InternetAddress.parse(recipients));
57              mess.setSubject(subject);
58              mess.setSentDate(new Date());
59              mess.setHeader("X-Mailer", "Maven Auto Build");
60              mess.setContent(message, "text/plain");
61              Transport.send(mess);
62          } catch (MessagingException me){
63              throw new MessengerException("Problem sending message", me);
64          }
65      }
66      
67  }
68  
69  
70  /*
71   *  $Log: EmailMessenger.java,v $
72   *  Revision 1.3  2004/03/19 13:02:25  jdt
73   *  Pruned the log messages - they cause conflicts on merge, 
74   *  best just to reduce them to the merge message.
75   *
76   *  Revision 1.2  2004/03/19 12:40:09  jdt
77   *  Merge from PLGN_JDT_bz199b.
78   *  Refactored log in pages to use xsp and xsl style sheets.  
79   *  Added pages for requesting a login, and requesting
80   *  a password reminder.
81   *
82   *
83   */