Articles in this section
Category / Section

How to programmatically generate and email a PDF document as an attachment in .NET Core

7 mins read

The Syncfusion Essential® PDF is a comprehensive .NET PDF library that facilitates the creation, editing, and reading of PDF documents. This high-performance toolset supports an array of features such as merging, splitting, stamping, form handling, compression, and securing of PDFs. Additionally, the library simplifies workflows by enabling users to send PDF documents via email directly from applications, enhancing document management efficiency and productivity.

Steps to send a PDF document in an email using C#:

 

  1. Create a new console application project.

Console App.png

  1. Install Syncfusion.Pdf.Net.Core NuGet package as a reference in your console applications from the NuGet.org.

NuGetPackage.png

 

  1. Include the following namespaces in the Program.cs file.

C#:

using Syncfusion.Drawing;
using Syncfusion.Pdf;
using Syncfusion.Pdf.Graphics;
using System.Net.Mail;
using System.Net;

 

VB.Net:

Imports Syncfusion.Drawing
Imports Syncfusion.Pdf
Imports Syncfusion.Pdf.Graphics
Imports System.Net.Mail
Imports System.Net

 

  1. Add the following code sample in 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:

' Create a new instance of PdfDocument class.
Dim document As 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, 20.0F)
' Draw the text.
g.DrawString("Hello world!", font, brush, New PointF(20, 20))
Dim ms As New MemoryStream()
' Save and close the document.
document.Save(ms)
document.Close(True)
' Reset the memory stream position.
ms.Position = 0
Dim file As 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)

 

To send a PDF document via email, use the following code. Be sure to enter your login credentials and the recipient's email address correctly.

C#:

 

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 plain text as email body
    emailMessage.IsBodyHtml = false;
    emailMessage.Body = body;

    // Add the file attachment to this email message
    emailMessage.Attachments.Add(attachment);

    // Sends the email with the prepared message
    using (SmtpClient client = 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);
    }
}

 

VB.Net:

Private Shared Sub SendEMail(fromAddress As String, recipients As String, subject As String, body As String, attachment As Attachment)
    ' Create the email message
    Dim emailMessage As New MailMessage(fromAddress, recipients)

    ' Add the subject for the email
    emailMessage.Subject = subject

    ' Set the email body as plain text
    emailMessage.IsBodyHtml = False
    emailMessage.Body = body

    ' Attach the file to the email
    emailMessage.Attachments.Add(attachment)

    ' Create and configure the SMTP client
    Using client As New SmtpClient()
        ' Set your SMTP server address
        client.Host = "smtp.outlook.com"

        ' Set to false if you are using custom credentials
        client.UseDefaultCredentials = False

        ' Provide your email credentials
        client.Credentials = New NetworkCredential(fromAddress, "password")

        client.Port = 587
        client.EnableSsl = True

        ' Send the email
        client.Send(emailMessage)
    End Using
End Sub

 A complete working example demonstrating how to send a PDF document via 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.

output pdf screenshot

Take a moment to explore the documentation, where you can find various options such as adding text and image watermarks to a PDF document.

Note:

Starting with version 16.2.0.x, if you reference Syncfusion® assemblies from a trial setup or the NuGet feed, you must include a valid license key in your project. Refer to the documentation to learn how to generate and register the Syncfusion® license key in your application to avoid displaying the trial message.

Conclusion

I hope you enjoyed learning on how to send a PDF document via email using .NET PDF library.

You can refer to our
ASP.NET Core PDF feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications. You can also explore our ASP.NET Core PDF example to understand how to create and manipulate data.

For current customers, you can check out our components from the
License and Downloads page. If you are new to Syncfusion®, you can try our 30-day free trial to check out our other controls.

If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our
support forums, Direct-Trac, or feedback portal. We are always happy to assist you!



 

 

 

 

 

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please  to leave a comment
Access denied
Access denied