How to Convert Emails (EML/MSG Files) to PDF in C#
In this tutorial, you’ll learn how to use PSPDFKit’s C# PDF library to convert email files (EML and MSG files) to PDF documents in C#. You’ll also learn about the specifics of the PDF/A format and how you can create PDF/A documents from your email files by changing just one line of code.
GdPicture.NET’s conversion from email files to PDFs offers the following benefits:
-
Ability to convert EML and MSG files to PDF without external libraries or dependency on Microsoft Office interop.
-
Embedding email attachments into the output PDF or PDF/A document as PDF attachment annotations.
-
Best-in-class Unicode support.
-
Full support for documents with mixed encoding and mixed right-to-left (RTL) and left-to-right (LTR) text.
PSPDFKit’s C# .NET PDF Library
PSPDFKit’s GdPicture.NET library is a complete SDK that supports:
-
Many other image and document-related tasks
The SDK also supports more than 100 formats and abstracts all of its powerful features behind a rich, cross-platform, and easy-to-use API to maximize developer productivity.
Requirements
You’ll be creating a .NET 6 application in this tutorial. There are a few things you need to do before you can begin. This list includes:
-
Setting up Visual Studio
-
Installing the GdPicture.NET SDK as a dependency
You can install Visual Studio by following this guide from Microsoft.
For now, you can only develop applications using GdPicture.NET on Windows. However, you can deploy them on multiple different platforms. So, make sure you’re following this tutorial on a Windows machine.
Creating a New .NET 6 Project
Open Visual Studio and create a new project. In this tutorial, to keep things simple, you’ll create a console app.
Name it EmailConverter
.
Choose .NET 6.0 as the target framework on the next screen. If you don’t like top-level statements, you can also toggle the checkbox.
This will result in a brand-new project with the name EmailConverter
. Now you’ll set up GdPicture.NET as a dependency for this project.
Setting Up GdPicture.NET as a Dependency
The Solution Explorer is on the right side of your Visual Studio window. Right-click the project name and select Manage NuGet Packages….
In the NuGet window, go to the Browse tab, set the Package source to nuget.org, and search for GdPicture. This will bring up GdPicture.API. Install it.
If you ever have to use .NET Framework or .NET Core 3.0, you can install GdPicture instead, as it supports these older .NET SDKs. However, to be able to deploy a cross-platform application, you’ll need to use GdPicture.API.
Once the installation is finished, open your project file, .csproj
, and it’ll have a reference similar to this:
<PackageReference Include="GdPicture.API" Version="14.2.37" />
The image below shows what the rest of the project file looks like at this stage.
Now you’re all set to start using GdPicture.NET in your project.
Working with GdPicture.NET
In this section, you’ll learn how to convert EML and MSG files to PDF and PDF/A documents.
Importing the GdPicture.NET Namespace
As this is the 14th version of GdPicture.NET, 14
is part of the namespace name. This will differ based on the GdPicture version you reference in your project file.
Now, add this code to unlock GdPicture.NET using your license key:
LicenseManager licenseManager = new LicenseManager(); licenseManager.RegisterKEY("YOUR_LICENSE_KEY");
Don’t forget to replace YOUR_LICENSE_KEY
with the key you received via email.
Converting EML to PDF in C#
The first conversion you’ll perform is from EML to PDF. You can download EML files from most email providers these days. You can use this EML file to follow along. Here’s the code to do the actual conversion:
using GdPictureDocumentConverter gdpictureDocumentConverter = new GdPictureDocumentConverter(); // Load the source document. gdpictureDocumentConverter.LoadFromFile(@"C:\temp\source.eml", GdPicture14.DocumentFormat.DocumentFormatEML); // Save the output in a new PDF document. gdpictureDocumentConverter.SaveAsPDF(@"C:\temp\output.pdf");
To convert from one document format to another, instantiate a GdPictureDocumentConverter
object. Then, load the source file using the LoadFromFile
method and inform GdPictureDocumentConverter
about the format of the input document. Lastly, use the appropriate method to save the document in the desired format. As you want to save the output as PDF, you have to use the SaveAsPDF
method.
Replace the source and destination paths in the code sample above and run your project. It’ll output a PDF file in the provided output location.
The image below shows what the generated PDF will look like.
All of your fancy emojis will show up perfectly in the output PDF. This just highlights the superb Unicode support provided by GdPicture.NET. If you’re using a trial evaluation license of GdPicture.NET, you’ll also see the popup shown below.
Click OK and the program will continue normal execution.
Now, here’s the best part: If an email has attachments, like the sample email you just converted, GdPicture.NET will reliably embed all the attachments in the output PDF! You can open the resulting PDF in a program that supports reading PDF attachments (like Adobe Acrobat Reader) and the original attachments will appear. Below is what the sample EML file with an MP4 attachment will look like in Acrobat Reader after conversion.
This is something that most other popular EML/MSG-to-PDF conversion solutions don’t support.
Converting MSG to PDF in C#
You might also want to convert MSG files to PDF. MSG is a format that was introduced by Microsoft as part of Microsoft Exchange Server and Microsoft Outlook in 2003. Most third-party tools make use of Microsoft Office interop for converting MSG files to PDF. To use interop, you need to have MS Office purchased and installed on your server/desktop, which causes licensing issues. However, our support for MSG files doesn’t rely on Microsoft Office interop.
You can use this MSG file to follow along. The code for converting a MSG file to a PDF is similar to the code for converting an EML to PDF:
using GdPictureDocumentConverter gdpictureDocumentConverter = new GdPictureDocumentConverter(); // Load the source document. gdpictureDocumentConverter.LoadFromFile(@"C:\temp\source.msg", GdPicture14.DocumentFormat.DocumentFormatMSG); // Save the output in a new PDF document. gdpictureDocumentConverter.SaveAsPDF(@"C:\temp\output.pdf");
The only difference in this code is the GdPicture14.DocumentFormat.DocumentFormatMSG
argument that tells the GdPictureDocumentConverter
object that the input document is a MSG type.
Converting EML and MSG to PDF/A
PDF/A is a subset of the PDF standard that’s designed to ensure the long-term preservation of electronic documents. The “A” in PDF/A stands for “Archival.” The primary differences between PDF and PDF/A are that PDF/A requires embedding all fonts and color spaces in the PDF file and providing additional metadata to keep the PDF accessible and visually consistent even as technology evolves. GdPicture.NET provides extensive PDF/A support out of the box.
If you want to learn more about PDF/A, check out our in-depth blog post, What Is PDF/A? A Complete 2023 Guide.
If you want to convert EML and MSG files to PDF/A format, you have two options. If you’re starting from scratch, you can convert the files to PDF/A directly using the same code as before. You just need to add one extra argument to the SaveAsPDF
method call to configure PDF conformance:
// Save the file as PDF with PDF/A-2a conformance level. gdpictureDocumentConverter.SaveAsPDF(@"C:\temp\output.pdf", PdfConformance.PDF_A_2a);
This will make sure that the output PDF only uses those PDF features that are part of the PDF/A standard. There are quite a few PDF conformance options supported by GdPicture.NET out of the box. Refer to this documentation page to read about all of them.
However, if you’ve already converted the EML/MSG file to PDF and now want to convert that PDF file to PDF/A, you can use this code:
using GdPicturePDF gdpicturePDF = new GdPicturePDF(); // Load the source document. gdpicturePDF.LoadFromFile(@"C:\temp\source.pdf"); // Convert to a document with PDF/A-2a conformance level. gdpicturePDF.ConvertToPDFA(@"C:\temp\output.pdf", PdfConversionConformance.PDF_A_2a, true, true); // Release unnecessary resources. gdpicturePDF.CloseDocument();
This code creates a new GdPicturePDF
object, loads a PDF file using the LoadFromFile
method, converts the file from PDF to PDF/A, and saves it to a new file using the ConvertToPDFA
method. Finally, it disposes of all unnecessary resources using the CloseDocument
method.
Configurable Properties of GdPictureDocumentConverter
Thus far, you’ve been using GdPictureDocumentConverter
with its default configuration. You can alter the conversion process by modifying the configuration of GdPictureDocumentConverter
. You can configure the following properties:
You can configure any of these properties directly on the GdPictureDocumentConverter
object like this:
using GdPictureDocumentConverter gdpictureDocumentConverter = new GdPictureDocumentConverter(); gdpictureDocumentConverter.EmailPreferOnePage = true;
Conclusion
In this tutorial, you learned about our .NET PDF SDK and how you can use it to convert EML and MSG files to PDF and PDF/A format. You also saw an example of email attachment handling and Unicode support provided by GdPicture.NET.
This tutorial scratches the surface of what’s possible with GdPicture.NET. If you’re interested in exploring more capabilities, you can try it for free, or reach out to our team.