James Moger
2013-09-30 699e71e76b15081baf746c6ce9c9144f7e5f1ff9
src/main/java/com/gitblit/MailExecutor.java
@@ -41,9 +41,9 @@
/**
 * The mail executor handles sending email messages asynchronously from queue.
 *
 *
 * @author James Moger
 *
 *
 */
public class MailExecutor implements Runnable {
@@ -60,6 +60,7 @@
      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)) {
@@ -79,7 +80,7 @@
      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");
@@ -89,6 +90,7 @@
      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);
@@ -103,30 +105,17 @@
   /**
    * Indicates if the mail executor can send emails.
    *
    *
    * @return true if the mail executor is ready to send emails
    */
   public boolean isReady() {
      return session != null;
   }
   /**
    * Creates a message for the administrators.
    *
    * @returna message
    */
   public Message createMessageForAdministrators() {
      List<String> toAddresses = settings.getStrings(Keys.mail.adminAddresses);
      if (toAddresses.size() == 0) {
         logger.warn("Can not notify administrators because no email addresses are defined!");
         return null;
      }
      return createMessage(toAddresses);
   }
   /**
    * Create a message.
    *
    *
    * @param toAddresses
    * @return a message
    */
@@ -136,7 +125,7 @@
   /**
    * Create a message.
    *
    *
    * @param toAddresses
    * @return a message
    */
@@ -155,7 +144,7 @@
         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>();
@@ -169,7 +158,7 @@
               } catch (Throwable t) {
               }
            }
         }
         }
         message.setRecipients(Message.RecipientType.BCC,
               tos.toArray(new InternetAddress[tos.size()]));
         message.setSentDate(new Date());
@@ -181,7 +170,7 @@
   /**
    * Returns the status of the mail queue.
    *
    *
    * @return true, if the queue is empty
    */
   public boolean hasEmptyQueue() {
@@ -190,7 +179,7 @@
   /**
    * Queue's an email message to be sent.
    *
    *
    * @param message
    * @return true if the message was queued
    */
@@ -225,13 +214,13 @@
                  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);
   }