View Javadoc

1   /* $Id: EmailMessengerFactoryImpl.java,v 1.1 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.Properties;
9   
10  import javax.mail.Authenticator;
11  import javax.mail.PasswordAuthentication;
12  import javax.mail.Session;
13  /***
14   * Messenger that dispatches emails
15   * 
16   * @author jdt
17   */
18  public final class EmailMessengerFactoryImpl implements EmailMessengerFactory  {
19      /***
20       * The mail session
21       */
22      private Session session;
23  
24      /***
25       * Constructor
26       * @param smtpServer IP address of server, port 25 assumed
27       * @param user username of sender's account
28       * @param password password for said account
29       * @param returnAddress who shall we say it's from?
30       */
31      public EmailMessengerFactoryImpl(final String smtpServer, final String user, final String password,final String returnAddress) {
32          final Properties props = new Properties();
33          props.setProperty("mail.transport.protocol", "smtp");
34          props.setProperty("mail.host", smtpServer);
35          props.setProperty("mail.user", user);
36          props.setProperty("mail.password", password);
37          props.setProperty("mail.from", returnAddress);
38          //final MyAuthenticator authenticator =new MyAuthenticator(user, password); 
39          session = Session.getInstance(props,null);
40  
41      }
42  
43      /***
44       * Create a messenger that can be used to message these recipients
45       * @param recipients comma separated list of recipients
46       * @return an EmailMessenger
47       */
48      public Messenger getEmailMessenger(final String recipients) {
49              return new EmailMessenger(session, recipients);
50      }
51      
52      /*** 
53       * Quick test
54       * 
55       * @param args server, user, password, return address, destinationAddress
56       * @throws MessengerException but hopefully not
57       */
58      public static void main(final String[] args) throws MessengerException {
59          if (args.length!=5) {
60              System.out.println("Usage: server user password returnAddress destination");
61              return;
62          }
63          final EmailMessengerFactoryImpl factory = new EmailMessengerFactoryImpl(args[0],args[1],args[2],args[3]);
64          final Messenger messenger = factory.getEmailMessenger(args[4]);
65          messenger.sendMessage("Test message 1", "Hello!");
66          messenger.sendMessage("Test message 2", "Hello again!");
67          final Messenger messenger2 = factory.getEmailMessenger(args[4]);
68          messenger2.sendMessage("Test message 3", "Goodbye!");
69      }
70      
71  }
72  
73  class MyAuthenticator extends Authenticator  {
74      
75      private PasswordAuthentication pa;
76      public MyAuthenticator(String user, String password) {
77          pa = new PasswordAuthentication(user, password);
78      }
79      protected PasswordAuthentication getPasswordAuthentication() {
80          System.out.println("authentication reqd");
81          return pa;
82      }
83  };
84  
85  
86  /*
87   *  $Log: EmailMessengerFactoryImpl.java,v $
88   *  Revision 1.1  2004/06/02 20:10:05  johndavidtaylor
89   *  Refactoring to allow email templates.
90   *
91   *  Revision 1.1  2004/03/18 15:21:54  johndavidtaylor
92   *  Copied across from incubation in beanpeeler
93   *
94   *  Revision 1.2  2004/03/15 13:40:13  johndavidtaylor
95   *  Now up the stage where it can be used.  Things still to do: 
96   *  change the way email.txt is picked up so it can be modified mid-flight,
97   *  remove the annoying logging.
98   *
99   *  Revision 1.1  2004/03/14 16:26:49  johndavidtaylor
100  *  refactored email
101  *
102  */