1 18 19 package cowsultants.itracker.ejb.client.util; 20 21 import java.io.*; 22 import java.rmi.*; 23 import java.util.*; 24 import javax.ejb.*; 25 import javax.naming.*; 26 import javax.mail.*; 27 import javax.mail.internet.*; 28 import javax.rmi.*; 29 30 import cowsultants.itracker.ejb.util.*; 31 import cowsultants.itracker.ejb.client.interfaces.*; 32 33 public class EmailHandler { 34 public static final String DEFAULT_FROM_ADDRESS = "itracker@localhost"; 35 public static final String DEFAULT_REPLYTO_ADDRESS = "itracker@localhost"; 36 public static final String DEFAULT_SMTP_HOST = "localhost"; 37 public static final String DEFAULT_SMTP_USERID = null; 38 public static final String DEFAULT_SMTP_PASSWORD = null; 39 public static final String DEFAULT_SMTP_CHARSET = "ISO-8859-1"; 40 41 private static String fromAddress = ""; 42 private static String replyToAddress = ""; 43 private static String smtpHost = ""; 44 private static String smtpUserid = null; 45 private static String smtpPassword = null; 46 private static String smtpCharset = ""; 47 48 static { 49 try { 50 InitialContext ic = new InitialContext(); 51 Object scRef = ic.lookup("java:comp/env/" + SystemConfiguration.JNDI_NAME); 52 SystemConfigurationHome scHome = (SystemConfigurationHome) PortableRemoteObject.narrow(scRef, SystemConfigurationHome.class); 53 SystemConfiguration sc = scHome.create(); 54 55 fromAddress = sc.getProperty("notification_from_address", DEFAULT_FROM_ADDRESS); 56 replyToAddress = sc.getProperty("notification_replyto_address", DEFAULT_REPLYTO_ADDRESS); 57 smtpHost = sc.getProperty("notification_smtp_host", DEFAULT_SMTP_HOST); 58 smtpUserid = sc.getProperty("notification_smtp_userid", null); 59 smtpPassword = sc.getProperty("notification_smtp_password", null); 60 smtpCharset = sc.getProperty("notification_smtp_charset", DEFAULT_SMTP_CHARSET); 61 62 if("".equals(smtpUserid) || "".equals(smtpPassword)) { 63 smtpUserid = null; 64 smtpPassword = null; 65 } 66 67 if(Logger.isLoggingDebug()) { 68 Logger.logDebug("Notification Init: From Address set to: " + fromAddress); 69 Logger.logDebug("Notification Init: ReplyTo Address set to: " + replyToAddress); 70 Logger.logDebug("Notification Init: SMTP server set to: " + smtpHost); 71 Logger.logDebug("Notification Init: SMTP userid set to: " + smtpUserid); 72 Logger.logDebug("Notification Init: SMTP password set to: " + smtpPassword); 73 } 74 } catch(CreateException ce) { 75 throw new EJBException(ce); 76 } catch(NamingException ne) { 77 throw new EJBException(ne); 78 } 79 } 80 81 public EmailHandler() { 82 } 83 84 public static void sendEmail(String address, String subject, String msgText) { 85 try { 86 InternetAddress[] recipients = new InternetAddress[1]; 87 recipients[0] = new InternetAddress(address); 88 89 sendEmail(recipients, subject, msgText); 90 } catch(AddressException ae) { 91 Logger.logDebug("AddressException while sending email.", ae); 92 } 93 } 94 95 public static void sendEmail(HashSet addresses, String subject, String msgText) { 96 int i = 0; 97 98 try { 99 InternetAddress[] recipients = new InternetAddress[addresses.size()]; 100 for(Iterator iterator = addresses.iterator(); iterator.hasNext(); i++) { 101 recipients[i] = new InternetAddress((String ) iterator.next()); 102 } 103 104 sendEmail(recipients, subject, msgText); 105 } catch(AddressException ae) { 106 Logger.logDebug("AddressException while sending email.", ae); 107 } 108 } 109 110 public static void sendEmail(InternetAddress[] addresses, String subject, String msgText) { 111 try { 112 Authenticator smtpAuth = null; 113 Properties props = new Properties(); 114 props.put("mail.smtp.host", smtpHost); 115 props.put("mail.mime.charset", smtpCharset); 116 117 if(smtpUserid != null && smtpPassword != null) { 118 smtpAuth = (Authenticator) new EmailAuthenticator(new PasswordAuthentication(smtpUserid, smtpPassword)); 119 } 120 121 MimeMessage msg = new MimeMessage(javax.mail.Session.getInstance(props, smtpAuth)); 122 123 msg.setFrom(new InternetAddress(fromAddress, "ITracker Notification System")); 124 msg.setReplyTo(new InternetAddress[] { new InternetAddress(replyToAddress, "ITracker Notification System") }); 125 msg.setRecipients(javax.mail.Message.RecipientType.TO, addresses); 126 msg.setSubject(subject, smtpCharset); 127 msg.setSentDate(new Date()); 128 129 msg.setHeader("Content-Transfer-Encoding", "quoted-printable"); 130 msg.setContent(msgText.toString(), "text/plain; charset=\"" + smtpCharset + "\""); 131 132 Transport.send(msg); 133 } catch(AddressException ae) { 134 Logger.logDebug("AddressException while sending email.", ae); 135 } catch(MessagingException me) { 136 Logger.logDebug("MessagingException while sending email.", me); 137 } catch(UnsupportedEncodingException uee) { 138 Logger.logDebug("UnsupportedEncodingException while sending email.", uee); 139 } 140 } 141 } | Popular Tags |