You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by sg...@apache.org on 2008/12/29 15:50:45 UTC

svn commit: r729905 - /commons/proper/email/trunk/src/java/org/apache/commons/mail/HtmlEmail.java

Author: sgoeschl
Date: Mon Dec 29 06:50:44 2008
New Revision: 729905

URL: http://svn.apache.org/viewvc?rev=729905&view=rev
Log:
Applied a patch to fix broken build() method - did some field testing (Apple Mail, Outlook WebAccess, Outlook, Thunderbird) and the result seems to be okay.

Modified:
    commons/proper/email/trunk/src/java/org/apache/commons/mail/HtmlEmail.java

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=729905&r1=729904&r2=729905&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 Mon Dec 29 06:50:44 2008
@@ -520,50 +520,50 @@
      */
     private void build() throws MessagingException, EmailException
     {
-        MimeMultipart container = this.getContainer();
-        MimeMultipart subContainer = null;
+        MimeMultipart rootContainer = this.getContainer();
+        MimeMultipart bodyEmbedsContainer = rootContainer;
+        MimeMultipart bodyContainer = rootContainer;
         BodyPart msgHtml = null;
         BodyPart msgText = null;
 
-        container.setSubType("related");
-        subContainer = new MimeMultipart("alternative");
-
-        if (EmailUtils.isNotEmpty(this.text))
-        {
-            msgText = new MimeBodyPart();
-            if (this.inlineEmbeds.size() > 0)
-            {
-                subContainer.addBodyPart(msgText);
-            }
-            else
-            {
-                container.addBodyPart(msgText);
-            }
-
-            // apply default charset if one has been set
-            if (EmailUtils.isNotEmpty(this.charset))
-            {
-                msgText.setContent(
-                    this.text,
-                    Email.TEXT_PLAIN + "; charset=" + this.charset);
-            }
-            else
-            {
-                msgText.setContent(this.text, Email.TEXT_PLAIN);
-            }
+        rootContainer.setSubType("mixed");
+        
+        // determine how to form multiparts of email
+        
+        if (EmailUtils.isNotEmpty(this.html) && this.inlineEmbeds.size()>0)
+        {
+            //If HTML body and embeds are used, create a related container and add it to the root container
+            bodyEmbedsContainer = new MimeMultipart("related");
+            bodyContainer = bodyEmbedsContainer;
+            this.addPart(bodyEmbedsContainer, 0);
+            
+            //If TEXT body was specified, create a alternative container and add it to the embeds container
+            if (EmailUtils.isNotEmpty(this.text))
+            {
+                bodyContainer = new MimeMultipart("alternative");
+                BodyPart bodyPart = createBodyPart();
+                try
+                {
+                    bodyPart.setContent(bodyContainer);
+                    bodyEmbedsContainer.addBodyPart(bodyPart, 0);
+                }
+                catch (MessagingException me)
+                {
+                    throw new EmailException(me);
+                }
+            }
+        }
+        else if (EmailUtils.isNotEmpty(this.text) && EmailUtils.isNotEmpty(this.html))
+        {
+            //If both HTML and TEXT bodies are provided, create a alternative container and add it to the root container
+            bodyContainer = new MimeMultipart("alternative");
+            this.addPart(bodyContainer, 0);
         }
 
         if (EmailUtils.isNotEmpty(this.html))
         {
             msgHtml = new MimeBodyPart();
-            if (this.inlineEmbeds.size() > 0)
-            {
-                subContainer.addBodyPart(msgHtml);
-            }
-            else
-            {
-                container.addBodyPart(msgHtml);
-            }
+            bodyContainer.addBodyPart(msgHtml, 0);
 
             // apply default charset if one has been set
             if (EmailUtils.isNotEmpty(this.charset))
@@ -581,14 +581,26 @@
             while (iter.hasNext())
             {
                 InlineImage ii = (InlineImage) iter.next();
-                container.addBodyPart(ii.getMbp());
+                bodyEmbedsContainer.addBodyPart(ii.getMbp());
             }
         }
 
-        if (this.inlineEmbeds.size() > 0)
+        if (EmailUtils.isNotEmpty(this.text))
         {
-            // add sub container to message
-            this.addPart(subContainer, 0);
+            msgText = new MimeBodyPart();
+            bodyContainer.addBodyPart(msgText, 0);
+
+            // apply default charset if one has been set
+            if (EmailUtils.isNotEmpty(this.charset))
+            {
+                msgText.setContent(
+                    this.text,
+                    Email.TEXT_PLAIN + "; charset=" + this.charset);
+            }
+            else
+            {
+                msgText.setContent(this.text, Email.TEXT_PLAIN);
+            }
         }
     }