You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by tn...@apache.org on 2013/10/10 23:14:34 UTC

svn commit: r1531110 - in /commons/proper/email/trunk/src: changes/changes.xml main/java/org/apache/commons/mail/Email.java main/java/org/apache/commons/mail/EmailConstants.java test/java/org/apache/commons/mail/EmailLiveTest.java

Author: tn
Date: Thu Oct 10 21:14:34 2013
New Revision: 1531110

URL: http://svn.apache.org/r1531110
Log:
[EMAIL-132] Added support for sending partial emails.

Modified:
    commons/proper/email/trunk/src/changes/changes.xml
    commons/proper/email/trunk/src/main/java/org/apache/commons/mail/Email.java
    commons/proper/email/trunk/src/main/java/org/apache/commons/mail/EmailConstants.java
    commons/proper/email/trunk/src/test/java/org/apache/commons/mail/EmailLiveTest.java

Modified: commons/proper/email/trunk/src/changes/changes.xml
URL: http://svn.apache.org/viewvc/commons/proper/email/trunk/src/changes/changes.xml?rev=1531110&r1=1531109&r2=1531110&view=diff
==============================================================================
--- commons/proper/email/trunk/src/changes/changes.xml (original)
+++ commons/proper/email/trunk/src/changes/changes.xml Thu Oct 10 21:14:34 2013
@@ -23,6 +23,9 @@
 
   <body>
     <release version="1.3.2" date="TBD">
+      <action dev="tn" type="add" issue="EMAIL-132" date="2013-10-10" due-to="Allen Xudong Cheng">
+        Added support for sending partial emails in case of invalid addresses.
+      </action>
       <action dev="tn" type="fix" issue="EMAIL-131" date="2013-09-14" due-to="Raju Y">
         The MimeMessageParser will now correctly parse MIME multi-parts of type "text/plain"
         and "text/html" with a content disposition header of "attachment". The parts will

Modified: commons/proper/email/trunk/src/main/java/org/apache/commons/mail/Email.java
URL: http://svn.apache.org/viewvc/commons/proper/email/trunk/src/main/java/org/apache/commons/mail/Email.java?rev=1531110&r1=1531109&r2=1531110&view=diff
==============================================================================
--- commons/proper/email/trunk/src/main/java/org/apache/commons/mail/Email.java (original)
+++ commons/proper/email/trunk/src/main/java/org/apache/commons/mail/Email.java Thu Oct 10 21:14:34 2013
@@ -29,6 +29,7 @@ import java.util.Properties;
 import javax.mail.Authenticator;
 import javax.mail.Message;
 import javax.mail.MessagingException;
+import javax.mail.SendFailedException;
 import javax.mail.Session;
 import javax.mail.Store;
 import javax.mail.Transport;
@@ -312,6 +313,15 @@ public abstract class Email
      */
     private boolean sslCheckServerIdentity;
 
+    /**
+     * If set to true, and a message has some valid and some invalid addresses, send the message anyway,
+     * reporting the partial failure with a SendFailedException.
+     * If set to false (the default), the message is not sent to any of the recipients
+     * if there is an invalid recipient address.
+     * Defaults to false.
+     */
+    private boolean sendPartial;
+
     /** The Session to mail with. */
     private Session session;
 
@@ -639,6 +649,11 @@ public abstract class Email
             properties.setProperty(EmailConstants.MAIL_TRANSPORT_STARTTLS_REQUIRED,
                     isStartTLSRequired() ? "true" : "false");
 
+            properties.setProperty(EmailConstants.MAIL_SMTP_SEND_PARTIAL,
+                    isSendPartial() ? "true" : "false");
+            properties.setProperty(EmailConstants.MAIL_SMTPS_SEND_PARTIAL,
+                    isSendPartial() ? "true" : "false");
+
             if (this.authenticator != null)
             {
                 properties.setProperty(MAIL_SMTP_AUTH, "true");
@@ -1641,6 +1656,7 @@ public abstract class Email
      */
     public Email setSSLCheckServerIdentity(boolean sslCheckServerIdentity)
     {
+        checkSessionAlreadyInitialized();
         this.sslCheckServerIdentity = sslCheckServerIdentity;
         return this;
     }
@@ -1676,6 +1692,37 @@ public abstract class Email
     }
 
     /**
+    * If partial sending of email enabled.
+    *
+    * @return true if sending partial email is enabled
+    * @since 1.3.2
+    */
+    public boolean isSendPartial()
+    {
+        return sendPartial;
+    }
+
+    /**
+     * Sets whether the email is partially send in case of invalid addresses.
+     * <p>
+     * In case the mail server rejects an address as invalid, the call to {@link #send()}
+     * may throw a {@link SendFailedException}, even if partial send mode is enabled (emails
+     * to valid addresses will be transmitted). In case the email server does not reject
+     * invalid addresses immediately, but return a bounce message, no exception will be thrown
+     * by the {@link #send()} method.
+     *
+     * @param sendPartial whether to enable partial send mode
+     * @return An Email.
+     * @since 1.3.2
+     */
+    public Email setSendPartial(boolean sendPartial)
+    {
+        checkSessionAlreadyInitialized();
+        this.sendPartial = sendPartial;
+        return this;
+    }
+
+    /**
      * Get the list of "To" addresses.
      *
      * @return List addresses

Modified: commons/proper/email/trunk/src/main/java/org/apache/commons/mail/EmailConstants.java
URL: http://svn.apache.org/viewvc/commons/proper/email/trunk/src/main/java/org/apache/commons/mail/EmailConstants.java?rev=1531110&r1=1531109&r2=1531110&view=diff
==============================================================================
--- commons/proper/email/trunk/src/main/java/org/apache/commons/mail/EmailConstants.java (original)
+++ commons/proper/email/trunk/src/main/java/org/apache/commons/mail/EmailConstants.java Thu Oct 10 21:14:34 2013
@@ -199,6 +199,28 @@ public final class EmailConstants
      */
     public static final String MAIL_SMTP_SSL_SOCKET_FACTORY_PORT = "mail.smtp.ssl.socketFactory.port";
 
+    /////////////////////////////////////////////////////////////////////////
+    // since 1.3.2
+    /////////////////////////////////////////////////////////////////////////
+
+    /**
+     * If set to true, and a message has some valid and some invalid addresses, send the message anyway,
+     * reporting the partial failure with a SendFailedException.
+     * If set to false (the default), the message is not sent to any of the recipients
+     * if there is an invalid recipient address.
+     * @since 1.3.2
+     */
+    public static final String MAIL_SMTP_SEND_PARTIAL = "mail.smtp.sendpartial";
+
+    /**
+     * If set to true, and a message has some valid and some invalid addresses, send the message anyway,
+     * reporting the partial failure with a SendFailedException.
+     * If set to false (the default), the message is not sent to any of the recipients
+     * if there is an invalid recipient address.
+     * @since 1.3.2
+     */
+    public static final String MAIL_SMTPS_SEND_PARTIAL = "mail.smtps.sendpartial";
+
     /** Hide constructor. */
     private EmailConstants()
     {

Modified: commons/proper/email/trunk/src/test/java/org/apache/commons/mail/EmailLiveTest.java
URL: http://svn.apache.org/viewvc/commons/proper/email/trunk/src/test/java/org/apache/commons/mail/EmailLiveTest.java?rev=1531110&r1=1531109&r2=1531110&view=diff
==============================================================================
--- commons/proper/email/trunk/src/test/java/org/apache/commons/mail/EmailLiveTest.java (original)
+++ commons/proper/email/trunk/src/test/java/org/apache/commons/mail/EmailLiveTest.java Thu Oct 10 21:14:34 2013
@@ -360,4 +360,26 @@ public class EmailLiveTest extends Abstr
             transport.close();
          }
     }
+    
+    /**
+     * Testing if we are able to send a partial email with an invalid address.
+     *
+     * https://issues.apache.org/jira/browse/EMAIL-132
+     *
+     * @throws Exception the test failed.
+     */
+    @Test
+    public void testPartialSend() throws Exception
+    {
+        SimpleEmail email = (SimpleEmail) create(SimpleEmail.class);
+        email.addTo("tn@apache.org");
+        email.addTo("asdkljfakld@kadjfka.com");
+        email.setSubject("TestPartialMail");
+        email.setMsg("This is a test mail ... :-)");
+
+        email.setSendPartial(true);
+
+        EmailUtils.writeMimeMessage( new File("./target/test-emails/partialmail.eml"), send(email).getMimeMessage());
+    }
+
 }