Converting email files to PDF
Convert email messages (MSG and EML) to PDF when you need long-term records for compliance, legal review, or business documentation. PDF output preserves message content in a stable format.
How Nutrient supports this workflow
Nutrient .NET SDK converts email files to PDF through a single API call.
If you run this workflow inside Docker on Linux, make sure the container includes Chrome and runs as a non-root user. Refer to the running Chrome-based conversions in Docker guide.
You don’t need to manage:
- Email format parsing
- Rendering of headers, message body, and formatting
- Inline images and attachments
- MIME content handling
Use the SDK API to handle conversion logic in your application.
Complete implementation
This example shows a complete email-to-PDF conversion flow. This line sets up the C# application. The using directive brings in the Nutrient SDK namespace:
using Nutrient;This line opens the email file. The using declaration(opens in a new tab) ensures the document is automatically closed when it goes out of scope, preventing resource leaks:
try{ using Document document = Document.Open("input.msg");This block exports the file to PDF and saves it as output.pdf. The try-catch block handles potential errors using NutrientException:
document.ExportAsPdf("output.pdf"); Console.WriteLine("Successfully converted to output.pdf");}catch (NutrientException e){ Console.Error.WriteLine($"Error: {e.Message}"); Environment.Exit(1);}Configure conversion security
By default, conversion restricts access to the local network and blocks background connections and tasks. Public resources, document JavaScript, and files extracted from the email remain available. Tighten or relax these settings according to how much you trust the source. Email conversion uses the same security settings as HTML conversion.
Configure these controls in the document’s conversion settings before opening the email.
| Setting | Default | Effect |
|---|---|---|
HtmlBlockPrivateNetworkAccess | true | Restricts access to the local network. |
HtmlPrivateNetworkAllowList | Empty | Permits exact host names, IP addresses, or CIDR ranges through private-network blocking. |
HtmlBlockFileAccess | false | Blocks access to files extracted from the email, including inline images. Other files are always blocked. |
HtmlBlockWebSocketsAndWorkers | true | Blocks background connections and tasks. |
HtmlBlockNetworkAccess | false | Blocks network requests during rendering, including public images and other remote resources. |
HtmlScriptsExecutionDisabled | false | Disables document JavaScript during rendering. |
For untrusted sources that need no external resources or JavaScript, set HtmlBlockNetworkAccess, HtmlScriptsExecutionDisabled, and HtmlBlockFileAccess to true. For trusted sources, relax only the restrictions needed by the document.
To enable access to a specific internal resource, add its host name, IP address, or CIDR range to the allow list. Entries exclude ports and URL schemes. Invalid entries fail when conversion starts. The allow list doesn’t override other blocking settings.
Summary
The conversion flow has two steps:
- Open the email file.
- Export it as PDF.
Nutrient handles email parsing and PDF rendering, so you don’t need to implement format-specific conversion logic.