How to attach a file to an HTML email using Apache Commons Email
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.
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
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
)