epugh       2004/11/14 10:07:43

  Modified:    email/src/java/org/apache/commons/mail MultiPartEmail.java
                        HtmlEmail.java
               email/src/test/org/apache/commons/mail/mocks
                        MockHtmlEmailConcrete.java
               email/src/test/org/apache/commons/mail
                        BaseEmailTestCase.java
  Added:       email/src/test/org/apache/commons/mail
                        SendWithAttachmentsTest.java
  Log:
  Bug 30973
  HTML email with plain text alternative and attachments
  
  Revision  Changes    Path
  1.12      +22 -2     
jakarta-commons-sandbox/email/src/java/org/apache/commons/mail/MultiPartEmail.java
  
  Index: MultiPartEmail.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-commons-sandbox/email/src/java/org/apache/commons/mail/MultiPartEmail.java,v
  retrieving revision 1.11
  retrieving revision 1.12
  diff -u -r1.11 -r1.12
  --- MultiPartEmail.java       1 Nov 2004 13:55:02 -0000       1.11
  +++ MultiPartEmail.java       14 Nov 2004 18:07:43 -0000      1.12
  @@ -60,6 +60,9 @@
       /** Indicates if the message has been initialized */
       private boolean initialized = false;
   
  +    /** Indicates if attachments have been added to the message */
  +    private boolean boolHasAttachments = false;
  +    
       /**
        * Set the MIME subtype of the email.
        * @param aSubType MIME subtype of the email
  @@ -363,7 +366,8 @@
           mbp.setFileName(name);
           mbp.setDescription(description);
           mbp.setDataHandler(new DataHandler(ds));
  -
  +        this.boolHasAttachments = true;
  +        
           return this;
       }
   
  @@ -405,6 +409,22 @@
               init();
           }
           return container;
  +    }
  +
  +    /**
  +     * @return boolHasAttachments
  +     */
  +    public boolean isBoolHasAttachments()
  +    {
  +        return boolHasAttachments;
  +    }
  +
  +    /**
  +     * @param b boolHasAttachments
  +     */
  +    public void setBoolHasAttachments(boolean b)
  +    {
  +        boolHasAttachments = b;
       }
   
   }
  
  
  
  1.16      +124 -19   
jakarta-commons-sandbox/email/src/java/org/apache/commons/mail/HtmlEmail.java
  
  Index: HtmlEmail.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-commons-sandbox/email/src/java/org/apache/commons/mail/HtmlEmail.java,v
  retrieving revision 1.15
  retrieving revision 1.16
  diff -u -r1.15 -r1.16
  --- HtmlEmail.java    2 Nov 2004 11:13:26 -0000       1.15
  +++ HtmlEmail.java    14 Nov 2004 18:07:43 -0000      1.16
  @@ -199,15 +199,64 @@
        */
       public void send() throws MessagingException
       {
  -        MimeMultipart container = this.getContainer();
  -        container.setSubType("related");
  +        // if the email has attachments then the base type is mixed, 
  +        // otherwise it should be related
  +        if (this.isBoolHasAttachments())
  +        {
  +            this.buildAttachments();
  +        }
  +        else
  +        {
  +            this.buildNoAttachments();
  +        }
   
  -        BodyPart msgText = null;
  +        super.send();
  +    }
  +
  +    /**
  +     * @throws MessagingException MessagingException
  +     */
  +    private void buildAttachments() throws MessagingException
  +    {
  +        MimeMultipart container = this.getContainer();
  +        MimeMultipart subContainer = null;
  +             MimeMultipart subContainerHTML = new MimeMultipart("related");
           BodyPart msgHtml = null;
  +        BodyPart msgText = null;
  +
  +        container.setSubType("mixed");
  +        subContainer = new MimeMultipart("alternative");
  +
  +        if (StringUtils.isNotEmpty(this.text))
  +        {
  +            msgText = new MimeBodyPart();
  +            subContainer.addBodyPart(msgText);
  +
  +            if (StringUtils.isNotEmpty(this.charset))
  +            {
  +                msgText.setContent(
  +                    this.text,
  +                    Email.TEXT_PLAIN + "; charset=" + this.charset);
  +            }
  +            else
  +            {
  +                msgText.setContent(this.text, Email.TEXT_PLAIN);
  +            }
  +        }
   
           if (StringUtils.isNotEmpty(this.html))
           {
  -            msgHtml = this.getPrimaryBodyPart();
  +                     if (this.inlineImages.size() > 0)
  +                     {
  +                             msgHtml = new MimeBodyPart();
  +                             subContainerHTML.addBodyPart(msgHtml);
  +                     }
  +                     else
  +                     {
  +                             msgHtml = new MimeBodyPart();
  +                             subContainer.addBodyPart(msgHtml);
  +                     }
  +
               if (StringUtils.isNotEmpty(this.charset))
               {
                   msgHtml.setContent(
  @@ -220,38 +269,94 @@
               }
   
               Iterator iter = this.inlineImages.iterator();
  -            while (iter.hasNext())
  +                     while (iter.hasNext())
  +                     {
  +                             subContainerHTML.addBodyPart((BodyPart) 
iter.next());
  +                     }
  +        }
  +
  +        // add sub containers to message
  +        this.addPart(subContainer);
  +
  +             if (this.inlineImages.size() > 0)
  +             {
  +                     // add sub container to message
  +                     this.addPart(subContainerHTML);
  +             }
  +    }
  +
  +    /**
  +     * @throws MessagingException MessagingException
  +     */
  +    private void buildNoAttachments() throws MessagingException
  +    {
  +        MimeMultipart container = this.getContainer();
  +        MimeMultipart subContainerHTML = new MimeMultipart("related");
  +
  +        container.setSubType("alternative");
  +
  +        BodyPart msgText = null;
  +        BodyPart msgHtml = null;
  +
  +        if (StringUtils.isNotEmpty(this.text))
  +        {
  +            msgText = this.getPrimaryBodyPart();
  +            if (StringUtils.isNotEmpty(this.charset))
               {
  -                container.addBodyPart((BodyPart) iter.next());
  +                msgText.setContent(
  +                    this.text,
  +                    Email.TEXT_PLAIN + "; charset=" + this.charset);
  +            }
  +            else
  +            {
  +                msgText.setContent(this.text, Email.TEXT_PLAIN);
               }
           }
   
  -        if (StringUtils.isNotEmpty(this.text))
  +        if (StringUtils.isNotEmpty(this.html))
           {
  -            // if the html part of the message was null, then the text part
  +            // if the txt part of the message was null, then the html part
               // will become the primary body part
  -            if (msgHtml == null)
  +            if (msgText == null)
               {
  -                msgText = getPrimaryBodyPart();
  +                msgHtml = getPrimaryBodyPart();
               }
               else
               {
  -                msgText = new MimeBodyPart();
  -                container.addBodyPart(msgText);
  +                if (this.inlineImages.size() > 0)
  +                {
  +                    msgHtml = new MimeBodyPart();
  +                    subContainerHTML.addBodyPart(msgHtml);
  +                }
  +                else
  +                {
  +                    msgHtml = new MimeBodyPart();
  +                    container.addBodyPart(msgHtml);
  +                }
               }
   
               if (StringUtils.isNotEmpty(this.charset))
               {
  -                msgText.setContent(
  -                    this.text,
  -                    Email.TEXT_PLAIN + "; charset=" + this.charset);
  +                msgHtml.setContent(
  +                    this.html,
  +                    Email.TEXT_HTML + "; charset=" + this.charset);
               }
               else
               {
  -                msgText.setContent(this.text, Email.TEXT_PLAIN);
  +                msgHtml.setContent(this.html, Email.TEXT_HTML);
               }
  -        }
   
  -        super.send();
  +            Iterator iter = this.inlineImages.iterator();
  +            while (iter.hasNext())
  +            {
  +                subContainerHTML.addBodyPart((BodyPart) iter.next());
  +            }
  +
  +            if (this.inlineImages.size() > 0)
  +            {
  +                // add sub container to message
  +                this.addPart(subContainerHTML);
  +            }
  +        }
       }
   }
  
  
  
  1.5       +23 -1     
jakarta-commons-sandbox/email/src/test/org/apache/commons/mail/mocks/MockHtmlEmailConcrete.java
  
  Index: MockHtmlEmailConcrete.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-commons-sandbox/email/src/test/org/apache/commons/mail/mocks/MockHtmlEmailConcrete.java,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- MockHtmlEmailConcrete.java        2 Nov 2004 11:13:26 -0000       1.4
  +++ MockHtmlEmailConcrete.java        14 Nov 2004 18:07:43 -0000      1.5
  @@ -15,9 +15,11 @@
    */
   package org.apache.commons.mail.mocks;
   
  +import java.io.IOException;
   import java.util.ArrayList;
   import java.util.List;
   
  +import javax.mail.MessagingException;
   import javax.mail.internet.InternetAddress;
   
   import org.apache.commons.mail.HtmlEmail;
  @@ -31,6 +33,26 @@
    */
   public class MockHtmlEmailConcrete extends HtmlEmail
   {
  +    /**
  +     * Retrieve the message content
  +     * @return Message Content
  +     */
  +    public String getMsg()
  +    {
  +        try
  +        {
  +            return this.getPrimaryBodyPart().getContent().toString();
  +        }
  +        catch (IOException ioE)
  +        {
  +            return null;
  +        }
  +        catch (MessagingException msgE)
  +        {
  +            return null;
  +        }
  +    }
  +
       /**
        * Retrieve the text msg
        * @return Message Content
  
  
  
  1.6       +27 -1     
jakarta-commons-sandbox/email/src/test/org/apache/commons/mail/BaseEmailTestCase.java
  
  Index: BaseEmailTestCase.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-commons-sandbox/email/src/test/org/apache/commons/mail/BaseEmailTestCase.java,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- BaseEmailTestCase.java    2 Nov 2004 11:13:26 -0000       1.5
  +++ BaseEmailTestCase.java    14 Nov 2004 18:07:43 -0000      1.6
  @@ -128,6 +128,32 @@
           fw.write(email.toString());
           fw.close();
       }
  +
  +    /**
  +     * @param intMsgNo the message to retrieve
  +     * @return message as string
  +     */
  +    public String getMessageAsString(int intMsgNo)
  +    {
  +        assertTrue(this.fakeMailServer.getReceievedEmailSize() >= intMsgNo);
  +        Iterator emailIter = fakeMailServer.getReceivedEmail();
  +        SmtpMessage emailMessage = null; 
  +        for (int intCurMsg = 0; intCurMsg < intMsgNo; intCurMsg++)
  +        {
  +            emailMessage = (SmtpMessage) emailIter.next();
  +        }
  +        
  +        if (emailMessage != null)
  +        {
  +            return emailMessage.toString();        
  +        }
  +        else
  +        {
  +            fail("Message note found");
  +            return "";
  +        }
  +    }
  +
       /** */
       public void getMailServer()
       {
  
  
  
  1.1                  
jakarta-commons-sandbox/email/src/test/org/apache/commons/mail/SendWithAttachmentsTest.java
  
  Index: SendWithAttachmentsTest.java
  ===================================================================
  /*
   * Copyright 2001-2004 The Apache Software Foundation
   *
   * Licensed under the Apache License, Version 2.0 ( the "License" );
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *     http://www.apache.org/licenses/LICENSE-2.0
   *
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   */
  package org.apache.commons.mail;
  
  import java.io.File;
  import java.io.IOException;
  import java.net.MalformedURLException;
  import java.net.URL;
  
  import javax.mail.MessagingException;
  
  import org.apache.commons.mail.mocks.MockHtmlEmailConcrete;
  import org.apache.commons.mail.settings.EmailConfiguration;
  
  /**
   * JUnit test case verifying bugzilla issue 30973 is fixed.
   *
   * @author <a href="mailto:[EMAIL PROTECTED]">Corey Scott</a>
   * @version $Id: SendWithAttachmentsTest.java,v 1.1 2004/11/14 18:07:43 epugh 
Exp $
   */
  
  public class SendWithAttachmentsTest extends BaseEmailTestCase
  {
      /** */
      private MockHtmlEmailConcrete email = null;
  
      /**
       * @param name name
       */
      public SendWithAttachmentsTest(String name)
      {
          super(name);
      }
  
      /** */
      protected void setUp()
      {
          super.setUp();
          // reusable objects to be used across multiple tests
          this.email = new MockHtmlEmailConcrete();
      }
  
      /** */
      public void testSendNoAttachments()
      {
          try
          {
              this.getMailServer();
  
              String strSubject = "Test HTML Send #1 Subject (w charset)";
  
              this.email = new MockHtmlEmailConcrete();
              this.email.setHostName(this.strTestMailServer);
              this.email.setFrom(this.strTestMailFrom);
              this.email.addTo(this.strTestMailTo);
  
              this.email.setAuthentication(this.strTestUser, 
this.strTestPasswd);
  
              this.email.setCharset(Email.ISO_8859_1);
              this.email.setSubject(strSubject);
  
              URL url = new URL(EmailConfiguration.TEST_URL);
              String cid = this.email.embed(url, "Apache Logo");
  
              String strHtmlMsg =
                  "<html>The Apache logo - <img src=\"cid:"; + cid + "\"><html>";
  
              this.email.setHtmlMsg(strHtmlMsg);
              this.email.setTextMsg(
                  "Your email client does not support HTML emails");
  
              this.email.send();
              this.fakeMailServer.stop();
  
              // validate txt message
              validateSend(
                  this.fakeMailServer,
                  strSubject,
                  this.email.getTextMsg(),
                  this.email.getFromAddress(),
                  this.email.getToList(),
                  this.email.getCcList(),
                  this.email.getBccList(),
                  true);
  
              // validate html message
              validateSend(
                  this.fakeMailServer,
                  strSubject,
                  this.email.getHtmlMsg(),
                  this.email.getFromAddress(),
                  this.email.getToList(),
                  this.email.getCcList(),
                  this.email.getBccList(),
                  false);
          }
          catch (MessagingException e)
          {
              e.printStackTrace();
              fail("Unexpected exception thrown");
          }
          catch (MalformedURLException e)
          {
              e.printStackTrace();
              fail("Unexpected exception thrown");
          }
          catch (IOException e)
          {
              e.printStackTrace();
              fail("Failed to save email to output file");
          }
  
      }
  
        /** */
        public void testSendWAttachments()
        {
                EmailAttachment attachment = new EmailAttachment();
                File testFile = null;
  
                try
                {
                        /** File to used to test file attachmetns (Must be 
valid) */
                        testFile = 
File.createTempFile("commons-email-testfile", ".txt");
                }
                catch (IOException e)
                {
                        e.printStackTrace();
                        fail("Test file cannot be found");
                }
  
                // 
====================================================================
                // Test Success
                // 
====================================================================
                try
                {
                        this.getMailServer();
  
                        String strSubject = "Test HTML Send #1 Subject (w 
charset)";
  
                        this.email = new MockHtmlEmailConcrete();
                        this.email.setHostName(this.strTestMailServer);
                        this.email.setFrom(this.strTestMailFrom);
                        this.email.addTo(this.strTestMailTo);
  
                        /** File to used to test file attachmetns (Must be 
valid) */
                        attachment.setName("Test Attachment");
                        attachment.setDescription("Test Attachment Desc");
                        attachment.setPath(testFile.getAbsolutePath());
                        this.email.attach(attachment);
  
                        this.email.setAuthentication(this.strTestUser, 
this.strTestPasswd);
  
                        this.email.setCharset(Email.ISO_8859_1);
                        this.email.setSubject(strSubject);
  
                        URL url = new URL(EmailConfiguration.TEST_URL);
                        String cid = this.email.embed(url, "Apache Logo");
  
                        String strHtmlMsg =
                                "<html>The Apache logo - <img src=\"cid:"; + cid 
+ "\"><html>";
  
                        this.email.setHtmlMsg(strHtmlMsg);
                        this.email.setTextMsg(
                                "Your email client does not support HTML 
emails");
  
                        this.email.send();
                        this.fakeMailServer.stop();
                        // validate txt message
                        validateSend(
                                this.fakeMailServer,
                                strSubject,
                                this.email.getTextMsg(),
                                this.email.getFromAddress(),
                                this.email.getToList(),
                                this.email.getCcList(),
                                this.email.getBccList(),
                                true);
  
                        // validate html message
                        validateSend(
                                this.fakeMailServer,
                                strSubject,
                                this.email.getHtmlMsg(),
                                this.email.getFromAddress(),
                                this.email.getToList(),
                                this.email.getCcList(),
                                this.email.getBccList(),
                                false);
  
                        // validate attachment
                        validateSend(
                                this.fakeMailServer,
                                strSubject,
                                attachment.getName(),
                                this.email.getFromAddress(),
                                this.email.getToList(),
                                this.email.getCcList(),
                                this.email.getBccList(),
                                false);
                }
                catch (MessagingException e)
                {
                        e.printStackTrace();
                        fail("Unexpected exception thrown");
                }
                catch (MalformedURLException e)
                {
                        e.printStackTrace();
                        fail("Unexpected exception thrown");
                }
                catch (IOException e)
                {
                        e.printStackTrace();
                        fail("Failed to save email to output file");
                }
      }
  
  }
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to