| | |
| | |
|
| | | /**
|
| | | * The mail executor handles sending email messages asynchronously from queue.
|
| | | * |
| | | *
|
| | | * @author James Moger
|
| | | * |
| | | *
|
| | | */
|
| | | public class MailExecutor implements Runnable {
|
| | |
|
| | |
| | |
|
| | | final String mailUser = settings.getString(Keys.mail.username, null);
|
| | | final String mailPassword = settings.getString(Keys.mail.password, null);
|
| | | final boolean smtps = settings.getBoolean(Keys.mail.smtps, false);
|
| | | boolean authenticate = !StringUtils.isEmpty(mailUser) && !StringUtils.isEmpty(mailPassword);
|
| | | String server = settings.getString(Keys.mail.server, "");
|
| | | if (StringUtils.isEmpty(server)) {
|
| | |
| | | props.setProperty("mail.smtp.auth", String.valueOf(authenticate));
|
| | | props.setProperty("mail.smtp.auths", String.valueOf(authenticate));
|
| | |
|
| | | if (isGMail) {
|
| | | if (isGMail || smtps) {
|
| | | props.setProperty("mail.smtp.starttls.enable", "true");
|
| | | props.put("mail.smtp.socketFactory.port", String.valueOf(port));
|
| | | props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
|
| | |
| | | if (!StringUtils.isEmpty(mailUser) && !StringUtils.isEmpty(mailPassword)) {
|
| | | // SMTP requires authentication
|
| | | session = Session.getInstance(props, new Authenticator() {
|
| | | @Override
|
| | | protected PasswordAuthentication getPasswordAuthentication() {
|
| | | PasswordAuthentication passwordAuthentication = new PasswordAuthentication(
|
| | | mailUser, mailPassword);
|
| | |
| | |
|
| | | /**
|
| | | * Indicates if the mail executor can send emails.
|
| | | * |
| | | *
|
| | | * @return true if the mail executor is ready to send emails
|
| | | */
|
| | | public boolean isReady() {
|
| | |
| | |
|
| | | /**
|
| | | * Create a message.
|
| | | * |
| | | *
|
| | | * @param toAddresses
|
| | | * @return a message
|
| | | */
|
| | |
| | |
|
| | | /**
|
| | | * Create a message.
|
| | | * |
| | | *
|
| | | * @param toAddresses
|
| | | * @return a message
|
| | | */
|
| | |
| | | for (String address : toAddresses) {
|
| | | uniques.add(address.toLowerCase());
|
| | | }
|
| | | |
| | |
|
| | | Pattern validEmail = Pattern
|
| | | .compile("^([a-zA-Z0-9_\\-\\.]+)@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.)|(([a-zA-Z0-9\\-]+\\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\\]?)$");
|
| | | List<InternetAddress> tos = new ArrayList<InternetAddress>();
|
| | |
| | | } catch (Throwable t) {
|
| | | }
|
| | | }
|
| | | } |
| | | }
|
| | | message.setRecipients(Message.RecipientType.BCC,
|
| | | tos.toArray(new InternetAddress[tos.size()]));
|
| | | message.setSentDate(new Date());
|
| | |
| | |
|
| | | /**
|
| | | * Returns the status of the mail queue.
|
| | | * |
| | | *
|
| | | * @return true, if the queue is empty
|
| | | */
|
| | | public boolean hasEmptyQueue() {
|
| | |
| | |
|
| | | /**
|
| | | * Queue's an email message to be sent.
|
| | | * |
| | | *
|
| | | * @param message
|
| | | * @return true if the message was queued
|
| | | */
|
| | |
| | | failures.add(message);
|
| | | }
|
| | | }
|
| | | |
| | |
|
| | | // push the failures back onto the queue for the next cycle
|
| | | queue.addAll(failures);
|
| | | }
|
| | | }
|
| | | }
|
| | | |
| | |
|
| | | public void sendNow(Message message) throws Exception {
|
| | | Transport.send(message);
|
| | | }
|