Emailing Dynamically Created PDFs

Emailing Dynamically Created PDFs

Support · Oct 25, 2005
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 .NET. 2. Using the "Draw" method save that PDF to File. 3. Using the .NET Framework's built in emailing functionality, 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 .NET examples for creating DynamicPDFs, http://www.DynamicPDF.com/Products/MergerForNET/Examples.csp , http://www.DynamicPDF.com/Products/GeneratorForNET/Examples.csp . Second, you will need to use the "Draw" method and save the PDF to an actual location on disk. Saving the file to an actual location on disk is required as the .NET Framework's emailing functionality does not have the ability to attach a file from memory. (There are third party tools out there that will allow you to attach a file from a memory stream if required.) Finally, use the System.Web.Mail objects to create, attached and send the email. Create a new "MailMessage" object and then set its properties (to, from, body, etc.). Then add an attachment to the message as a new "MailAttachment" specifying the file path to the PDF. Lastly, make sure that you have specified an SMTP server to send from using the "SmtpMail.SmtpServer" static (Shared in VB.NET) property. Here is some sample code for both C# and VB.NET C# ceTe.DynamicPDF.Document objDocument = new ceTe.DynamicPDF.Document(); //Create DynamicPDF Document Here objDocument.Draw( @"C:\temp\document.pdf" ); MailMessage objMessage = new MailMessage(); objMessage.To = "receiver@domain.com"; objMessage.From = "sender@domain.com"; objMessage.Subject = "Attached PDF Email"; objMessage.Body = "This is the body of the email. The DynamicPDF is attached"; SmtpMail.SmtpServer = "mail.domain.net"; objMessage.Attachments.Add( new MailAttachment( @"C:\temp\document.pdf" ) ); SmtpMail.Send( objMessage ); VB.NET Dim objDocument As ceTe.DynamicPDF.Document = New ceTe.DynamicPDF.Document 'Create DynamicPDF Document Here objDocument.Draw("C:\temp\document.pdf") Dim objMessage As MailMessage = New MailMessage objMessage.To = "receiver@domain.com" objMessage.From = "sender@domain.com" objMessage.Subject = "Attached PDF Email" objMessage.Body = "This is the body of the email. The DynamicPDF is attached" SmtpMail.SmtpServer = "mail.domain.net" objMessage.Attachments.Add(New MailAttachment("C:\temp\document.pdf")) SmtpMail.Send(objMessage)