Attach PDF to Email

What I want to do is attach one or more PDF files to the email. I am currently using MimeMessageto send emails that work flawlessly. However, the problem is that I have no idea how to attach files. (More specifically, the PDF files that I create with itext).

Any examples or tips appreciated!

+3
source share
3 answers

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

+3
source

MimeMessage (. javadocs), "application/pdf", OutputStream PDF ( Apache commons-io IOUtils).

+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 must use the MultiPartEmail class.

Hope can be helpful ...

Rob

+2
source

Source: https://habr.com/ru/post/1788938/


All Articles