package cse308.swift.server.notifications; import java.util.ArrayList; import java.util.Properties; import javax.mail.Address; import javax.mail.MessagingException; import javax.mail.Session; import javax.mail.Transport; import javax.mail.internet.AddressException; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMessage; import javax.mail.internet.MimeMessage.RecipientType; import org.apache.log4j.Logger; import cse308.swift.shared.beans.ClientAccountData; public class SwiftServerNotifier { /** * Instance of the Log4j Logger */ static Logger logger = Logger.getLogger(SwiftServerNotifier.class.getName()); /** * Enum for the Service Providers, Provides the Suffixes for the Service providers * @author Ray */ public enum ServiceProviders { VERIZON { public String toString() { return "@vtext.com"; } }, SPRINT { public String toString() { return "@messaging.sprintpcs.com"; } }, ATT { public String toString() { return "@txt.att.net"; } }, TMOBILE { public String toString() { return "@tmomail.net"; } } } /** * Sends a Notification to the User based on the User's Preference * @param clientData The User's ClientAccountData * @param subject The Subject of the Message * @param notificationMessage The body of the Message */ public static void sendNotification(ClientAccountData clientData, String subject, String notificationMessage) { String host = "smtp.gmail.com"; String userid = "SwiftTradingServer"; String password = "cse308swift"; logger.info("Attempting to send Notification to " + clientData.getLastName() + ", " + clientData.getFirstName() + "\n\t Message: " + notificationMessage); if(clientData.isEmailAlerts() == false && clientData.isTextAlerts() == false) { logger.info("User Preference is set to NO Notifications!"); } else { try{ Properties props = System.getProperties(); props.put("mail.smtp.starttls.enable", "true"); props.put("mail.smtp.host", host); props.setProperty("mail.transport.protocol", "smtps"); props.put("mail.smtp.user", userid); props.put("mail.smtp.password", password); props.put("mail.smtp.port", "578"); props.put("mail.smtps.auth", "true"); Session session = Session.getDefaultInstance(props, null); MimeMessage message = new MimeMessage(session); try{ message.setFrom(new InternetAddress("SwiftTradingServer")); ArrayList toAddresses = new ArrayList(); if(clientData.isEmailAlerts()) { toAddresses.add(new InternetAddress(clientData.getEmailAddress())); } if(clientData.isTextAlerts()) { String cellNumber = clientData.getCellularPhoneNumber().replace("-", ""); cellNumber = clientData.getCellularPhoneNumber().replace("(", ""); cellNumber = clientData.getCellularPhoneNumber().replace(")", ""); cellNumber = clientData.getCellularPhoneNumber().replace(" ", ""); if(clientData.getCellularProvider().equalsIgnoreCase("VERIZON")){ toAddresses.add(new InternetAddress(cellNumber + ServiceProviders.VERIZON)); } else if(clientData.getCellularProvider().equalsIgnoreCase("ATT")){ toAddresses.add(new InternetAddress(cellNumber + ServiceProviders.ATT)); } else if(clientData.getCellularProvider().equalsIgnoreCase("TMOBILE")){ toAddresses.add(new InternetAddress(cellNumber + ServiceProviders.TMOBILE)); } else if(clientData.getCellularProvider().equalsIgnoreCase("SPRINT")){ toAddresses.add(new InternetAddress(cellNumber + ServiceProviders.SPRINT)); } } if(toAddresses.size() == 1) { message.addRecipient(RecipientType.TO, toAddresses.get(0)); } else if(toAddresses.size() > 1) { String emailAddresses = toAddresses.get(0).getAddress() + ", " + toAddresses.get(1).getAddress(); message.addRecipients(RecipientType.TO, emailAddresses); } message.setSubject(subject); message.setText(notificationMessage); Transport transport = session.getTransport("smtps"); transport.connect(host, userid, password); transport.sendMessage(message, message.getAllRecipients()); logger.info("Successfully Sent Noftification to " + clientData.getLastName() + ", " + clientData.getFirstName() + "\n\t Message: " + notificationMessage); transport.close(); } catch (AddressException ae) { logger.warn("Could Not Use Email Address for User: " + clientData.getLastName() + ", " + clientData.getFirstName()); } } catch (MessagingException e) { logger.warn("Could Not Email Notification to User: " + clientData.getLastName() + ", " + clientData.getFirstName() + "\n \t Message: " + notificationMessage); } } } }