commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From sgoes...@apache.org
Subject svn commit: r944001 - in /commons/proper/email/trunk/src: changes/ java/org/apache/commons/mail/ test/org/apache/commons/mail/
Date Thu, 13 May 2010 20:12:27 GMT
Author: sgoeschl
Date: Thu May 13 20:12:27 2010
New Revision: 944001

URL: http://svn.apache.org/viewvc?rev=944001&view=rev
Log:
[EMAIL-95] Thowing an IllegalStateException is buildMimeMessage is invoked more than once.

Modified:
    commons/proper/email/trunk/src/changes/changes.xml
    commons/proper/email/trunk/src/java/org/apache/commons/mail/Email.java
    commons/proper/email/trunk/src/java/org/apache/commons/mail/HtmlEmail.java
    commons/proper/email/trunk/src/java/org/apache/commons/mail/MultiPartEmail.java
    commons/proper/email/trunk/src/test/org/apache/commons/mail/HtmlEmailTest.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=944001&r1=944000&r2=944001&view=diff
==============================================================================
--- commons/proper/email/trunk/src/changes/changes.xml (original)
+++ commons/proper/email/trunk/src/changes/changes.xml Thu May 13 20:12:27 2010
@@ -23,7 +23,13 @@
 
   <body>
     <release version="1.3-SNAPSHOT" date="as in SVN">
-      <action dev="sgoeschl" type="add" issue="EMAIL-91" date="2010-05-13" due-to="Kevin
Lester">
+      <action dev="sgoeschl" type="fix" issue="EMAIL-95" date="2010-05-13">
+       Calling buildMimeMessage() before invoking send() caused
+       duplicated mime parts for HtmlEmail. The implementation now enforces
+       that an email can be only used once and throw an exception when
+       multiple invocations of buildMimeMessage() are detected.
+      </action>
+      <action dev="sgoeschl" type="fix" issue="EMAIL-91" date="2010-05-13" due-to="Kevin
Lester">
         Incorrect SMTP Port number shown in error message when an email fails
         to send due to a blocked port and SSL is used.
       </action>        

Modified: commons/proper/email/trunk/src/java/org/apache/commons/mail/Email.java
URL: http://svn.apache.org/viewvc/commons/proper/email/trunk/src/java/org/apache/commons/mail/Email.java?rev=944001&r1=944000&r2=944001&view=diff
==============================================================================
--- commons/proper/email/trunk/src/java/org/apache/commons/mail/Email.java (original)
+++ commons/proper/email/trunk/src/java/org/apache/commons/mail/Email.java Thu May 13 20:12:27
2010
@@ -1084,13 +1084,23 @@ public abstract class Email
     public abstract Email setMsg(String msg) throws EmailException;
 
     /**
-     * Build the internal MimeMessage to be sent.
+     * Does the work of actually building the MimeMessage. Please note that
+     * a user rarely calls this method directly and only if he/she is
+     * interested in the sending the underlying MimeMessage without
+     * commons-email.
      *
-     * @throws EmailException if there was an error.
+     * @exception EmailException if there was an error.
      * @since 1.0
      */
     public void buildMimeMessage() throws EmailException
     {
+        if(this.message != null)
+        {
+            // EMAIL-95 we assume that an email is not reused therefore invoking
+            // buildMimeMessage() more than once is illegal.
+            throw new IllegalStateException("The MimeMessage is already built.");
+        }
+        
         try
         {
             this.getMailSession();
@@ -1325,14 +1335,14 @@ public abstract class Email
      */
     public String getHostName()
     {
-        if (this.session != null && EmailUtils.isNotEmpty(this.session.getProperty(MAIL_HOST)))
-        {
-            return this.session.getProperty(MAIL_HOST);   
-        } 
-        else if (EmailUtils.isNotEmpty(this.hostName))
+        if (EmailUtils.isNotEmpty(this.hostName))
         {
             return this.hostName;
         }
+        else if (this.session != null)
+        {
+            return this.session.getProperty(MAIL_HOST);
+        }
         return null;
     }
 
@@ -1343,14 +1353,14 @@ public abstract class Email
      */
     public String getSmtpPort()
     {
-        if (this.session != null && EmailUtils.isNotEmpty(this.session.getProperty(MAIL_PORT)))
-        {
-            return this.session.getProperty(MAIL_PORT);
-        } 
-        else if (EmailUtils.isNotEmpty(this.smtpPort))
+        if (EmailUtils.isNotEmpty(this.smtpPort))
         {
             return this.smtpPort;
         }
+        else if (this.session != null)
+        {
+            return this.session.getProperty(MAIL_PORT);
+        }
         return null;
     }
 
@@ -1427,14 +1437,14 @@ public abstract class Email
      */
     public String getSslSmtpPort()
     {
-        if (this.session != null && EmailUtils.isNotEmpty(this.session.getProperty(MAIL_SMTP_SOCKET_FACTORY_PORT)))
-        {
-            return this.session.getProperty(MAIL_SMTP_SOCKET_FACTORY_PORT);
-        }
         if (EmailUtils.isNotEmpty(this.sslSmtpPort))
         {
             return this.sslSmtpPort;
         }
+        else if (this.session != null)
+        {
+            return this.session.getProperty(MAIL_SMTP_SOCKET_FACTORY_PORT);
+        }
         return null;
     }
 

Modified: commons/proper/email/trunk/src/java/org/apache/commons/mail/HtmlEmail.java
URL: http://svn.apache.org/viewvc/commons/proper/email/trunk/src/java/org/apache/commons/mail/HtmlEmail.java?rev=944001&r1=944000&r2=944001&view=diff
==============================================================================
--- commons/proper/email/trunk/src/java/org/apache/commons/mail/HtmlEmail.java (original)
+++ commons/proper/email/trunk/src/java/org/apache/commons/mail/HtmlEmail.java Thu May 13
20:12:27 2010
@@ -495,11 +495,14 @@ public class HtmlEmail extends MultiPart
     }
 
     /**
-     * Does the work of actually building the email.
+     * Does the work of actually building the MimeMessage. Please note that
+     * a user rarely calls this method directly and only if he/she is
+     * interested in the sending the underlying MimeMessage without
+     * commons-email.
      *
      * @exception EmailException if there was an error.
      * @since 1.0
-     */
+     */    
     public void buildMimeMessage() throws EmailException
     {
         try

Modified: commons/proper/email/trunk/src/java/org/apache/commons/mail/MultiPartEmail.java
URL: http://svn.apache.org/viewvc/commons/proper/email/trunk/src/java/org/apache/commons/mail/MultiPartEmail.java?rev=944001&r1=944000&r2=944001&view=diff
==============================================================================
--- commons/proper/email/trunk/src/java/org/apache/commons/mail/MultiPartEmail.java (original)
+++ commons/proper/email/trunk/src/java/org/apache/commons/mail/MultiPartEmail.java Thu May
13 20:12:27 2010
@@ -216,12 +216,14 @@ public class MultiPartEmail extends Emai
     }
 
     /**
-     * Builds the actual MimeMessage
+     * Does the work of actually building the MimeMessage. Please note that
+     * a user rarely calls this method directly and only if he/she is
+     * interested in the sending the underlying MimeMessage without
+     * commons-email.
      *
-     * @throws EmailException see javax.mail.internet.MimeBodyPart
-     *  for definitions
+     * @exception EmailException if there was an error.
      * @since 1.0
-     */
+     */    
     public void buildMimeMessage() throws EmailException
     {
         try

Modified: commons/proper/email/trunk/src/test/org/apache/commons/mail/HtmlEmailTest.java
URL: http://svn.apache.org/viewvc/commons/proper/email/trunk/src/test/org/apache/commons/mail/HtmlEmailTest.java?rev=944001&r1=944000&r2=944001&view=diff
==============================================================================
--- commons/proper/email/trunk/src/test/org/apache/commons/mail/HtmlEmailTest.java (original)
+++ commons/proper/email/trunk/src/test/org/apache/commons/mail/HtmlEmailTest.java Thu May
13 20:12:27 2010
@@ -554,4 +554,47 @@ public class HtmlEmailTest extends BaseE
         assertTrue(this.email.getHtmlMsg().contains("3DTZC268X93337.zip"));
         assertFalse(this.email.getHtmlMsg().contains("3DTZC268X93337..zip"));
     }
+
+    /**
+     * According to EMAIL-95 calling buildMimeMessage() before calling send()
+     * causes duplicate mime parts - now we throw an exception
+     *
+     * @throws Exception
+     */
+    public void testCallingBuildMimeMessageBeforeSent() throws Exception {
+
+        String htmlMsg = "<b>Hello World</b>";
+
+        this.email = new MockHtmlEmailConcrete();
+        this.email.setHostName(this.strTestMailServer);
+        this.email.setSmtpPort(this.getMailServerPort());
+        this.email.setFrom(this.strTestMailFrom);
+        this.email.addTo(this.strTestMailTo);
+        this.email.setCharset(Email.ISO_8859_1);
+
+        if (this.strTestUser != null && this.strTestPasswd != null)
+        {
+            this.email.setAuthentication(
+                this.strTestUser,
+                this.strTestPasswd);
+        }
+
+        String strSubject = "testCallingBuildMimeMessageBeforeSent";
+        this.email.setSubject(strSubject);
+        this.email.setHtmlMsg(htmlMsg);
+
+        // this should NOT be called when sending a message
+        this.email.buildMimeMessage();
+
+        try
+        {
+            this.email.send();
+        }
+        catch(IllegalStateException e)
+        {
+            return;
+        }
+
+        fail("Expecting an exception when calling buildMimeMessage() before send() ...");
+    }
 }



Mime
View raw message