Convert Email to PDF

There could be various scenarios when you need to embed the content of the emails within your web or desktop applications. In such cases, you can convert the email messages into a format that can easily be embedded and displayed. One of the possible solutions is email to PDF conversion. This article provides a step-by-step guide and code sample of how to convert an email to PDF in C#. You can follow the the provided steps and use the code sample for EML to PDF and MSG to PDF conversion in C#.

C# Email to PDF Converter - Free Download

Aspose.Email for .NET is a C# class library that is designed to create and manipulate popular email formats from within the .NET applications. In combination with Aspose.Words for .NET, the API lets you convert the EML and MSG email messages to PDF format with high fidelity. You can either download the APIs’ DLLs or install them within your .NET applications using NuGet.

PM> Install-Package Aspose.Words
PM> Install-Package Aspose.Email

Steps to Convert EML or MSG to PDF in C#

The following are the steps to convert email messages to PDF format in C#.

// Load email message using file
MailMessage mailMsg = MailMessage.Load("message.msg");
view raw load-email.cs hosted with ❤ by GitHub
  • Save the email message into a MemoryStream object as default MHTML format.
// Create memory stream
MemoryStream ms = new MemoryStream();
// Save email message into memory stream
MailMessage.Save(ms, Aspose.Email.SaveOptions.DefaultMhtml);
// Create and set load options
var loadOptions = new Aspose.Words.LoadOptions();
loadOptions.LoadFormat = LoadFormat.Mhtml;
// Create an instance of Document and load the MTHML from MemoryStream
var document = new Aspose.Words.Document(ms, loadOptions);
// create an instance of PDFSaveOptions class
var pdfSaveOptions = new Aspose.Words.Saving.PdfSaveOptions();
// Save email as PDF
document.Save("email-to-pdf.pdf", pdfSaveOptions);

Email to PDF C# Conversion - Complete Code

The following is the complete code to convert an email message to PDF using Aspose.Emails for .NET and Aspose.Words for .NET APIs.

// Load email message
MailMessage mailMsg = MailMessage.Load("message.msg");
MemoryStream ms = new MemoryStream();
mailMsg.Save(ms, Aspose.Email.SaveOptions.DefaultMhtml);
// create an instance of LoadOptions and set the LoadFormat to Mhtml
var loadOptions = new Aspose.Words.LoadOptions();
loadOptions.LoadFormat = LoadFormat.Mhtml;
// create an instance of Document and load the MTHML from MemoryStream
var document = new Aspose.Words.Document(ms, loadOptions);
// create an instance of HtmlSaveOptions
var saveOptions = new Aspose.Words.Saving.PdfSaveOptions();
document.Save("email-to-pdf.pdf", saveOptions);

EML or MSG to PDF C# Converter - Get a Free License

You can get a free temporary license in order to convert emails to PDF without evaluation limitations.

Aspose Email to PDF Converter - Live Demo

Conclusion

Email to PDF conversion may come into practice when you need to embed the email messages within your web or desktop applications. Therefore, in this article, you have learned how to convert emails to PDF in C# within your .NET applications. In order to explore more about the APIs being used, you can visit the following documentations.

See Also