Guest User

Untitled

a guest
Jan 19th, 2017
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.39 KB | None | 0 0
  1. //
  2. // Decompiled by Procyon v0.5.30
  3. //
  4.  
  5. package com.tw.manage.business.services;
  6.  
  7. import java.util.Hashtable;
  8. import java.util.Iterator;
  9. import javax.mail.Transport;
  10. import javax.mail.MessagingException;
  11. import javax.mail.Address;
  12. import javax.mail.Message;
  13. import javax.mail.internet.InternetAddress;
  14. import javax.mail.internet.MimeMessage;
  15. import javax.mail.Authenticator;
  16. import javax.mail.Session;
  17. import java.util.Properties;
  18. import com.tw.manage.business.ManageException;
  19. import java.util.List;
  20. import org.apache.log4j.Logger;
  21.  
  22. public class MailService
  23. {
  24. private static Logger log;
  25.  
  26. public static void sendMessage(final List<String> toEmails, final List<String> ccEmails, final String subject, final String content) throws ManageException {
  27. final String fromEmail = PropertyService.getProperty("mailer.from");
  28. sendMessage(toEmails, ccEmails, fromEmail, subject, content);
  29. }
  30.  
  31. public static void sendMessage(final List<String> toEmails, final List<String> ccEmails, final String fromEmail, final String subject, final String content) throws ManageException {
  32. if (fromEmail == null || fromEmail.equalsIgnoreCase("")) {
  33. throw new ManageException("From email address is required.");
  34. }
  35. Transport t = null;
  36. final StringBuffer logMessage = new StringBuffer();
  37. try {
  38. final String host = PropertyService.getProperty("mailer.smtp.host");
  39. final String user = PropertyService.getProperty("mailer.user");
  40. final String password = PropertyService.getProperty("mailer.password");
  41. final String protocol = PropertyService.getProperty("mailer.protocol");
  42. final String port = PropertyService.getProperty("mailer.smtp.port");
  43. final String mimeType = PropertyService.getProperty("mailer.mime.type");
  44. final String subjectMimeType = PropertyService.getProperty("mailer.subject.mime.type");
  45. final Properties props = new Properties();
  46. ((Hashtable<String, String>)props).put("mail.smtp.host", host);
  47. ((Hashtable<String, String>)props).put("mail.smtp.port", port);
  48. ((Hashtable<String, String>)props).put("mail.smtp.starttls.enable", "true");
  49. ((Hashtable<String, String>)props).put("mail." + protocol + ".auth", "true");
  50. final Session session = Session.getDefaultInstance(props, (Authenticator)null);
  51. session.setDebug(false);
  52. final MimeMessage msg = new MimeMessage(session);
  53. logMessage.append("TO: ");
  54. for (final String toEmail : toEmails) {
  55. final InternetAddress toAddress = new InternetAddress(toEmail);
  56. msg.addRecipient(Message.RecipientType.TO, (Address)toAddress);
  57. logMessage.append(toEmail).append(",");
  58. }
  59. logMessage.append(" CC: ");
  60. if (ccEmails != null) {
  61. for (final String ccEmail : ccEmails) {
  62. final InternetAddress ccAddress = new InternetAddress(ccEmail);
  63. msg.addRecipient(Message.RecipientType.CC, (Address)ccAddress);
  64. logMessage.append(ccEmail).append(",");
  65. }
  66. }
  67. msg.setSubject(subject, subjectMimeType);
  68. logMessage.append(" FROM: ").append(fromEmail);
  69. logMessage.append(" SUBJECT: ").append(subject);
  70. msg.setContent((Object)content, mimeType);
  71. final InternetAddress addressFrom = new InternetAddress(fromEmail);
  72. msg.setFrom((Address)addressFrom);
  73. t = session.getTransport(protocol);
  74. t.connect(host, user, password);
  75. t.sendMessage((Message)msg, msg.getAllRecipients());
  76. MailService.log.info((Object)((Object)logMessage + " - SENT"));
  77. }
  78. catch (MessagingException e) {
  79. MailService.log.error((Object)((Object)logMessage + " - NOT SENT "), (Throwable)e);
  80. throw new ManageException("Cannot send email.", (Throwable)e);
  81. }
  82. finally {
  83. try {
  84. t.close();
  85. }
  86. catch (MessagingException e2) {
  87. MailService.log.error((Object)"Cannot close email transport.", (Throwable)e2);
  88. }
  89. }
  90. }
  91.  
  92. static {
  93. MailService.log = Logger.getLogger((Class)MailService.class);
  94. }
  95. }
Advertisement
Add Comment
Please, Sign In to add comment