001 package org.hackystat.sensorbase.mailer; 002 003 import java.util.Date; 004 import java.util.Properties; 005 import java.util.logging.LogManager; 006 import java.util.logging.Logger; 007 008 import javax.mail.Message; 009 import javax.mail.MessagingException; 010 import javax.mail.Session; 011 import javax.mail.Transport; 012 import javax.mail.internet.InternetAddress; 013 import javax.mail.internet.MimeMessage; 014 015 import static org.hackystat.sensorbase.server.ServerProperties.SMTP_HOST_KEY; 016 import static org.hackystat.sensorbase.server.ServerProperties.ADMIN_EMAIL_KEY; 017 import static org.hackystat.sensorbase.server.ServerProperties.TEST_INSTALL_KEY; 018 import static org.hackystat.sensorbase.server.ServerProperties.TEST_DOMAIN_KEY; 019 020 /** 021 * Provides a wrapper for SensorBase email services. Use the singleton instance to send emails: 022 * Mailer.getInstance().send(). To aid in testing, no emails are sent if the Hackystat server 023 * is a test installation, or the user email is in hackystat test domain. 024 * 025 * @author Philip M. Johnson 026 */ 027 public class Mailer { 028 029 /** Administrator's email. */ 030 private String adminEmail; 031 032 /** If this SensorBase exists purely for testing, should be 'true' or 'false'. */ 033 private String testInstall; 034 035 /** The domain for test users, such as "hackystat.org". */ 036 private String testDomain; 037 038 /** Session object. */ 039 private Session session; 040 041 /** Mailer object. */ 042 private static Mailer mailer; 043 044 /** The singleton instance maintaining an Email session. */ 045 private Mailer() { 046 Properties props = new Properties(); 047 props.put("mail.smtp.host", System.getProperty(SMTP_HOST_KEY)); 048 this.session = Session.getInstance(props); 049 this.adminEmail = "Hackystat Admin <" + System.getProperty(ADMIN_EMAIL_KEY) + ">"; 050 this.testInstall = System.getProperty(TEST_INSTALL_KEY).toLowerCase(); 051 this.testDomain = System.getProperty(TEST_DOMAIN_KEY); 052 } 053 054 /** 055 * Returns the singleton instance of Mailer, creating it if necesssary. 056 * 057 * @return The singleton Mailer instance. 058 */ 059 public static Mailer getInstance() { 060 if (mailer == null) { 061 Mailer.mailer = new Mailer(); 062 } 063 return Mailer.mailer; 064 } 065 066 067 /** 068 * Attempts to send an email. To aid in testing, no emails are sent if the Hackystat server 069 * is a test installation, or the user email is in hackystat test domain. 070 * Returns false if the send fails. 071 * 072 * @param fromAddr The email address from 073 * @param toAddr The email address to send to. 074 * @param subject The subject of the email. 075 * @param body The email body. 076 * @return True if no error occurred during send, false otherwise 077 */ 078 public boolean send(String fromAddr, String toAddr, String subject, String body) { 079 if (this.testInstall.equals("true") || toAddr.endsWith(this.testDomain)) { 080 return true; 081 } 082 try { 083 Message msg = new MimeMessage(this.session); 084 InternetAddress adminAddress = new InternetAddress(fromAddr); 085 InternetAddress[] adminAddressArray = {adminAddress}; 086 InternetAddress userAddress = new InternetAddress(toAddr); 087 msg.setFrom(adminAddress); 088 msg.setReplyTo(adminAddressArray); 089 msg.setRecipient(Message.RecipientType.TO, userAddress); 090 msg.setSubject(subject); 091 msg.setSentDate(new Date()); 092 msg.setText(body); 093 Transport.send(msg); 094 return true; 095 } 096 catch (MessagingException mex) { 097 Logger logger = LogManager.getLogManager().getLogger("org.hackystat.sensorbase"); 098 logger.warning("Mail failure to: " + toAddr + "\n" + mex); 099 return false; 100 } 101 102 } 103 104 /** 105 * Attempts to send an email. To aid in testing, no emails are sent if the Hackystat server 106 * is a test installation, or the user email is in hackystat test domain. 107 * Returns false if the send fails. 108 * 109 * @param toAddr The email address to send to. 110 * @param subject The subject of the email. 111 * @param body The email body. 112 * @return True if no error occurred during send, false otherwise 113 */ 114 public boolean send(String toAddr, String subject, String body) { 115 return this.send(this.adminEmail, toAddr, subject, body); 116 } 117 } 118