Advertisement
Guest User

Untitled

a guest
Oct 8th, 2015
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.21 KB | None | 0 0
  1. package com.xxx.xxx.server.mailjob;
  2.  
  3. import java.util.Date;
  4. import java.util.Properties;
  5.  
  6. import javax.mail.Authenticator;
  7. import javax.mail.Message;
  8. import javax.mail.MessagingException;
  9. import javax.mail.PasswordAuthentication;
  10. import javax.mail.Session;
  11. import javax.mail.Transport;
  12. import javax.mail.internet.AddressException;
  13. import javax.mail.internet.InternetAddress;
  14. import javax.mail.internet.MimeMessage;
  15.  
  16. import org.apache.log4j.Logger;
  17.  
  18. import com.xxx.xxx.server.helper.Helper;
  19.  
  20.  
  21. public class MailJob implements Runnable {
  22.     private Logger LOG = Logger.getLogger(MailJob.class);
  23.  
  24.     private final String MAIL_HOST = "smtp.xxx.com";
  25.     private final String MAIL_PORT = "465";
  26.     private final String MAIL_USER = "mailrobot@xxxx.com";
  27.     private final String MAIL_PASS = "xxxxxxx";
  28.     private final String MAIL_SENDER = "some@email.com";
  29.  
  30.     private String email;
  31.     private String debugEmail;
  32.     private String subject;
  33.     private String message;
  34.  
  35.     public MailJob(String email, String debugEmail, String subject, String message) {
  36.         this.email = email;
  37.         this.debugEmail = debugEmail;
  38.         this.subject = subject;
  39.         this.message = message;
  40.     }
  41.  
  42.     public void run() {
  43.         long threadId = Thread.currentThread().getId();
  44.         LOG.debug("Starting new MailJob (ID: " + threadId + ") ...");
  45.         try {
  46.             sendEmail(email, debugEmail, subject, message);
  47.         } catch (Exception e) {
  48.             LOG.error("There was an error while sending e-mail", e);
  49.             e.printStackTrace();
  50.         } finally {
  51.             LOG.debug("MailJob (ID: " + threadId + ") terminated");
  52.         }
  53.         LOG.debug("Thread ending");
  54.     }
  55.  
  56.     public void sendEmail(String email, String emailDebug, String subject, String message) throws MessagingException, AddressException {
  57.         String to = email;
  58.         String from = MAIL_SENDER;
  59.         String host = MAIL_HOST;
  60.  
  61.         Properties props = System.getProperties();
  62.         props.setProperty("mail.smtp.host", host);
  63.         props.put("mail.transport.protocol", "smtp");
  64.  
  65.         props.put("mail.smtp.auth", "true");// If you need to authenticate
  66.  
  67.         if ("465".equals(MAIL_PORT)) {
  68.             // STARTTLS
  69.             props.put("mail.smtp.starttls.enable", "true");
  70.  
  71.             // Use the following if you need SSL
  72.             props.put("mail.smtp.socketFactory.port", MAIL_PORT);
  73.             props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
  74.             props.put("mail.smtp.socketFactory.fallback", "false");
  75.         } else {
  76.             props.put("mail.smtp.port", MAIL_PORT);
  77.         }
  78.  
  79.         Authenticator auth = new SMTPAuthenticator(MAIL_USER, MAIL_PASS);
  80.         LOG.debug("Get Instance...");
  81.         Session session = Session.getInstance(props, auth);
  82.         LOG.debug("Got instance...");
  83.         Message msg = new MimeMessage(session);
  84.  
  85.         msg.setFrom(new InternetAddress(from));
  86.         msg.setSubject(subject);
  87.         msg.setRecipient(Message.RecipientType.TO, new InternetAddress(to));
  88.         if (!Helper.isBlank(emailDebug)) {
  89.             LOG.debug("Add MAIL_DEBUG_RECEIVER_EMAIL to BCC " + emailDebug);
  90.             msg.addRecipient(Message.RecipientType.BCC, new InternetAddress(emailDebug));
  91.         }
  92.  
  93.         msg.setContent(message, "text/html; charset=utf-8");
  94.  
  95.         msg.setSentDate(new Date());
  96.         LOG.info("Start sending!");
  97.         Transport.send(msg);
  98.  
  99.         LOG.info("Email successfully sent!");
  100.     }
  101.  
  102.     private class SMTPAuthenticator extends Authenticator {
  103.         private PasswordAuthentication authentication;
  104.  
  105.         public SMTPAuthenticator(String login, String password) {
  106.             authentication = new PasswordAuthentication(login, password);
  107.         }
  108.  
  109.         @Override
  110.         protected PasswordAuthentication getPasswordAuthentication() {
  111.             return authentication;
  112.         }
  113.     }
  114.  
  115. }
  116.  
  117.  
  118.  
  119.  
  120.  
  121. // within the JUnit thest, I tried the following
  122.  
  123. Thread mailJob = new Thread(new MailJob("my@mail.com", null, "test", "cool message"));
  124. ClassLoader c = Thread.currentThread().getContextClassLoader();
  125. mailJob.setContextClassLoader(c);
  126. mailJob.setName("MailJob");
  127. mailJob.start();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement