How to generate and send a PDF document as attachment in an email using .Net Core C#?
The Syncfusion Essential® PDF is a feature rich and high-performance .NET PDF library used to create, read, and edit PDF documents programmatically without Adobe dependencies. This library also offers functionality to merge, split, stamp, forms, compress, and secure PDF files.
You can send a pdf document in an email using this library.
Steps to send a pdf document in an email using C#:
- Create a new C# console application project.
- Install Syncfusion.Pdf.Net.Core NuGet package as a reference to your .NET Framework applications from the NuGet.org.
- Include the following namespaces in the Program.cs file.
C#:
using Syncfusion.Pdf; using Syncfusion.Pdf.Parsing;
VB.Net:
Imports Syncfusion.Pdf Imports Syncfusion.Pdf.Parsing
- Add the following code snippet to the Program.cs file.
C#:
// Create a new instance of PdfDocument class. PdfDocument document = new PdfDocument(); // Add a page to the document. PdfPage page = document.Pages.Add(); // Create PDF graphics for the page. PdfGraphics g = page.Graphics; // Create a solid brush PdfBrush brush = new PdfSolidBrush(Color.Black); // Set the font. PdfFont font = new PdfStandardFont(PdfFontFamily.Helvetica, 20f); // Draw the text. g.DrawString("Hello world!", font, brush, new PointF(20, 20)); MemoryStream ms = new MemoryStream(); // Save and close the document. document.Save(ms); document.Close(true); //Reset the memory stream position. ms.Position = 0; Attachment file = new Attachment(ms, "PdfAttachment.pdf", "application/pdf"); //Sends the email message //Update the required e-mail id here SendEMail("user1@outlook.com", "user2@outlook.com", "Essential PDF document", "Create PDF MailBody", file);
VB.Net:
Private Sub SurroundingSub() ‘Create a new instance of PdfDocument class. Dim document As PdfDocument = New PdfDocument() ‘Add a page to the document. Dim page As PdfPage = document.Pages.Add() ‘Create PDF graphics for the page. Dim g As PdfGraphics = page.Graphics ‘Create a solid brush Dim brush As PdfBrush = New PdfSolidBrush(Color.Black) ‘Set the font. Dim font As PdfFont = New PdfStandardFont(PdfFontFamily.Helvetica, 20F) ‘Draw the text. g.DrawString("Hello world!", font, brush, new PointF(20, 20)) Dim ms As MemoryStream = New MemoryStream() ‘Save and close the document. document.Save(ms) document.Close(True) ms.Position = 0 Dim file As Attachment = New Attachment(ms, "PdfAttachment.pdf", "application/pdf") SendEMail("MailId@outlook.com", "RecipientMailId@outlook.com", "Essential PDF generated Pdf document", mailBody, docStream) End Sub
Use the following code, enter your login details and recipient mail IDs to send a pdf document in an email.
private static void SendEMail(string from, string recipients, string subject, string body, Attachment attachment) { //Creates the email message MailMessage emailMessage = new MailMessage(from, recipients); //Adds the subject for email emailMessage.Subject = subject; //Sets the HTML string as email body emailMessage.IsBodyHtml = false; emailMessage.Body = body; //Add the file attachment to this e-mail message. emailMessage.Attachments.Add(attachment); //Sends the email with prepared message using (SmtpClient client = new SmtpClient()) { //Update your SMTP Server address here client.Host = "smtp.Live.com"; client.UseDefaultCredentials = false; //Update your email credentials here client.Credentials = new System.Net.NetworkCredential(from, "password"); client.Port = 587; client.EnableSsl = true; } }
VB.Net:
Private Sub SendEMail(ByVal from As String, ByVal recipients As String, ByVal subject As String, ByVal body As String, ByVal docStream As MemoryStream) 'Creates the email message Dim emailMessage As MailMessage = New MailMessage(from, recipients) 'Adds the subject for email emailMessage.Subject = subject 'Sets the HTML string as email body emailMessage.IsBodyHtml = True emailMessage.Body = body 'Add the file attachment to this e-mail message. emailMessage.Attachments.Add(New Attachment(docStream, "Sample.docx")) 'Sends the email with prepared message Using client As SmtpClient = New SmtpClient 'Update your SMTP Server address here client.Host = "smtp.outlook.com" client.UseDefaultCredentials = False 'Update your email credentials here client.Credentials = New System.Net.NetworkCredential(from, "password") client.Port = 587 client.EnableSsl = True client.Send(emailMessage) End Using End Sub
A complete working example to send a pdf document in an email using C# can be downloaded from send a pdf document in an email.zip
By executing the program, you can send a pdf document in an email as follows.
Take a moment to peruse the documentation, where you can find the options like adding text and image watermark to the PDF document.
Refer here to explore the rich set of Syncfusion Essential® PDF features.
Starting with v16.2.0.x, if you reference Syncfusion® assemblies from trial setup or from the NuGet feed,include a license key in your projects. Refer to link to learn about generating and registering Syncfusion® license key in your application to use the components without trail message.