Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.xxx.xxx.server.mailjob;
- import java.util.Date;
- import java.util.Properties;
- import javax.mail.Authenticator;
- import javax.mail.Message;
- import javax.mail.MessagingException;
- import javax.mail.PasswordAuthentication;
- import javax.mail.Session;
- import javax.mail.Transport;
- import javax.mail.internet.AddressException;
- import javax.mail.internet.InternetAddress;
- import javax.mail.internet.MimeMessage;
- import org.apache.log4j.Logger;
- import com.xxx.xxx.server.helper.Helper;
- public class MailJob implements Runnable {
- private Logger LOG = Logger.getLogger(MailJob.class);
- private final String MAIL_HOST = "smtp.xxx.com";
- private final String MAIL_PORT = "465";
- private final String MAIL_USER = "mailrobot@xxxx.com";
- private final String MAIL_PASS = "xxxxxxx";
- private final String MAIL_SENDER = "some@email.com";
- private String email;
- private String debugEmail;
- private String subject;
- private String message;
- public MailJob(String email, String debugEmail, String subject, String message) {
- this.email = email;
- this.debugEmail = debugEmail;
- this.subject = subject;
- this.message = message;
- }
- public void run() {
- long threadId = Thread.currentThread().getId();
- LOG.debug("Starting new MailJob (ID: " + threadId + ") ...");
- try {
- sendEmail(email, debugEmail, subject, message);
- } catch (Exception e) {
- LOG.error("There was an error while sending e-mail", e);
- e.printStackTrace();
- } finally {
- LOG.debug("MailJob (ID: " + threadId + ") terminated");
- }
- LOG.debug("Thread ending");
- }
- public void sendEmail(String email, String emailDebug, String subject, String message) throws MessagingException, AddressException {
- String to = email;
- String from = MAIL_SENDER;
- String host = MAIL_HOST;
- Properties props = System.getProperties();
- props.setProperty("mail.smtp.host", host);
- props.put("mail.transport.protocol", "smtp");
- props.put("mail.smtp.auth", "true");// If you need to authenticate
- if ("465".equals(MAIL_PORT)) {
- // STARTTLS
- props.put("mail.smtp.starttls.enable", "true");
- // Use the following if you need SSL
- props.put("mail.smtp.socketFactory.port", MAIL_PORT);
- props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
- props.put("mail.smtp.socketFactory.fallback", "false");
- } else {
- props.put("mail.smtp.port", MAIL_PORT);
- }
- Authenticator auth = new SMTPAuthenticator(MAIL_USER, MAIL_PASS);
- LOG.debug("Get Instance...");
- Session session = Session.getInstance(props, auth);
- LOG.debug("Got instance...");
- Message msg = new MimeMessage(session);
- msg.setFrom(new InternetAddress(from));
- msg.setSubject(subject);
- msg.setRecipient(Message.RecipientType.TO, new InternetAddress(to));
- if (!Helper.isBlank(emailDebug)) {
- LOG.debug("Add MAIL_DEBUG_RECEIVER_EMAIL to BCC " + emailDebug);
- msg.addRecipient(Message.RecipientType.BCC, new InternetAddress(emailDebug));
- }
- msg.setContent(message, "text/html; charset=utf-8");
- msg.setSentDate(new Date());
- LOG.info("Start sending!");
- Transport.send(msg);
- LOG.info("Email successfully sent!");
- }
- private class SMTPAuthenticator extends Authenticator {
- private PasswordAuthentication authentication;
- public SMTPAuthenticator(String login, String password) {
- authentication = new PasswordAuthentication(login, password);
- }
- @Override
- protected PasswordAuthentication getPasswordAuthentication() {
- return authentication;
- }
- }
- }
- // within the JUnit thest, I tried the following
- Thread mailJob = new Thread(new MailJob("my@mail.com", null, "test", "cool message"));
- ClassLoader c = Thread.currentThread().getContextClassLoader();
- mailJob.setContextClassLoader(c);
- mailJob.setName("MailJob");
- mailJob.start();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement