10

I'm using Apache Commons Email 1.1 and I can't figure out how to attach a file to an HtmlEmail. If I run the code below, I get an email with an attachment, but the HTML message comes across as an attachment also.

If I don't call email.attach() the HTML message come through as you would expect, but I need both the HTML message to come through and the attachment. What am I missing?

  HtmlEmail email = new HtmlEmail();
  email.setHostName("localhost");
  email.addTo("test@mail.com", "Test");
  email.setFrom("testapp@mail.com", "Test App");
  email.setSubject("Test message");
  email.setHtmlMsg("<div style='font-size: 20px; color: green;'>This is html email</div>");

  EmailAttachment attachment = new EmailAttachment();
  attachment.setPath(pdfPath);
  attachment.setDisposition(EmailAttachment.ATTACHMENT);
  email.attach(attachment);

  email.send();
skaffman
  • 398,947
  • 96
  • 818
  • 769
delux247
  • 2,177
  • 3
  • 21
  • 29
  • Oh.. by the way, I was using gmail as my test client. – delux247 Oct 22 '09 at 22:19
  • Did you try to force content type by doing email.setContent(javax.mail.internet.MimeMultipart aMimeMultipart)? It's probably redundant but worth a shot – Bostone Oct 22 '09 at 22:32
  • @droidin-net I'm not sure I understand what you mean. I though the whole point of apache commons email was to abstract you from the MimeMultipart stuff. – delux247 Oct 23 '09 at 15:07

6 Answers6

13
email.attach(new ByteArrayDataSource(pdfBytes, "application/pdf"),
      "document.pdf", "Document description",
       EmailAttachment.ATTACHMENT);

this works with commons-email 1.1.

pdfBytes should be a byte[] containing the bytes of the pdf document. If that doesn't suit you, you can try other DataSource implementations, but I can't guarantee they'd work (although they should).

(The one above is org.apache.commons.mail.ByteArrayDataSource)

Bozho
  • 588,226
  • 146
  • 1,060
  • 1,140
4

Note that using:

email.attach(new ByteArrayDataSource(pdfBytes, "application/pdf"),
      "document.pdf", "Document description",
       EmailAttachment.ATTACHMENT);

on a HtmlEmail using commons-email 1.1 causes the resulting email to have its message (text or html) enclosed as an attachment.

Switching to a MultiPartEmail fixed this.

Kristian Glass
  • 37,325
  • 7
  • 45
  • 73
Thomas N
  • 418
  • 1
  • 7
  • 14
3

I suggest you try the current release candidate v1.2 RC2 as 1.1 (I guess you use that) has some html layout problems

commons-email 1.2 RC2

jitter
  • 53,475
  • 11
  • 111
  • 124
0

I use the HtmlEmail#embed(URL, String) method:

File pdf = new File(pdfPath);
email.embed(pdf.toURI().toURL(), pdf.getName)
Jerry
  • 789
  • 1
  • 13
  • 31
0

On latest release (1.5) the following code worked for me

 email.attach(new FileDataSource(attachmentFileObject), "AttachmentName", "Description");
Cybermonk
  • 514
  • 1
  • 6
  • 28
0

You can use up-casting and down-casting in Java.

  1. HtmlEmail extends MultiPartEmail, and attacch method returns the MultiPartEmail class.
  2. HtmlEmail will be converted to MultiPartEmail, and then use tempEmail.attach (attachment).
  3. Then MultiPartEmail will be converted back to HtmlEmail class.

    HtmlEmail email;
    MultiPartEmail tempEmail;
    
    // HtmlEmail -> MultiPartEmail
    tempEmail = new HtmlEmail();
    
    // Add a attachment
    EmailAttachment attachment = new EmailAttachment();
    attachment.setURL(new URL(fileURL));
    attachment.setDisposition(EmailAttachment.ATTACHMENT);
    attachment.setName(MimeUtility.encodeText(fileName));
    tempEmail = tempEmail.attach(attachment);
    
    // MultiPartEmail -> HtmlEmail
    email = (HtmlEmail)tempEmail;
    
JamesR
  • 1