You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by bu...@apache.org on 2005/10/19 12:05:39 UTC

DO NOT REPLY [Bug 37159] New: - Problems with HTML emails with attachments and embedded images

DO NOT REPLY TO THIS EMAIL, BUT PLEASE POST YOUR BUG�
RELATED COMMENTS THROUGH THE WEB INTERFACE AVAILABLE AT
<http://issues.apache.org/bugzilla/show_bug.cgi?id=37159>.
ANY REPLY MADE TO THIS MESSAGE WILL NOT BE COLLECTED AND�
INSERTED IN THE BUG DATABASE.

http://issues.apache.org/bugzilla/show_bug.cgi?id=37159

           Summary: Problems with HTML emails with attachments and embedded
                    images
           Product: Commons
           Version: 1.0 Final
          Platform: PC
        OS/Version: Windows XP
            Status: NEW
          Severity: normal
          Priority: P2
         Component: Email
        AssignedTo: commons-dev@jakarta.apache.org
        ReportedBy: pbrether@capgemini.fr


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) {
		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();		
	}


}

-- 
Configure bugmail: http://issues.apache.org/bugzilla/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
You are the assignee for the bug, or are watching the assignee.

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org