Having the ability to create great looking, DynamicPDF documents is one thing but we sometimes need those documents to get to an end user, a customer or a colleague in real time. Well, here is some helpful information that will allow you to email those PDFs from within your DynamicPDF applications.
There are three basic steps for getting your PDFs emailed on the fly:
1. Create your PDF using DynamicPDF Generator/Merger for Java.
2. Using the "Draw" method save that PDF to a File.
3. Using the emailing functionality of the JavaMail API and the Java Activation Framework (JAF) attach and send the PDF.
First, create a PDF. If you are just getting started you can take a look at any of our Merger or Generator for Java examples for creating DynamicPDFs, http://www.DynamicPDF.com/Products/MergerForJava/Examples.csp
, http://www.DynamicPDF.com/Products/GeneratorForJava/Examples.csp
.
Second, use the "Draw" method and save the PDF to an actual location on disk. Note that it is possible to attach the PDF to the email message directly from the outputstream stream (more information on this can be provided if needed).
Finally, create, attached and send the email. Create a new "MimeMessage" object using the appropriate SMTP settings. Set the appropriate properties (to, from, body, etc.). Then add an attachment to the message and send it.
Here is some sample Java code:
Document objDocument = new Document();
// Create DynamicPDF Document Here
objDocument.draw("C:\\temp\\document.pdf");
Properties props = System.getProperties();
props.put("mail.smtp.host", "mail.domain.net");
MimeMessage message = new MimeMessage(Session.getDefaultInstance(props, null));
message.setFrom(new InternetAddress("sender@domain.com"));
message.addRecipient(Message.RecipientType.TO, new InternetAddress("receiver@domain.com"));
message.setSubject("Attached PDF Email");
MimeBodyPart mbp1 = new MimeBodyPart();
FileDataSource fds = new FileDataSource("C:\\temp\\document.pdf");
mbp1.setDataHandler(new DataHandler(fds));
mbp1.setFileName(fds.getName());
Multipart mp = new MimeMultipart();
mp.addBodyPart(mbp1);
message.setContent(mp);
Transport.send(message);