3

What I want to do is attach one or several PDFs to an e-mail. I am currently using MimeMessage to send emails which works flawlessly. The problem however is that I have no idea how to attach files. (More specifically PDFs I create using itext).

Any examples or tips are appreciated!

whirlwin
  • 16,044
  • 17
  • 67
  • 98

3 Answers3

3

This reading ("How to create an in-memory PDF report and send as an email attachment using iText and Java") should help you

Maxym
  • 11,836
  • 3
  • 44
  • 48
  • http://pdfbox.apache.org/ Apache Java PDF Library. – Dhananjay Jan 31 '11 at 10:02
  • @blob, please explain a bit... Article is about iText, as well as question (at least it was tagged as iText). PDFBox is alternative to iText, but how does it solve problem with attaching? – Maxym Jan 31 '11 at 10:10
2

Create an attachment on the MimeMessage (see javadocs), set the content type to "application/pdf", get the content OutputStream of it and write the bytes of the PDF to it (with Apache's commons-io IOUtils).

Geoffrey De Smet
  • 26,223
  • 11
  • 73
  • 120
2

You can use the famous Apache Jakart library called Commons Email.

If your emails are in html format you can use this code:

HtmlEmail email = new HtmlEmail();
email.setSubject("<your subject>");
email.setHtmlMsg("<your html message body>");
email.setHostName("<host>");
email.setFrom("<from_address>");
email.addTo("<recipient_address>");
email.send();

and then attach your pdf files

EmailAttachment attachment = new EmailAttachment();

String filePath = "pathtofile";
attachment.setPath(filePath);
attachment.setDisposition(EmailAttachment.ATTACHMENT);
attachment.setDescription("description for this attachment");

email.attach(attachment);

Otherwise you should use the MultiPartEmail class.

Hope can be helpful...

ROb

Impiastro
  • 848
  • 6
  • 8