Uploaded image for project: 'Commons Email'
  1. Commons Email
  2. EMAIL-28

[email] Problems with HTML emails with attachments and embedded images

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Major
    • Resolution: Fixed
    • 1.0
    • 1.1
    • None
    • Operating System: Windows XP
      Platform: PC

    • 37159

    Description

      I have three problems that are probably related. The results are using Mozilla
      Thunderbird.

      1. When I send an HtmlEmail with attachments, embedded images, an HTML body and
      an alternative text body both the alternative text and the HTML are displayed.

      2. When I send an HtmlEmail with embedded images, no attachments, an HTML body
      and I do not provide an alternative text body, the HTML body is not displayed.

      3. When I send an HtmlEmail with attachments, embedded images, an HTML body and
      I do not provide an alternative text, everything works almost fine. The only
      problem is that when the SMTP server is sending emails outside its domain it
      adds a disclaimer. The addition of this disclaimer means that only the
      disclaimer is displayed. This does not happend when using Javamail.

      Below is the class that I used for my tests.

      import java.net.MalformedURLException;
      import java.net.URL;

      import org.apache.commons.mail.EmailAttachment;
      import org.apache.commons.mail.EmailException;
      import org.apache.commons.mail.HtmlEmail;

      public class CommonsEmailTest {

      /**

      • @param args
        */
        public static void main(String[] args)
        Unknown macro: { try { testHtmlEmailWithAttachmentsAndEmbeddedImages(); testHtmlEmailWithoutAttachmentsOrText(); testHtmlEmailWithAttachmentsAndEmbeddedImagesNoText(); } catch (MalformedURLException e) { e.printStackTrace(); } catch (EmailException e) { e.printStackTrace(); } }

      /**

      • This method sends an HTML email with attachments, embedded images,
      • an HTML body and an alternative text body. The result is that both
      • the HTML and the text are displayed.
      • @throws EmailException
      • @throws MalformedURLException
        */
        public static void testHtmlEmailWithAttachmentsAndEmbeddedImages() throws
        EmailException, MalformedURLException { // Create the email message HtmlEmail email = new HtmlEmail(); email.setHostName("smtp.capgemini.fr"); email.addTo("pbrether@capgemini.fr", "You"); email.setFrom("philip.bretherton@capgemini.fr", "Me"); email.setSubject("Test email with inline image and attachment"); // Create the attachment EmailAttachment attachment = new EmailAttachment(); attachment.setPath("D:/temp/mypic.jpg"); attachment.setDisposition(EmailAttachment.ATTACHMENT); attachment.setDescription("A picture"); attachment.setName("Thingy"); email.attach(attachment); // embed the image and get the content id URL url = new URL("http://www.apache.org/images/asf_logo_wide.gif"); String cid = email.embed(url, "Apache logo"); // set the html message email.setHtmlMsg("<html>The apache logo - <img src=\"cid:"+cid+"\"></html>"); // set the alternative message email.setTextMsg("Your email client does not support HTML messages"); // send the email email.send(); }

      /**

      • This method sends an HTML email with embedded images,
      • an HTML body and no alternative text body. The result is that both
      • the HTML is not displayed.
      • @throws EmailException
      • @throws MalformedURLException
        */
        public static void testHtmlEmailWithoutAttachmentsOrText() throws
        EmailException, MalformedURLException { // Create the email message HtmlEmail email = new HtmlEmail(); email.setHostName("smtp.capgemini.fr"); email.addTo("pbrether@capgemini.fr", "You"); email.setFrom("philip.bretherton@capgemini.fr", "Me"); email.setSubject("Test email with inline image and no alternative text"); // embed the image and get the content id URL url = new URL("http://www.apache.org/images/asf_logo_wide.gif"); String cid = email.embed(url, "Apache logo"); // set the html message email.setHtmlMsg("<html>The apache logo - <img src=\"cid:"+cid+"\"></html>"); // send the email email.send(); }

      /**

      • This method sends an HTML email with attachments, embedded images,
      • an HTML body and no alternative text body. The normal result is that
      • everything is displayed as expected. However, if the SMTP server
      • adds a disclaimer, the disclaimer is the only thing displayed.
      • @throws EmailException
      • @throws MalformedURLException
        */
        public static void testHtmlEmailWithAttachmentsAndEmbeddedImagesNoText() throws
        EmailException, MalformedURLException { // Create the email message HtmlEmail email = new HtmlEmail(); email.setHostName("smtp.capgemini.fr"); email.addTo("pbretherton@gmail.com", "You"); email.setFrom("philip.bretherton@capgemini.fr", "Me"); email.setSubject("Test email with disclaimer"); // Create the attachment EmailAttachment attachment = new EmailAttachment(); attachment.setPath("D:/temp/mypic.jpg"); attachment.setDisposition(EmailAttachment.ATTACHMENT); attachment.setDescription("A picture"); attachment.setName("Thingy"); email.attach(attachment); // embed the image and get the content id URL url = new URL("http://www.apache.org/images/asf_logo_wide.gif"); String cid = email.embed(url, "Apache logo"); // set the html message email.setHtmlMsg("<html>The apache logo - <img src=\"cid:"+cid+"\"></html>"); // send the email email.send(); }

      }

      Attachments

        Issue Links

          Activity

            People

              Unassigned Unassigned
              pbrether@capgemini.fr Phil Bretherton
              Votes:
              2 Vote for this issue
              Watchers:
              0 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: