Render email messages as HTML, PDF, PNG, and JPEG files

View as MarkdownMarkdown

Set up your AI agent

Give your coding agent live access to the GroupDocs documentation and API reference through the GroupDocs Docs MCP server — no install, no API key. Learn more

claude mcp add --transport http groupdocs-docs https://docs.groupdocs.com/mcp

Also process documents locally

The GroupDocs.Viewer MCP server lets your agent work with files on your machine; they are never uploaded. It needs the .NET 10 SDK or Docker.

claude mcp add groupdocs-viewer -- dnx GroupDocs.Viewer.Mcp --yes
Folders, licensing and more →

GroupDocs.Viewer for .NET allows you to render your email messages in HTML, PDF, PNG, and JPEG formats. You do not need to use third-party email clients to view the contents of email files within your .NET application (web or desktop).

To start with the GroupDocs.Viewer API, create a Viewer class instance. Pass an email message you want to view to the class constructor. You can load the message from a file or stream. Call one of the Viewer.View method overloads to convert the loaded message to HTML, PDF, or image format. These methods allow you to render the entire message or specific pages.

View email files online View demos and examples on GitHub

Supported email file formats

GroupDocs.Viewer supports the following email file formats:

GroupDocs.Viewer can detect the email file format automatically based on information in the file header.

Render email messages as HTML

Create an HtmlViewOptions class instance and pass it to the Viewer.View method to convert an email message to HTML. The HtmlViewOptions class properties allow you to control the conversion process. For instance, you can embed all external resources in the generated HTML file, minify the output file, and exclude specific fonts. Refer to the following documentation section for details: Rendering to HTML.

Create an HTML file with embedded resources

To embed an email message in an HTML page, call the HtmlViewOptions.ForEmbeddedResources method and specify the output file name.

using GroupDocs.Viewer;
using GroupDocs.Viewer.Options;
// ...

using (var viewer = new Viewer("sample.eml"))
{
   // Create an HTML file.
    var viewOptions = HtmlViewOptions.ForEmbeddedResources("output.html");
    viewer.View(viewOptions);
}
Imports GroupDocs.Viewer
Imports GroupDocs.Viewer.Options
' ...

Module Program
    Sub Main(args As String())
        Using viewer = New Viewer("sample.eml")
            ' Create an HTML file.
            Dim viewOptions = HtmlViewOptions.ForEmbeddedResources("output.html")
            viewer.View(viewOptions)
        End Using
    End Sub
End Module

sample.eml is the sample file used in this example. Click here to download it.

HTML file (251 KB)

Download full output

Create an HTML file with external resources

To save an email message to a separate folder, call the HtmlViewOptions.ForExternalResources method and pass the following parameters:

  • The output file path format
  • The path format for the folder with external resources
  • The resource URL format
using GroupDocs.Viewer;
using GroupDocs.Viewer.Options;
// ...

using (var viewer = new Viewer("sample.eml"))
{
    // Specify the HTML file name and location of external resources.
    // {0} is replaced with the page number, {1} with the resource name.
    var viewOptions = HtmlViewOptions.ForExternalResources("page_{0}.html", "page_{0}/resource_{0}_{1}", "page_{0}/resource_{0}_{1}");
    viewer.View(viewOptions);
}
Imports GroupDocs.Viewer
Imports GroupDocs.Viewer.Options
' ...

Module Program
    Sub Main(args As String())
        Using viewer = New Viewer("sample.eml")
            ' Specify the HTML file name and location of external resources.
            ' {0} is replaced with the page number, {1} with the resource name.
            Dim viewOptions = HtmlViewOptions.ForExternalResources("page_{0}.html", "page_{0}/resource_{0}_{1}", "page_{0}/resource_{0}_{1}")
            viewer.View(viewOptions)
        End Using
    End Sub
End Module

sample.eml is the sample file used in this example. Click here to download it.

page_1.html (15 KB)
page_1/resource_1_f.ttf (54 KB)
page_1/resource_1_f1.ttf (36 KB)
page_1/resource_1_f2.ttf (45 KB)
page_1/resource_1_f3.ttf (32 KB)
page_1/resource_1_i.png (9 KB)
page_1/resource_1_s.css (3 KB)

Download full output

External resources are placed in a separate folder.

Render email messages as PDF

Create a PdfViewOptions class instance and pass it to the Viewer.View method to convert an email message to PDF. The PdfViewOptions class properties allow you to control the conversion process. For instance, you can protect the output PDF file, reorder its pages, and specify the quality of document images. Refer to the following documentation section for details: Rendering to PDF.

using GroupDocs.Viewer;
using GroupDocs.Viewer.Options;
// ...

using (var viewer = new Viewer("sample.eml"))
{
    // Create a PDF file.
    var viewOptions = new PdfViewOptions("output.pdf");
    viewer.View(viewOptions);
}
Imports GroupDocs.Viewer
Imports GroupDocs.Viewer.Options
' ...

Module Program
    Sub Main(args As String())
        Using viewer = New Viewer("sample.eml")
            ' Create a PDF file.
            Dim viewOptions = New PdfViewOptions("output.pdf")
            viewer.View(viewOptions)
        End Using
    End Sub
End Module

sample.eml is the sample file used in this example. Click here to download it.

Binary file (PDF, 102 KB)

Download full output

Render email messages as PNG

Create a PngViewOptions class instance and pass it to the Viewer.View method to convert an email message to PNG. Use the PngViewOptions.Height and PngViewOptions.Width properties to specify the output image size in pixels.

using GroupDocs.Viewer;
using GroupDocs.Viewer.Options;
// ...

using (var viewer = new Viewer("sample.eml"))
{
    // Create a PNG image.
    var viewOptions = new PngViewOptions("output_{0}.png");
    // Set width and height.
    viewOptions.Width = 800;
    viewOptions.Height = 1000;
    viewer.View(viewOptions);
}
Imports GroupDocs.Viewer
Imports GroupDocs.Viewer.Options
' ...

Module Program
    Sub Main(args As String())
        Using viewer = New Viewer("sample.eml")
            ' Create a PNG image.
            Dim viewOptions = New PngViewOptions("output_{0}.png")
            ' Set width and height.
            viewOptions.Width = 800
            viewOptions.Height = 1000
            viewer.View(viewOptions)
        End Using
    End Sub
End Module

sample.eml is the sample file used in this example. Click here to download it.

output_1.png (171 KB)
output_2.png (47 KB)

Download full output

Render email messages as JPEG

Create a JpgViewOptions class instance and pass it to the Viewer.View method to convert an email message to JPEG. Use the JpgViewOptions.Height and JpgViewOptions.Width properties to specify the output image size in pixels.

using GroupDocs.Viewer;
using GroupDocs.Viewer.Options;
// ...

using (var viewer = new Viewer("sample.eml"))
{
    // Create a JPG image.
    var viewOptions = new JpgViewOptions("output_{0}.jpg");
    // Set width and height.
    viewOptions.Width = 800;
    viewOptions.Height = 1000;
    viewer.View(viewOptions);
}
Imports GroupDocs.Viewer
Imports GroupDocs.Viewer.Options
' ...

Module Program
    Sub Main(args As String())
        Using viewer = New Viewer("sample.eml")
            ' Create a JPG image.
            Dim viewOptions = New JpgViewOptions("output_{0}.jpg")
            ' Set width and height.
            viewOptions.Width = 800
            viewOptions.Height = 1000
            viewer.View(viewOptions)
        End Using
    End Sub
End Module

sample.eml is the sample file used in this example. Click here to download it.

output_1.jpg (113 KB)
output_2.jpg (35 KB)

Download full output

Specify rendering options

GroupDocs.Viewer supports the EmailOptions class that allows you to specify different options for rendering email messages. To access these options, use the EmailOptions property for one of the following classes (depending on the output file format):

Set the output page size

GroupDocs.Viewer allows you to specify page size for the output file when you convert an email message to HTML, PDF, or image format. Assign a PageSize enumeration member to the EmailOptions.PageSize property to select one of the predefined page sizes (Letter, Ledger, A0, A1, A2, A3, or A4).

The following example specifies page size for the output PDF file:

using GroupDocs.Viewer;
using GroupDocs.Viewer.Options;
// ...

using (var viewer = new Viewer("sample.eml"))
{
    // Create a PDF file.
    var viewOptions = new PdfViewOptions("output.pdf");
    // Specify the page size.
    viewOptions.EmailOptions.PageSize = PageSize.A4;
    viewer.View(viewOptions);
}
Imports GroupDocs.Viewer
Imports GroupDocs.Viewer.Options
' ...

Module Program
    Sub Main(args As String())
        Using viewer = New Viewer("sample.eml")
            ' Create a PDF file.
            Dim viewOptions = New PdfViewOptions("output.pdf")
            ' Specify the page size.
            viewOptions.EmailOptions.PageSize = PageSize.A4
            viewer.View(viewOptions)
        End Using
    End Sub
End Module

sample.eml is the sample file used in this example. Click here to download it.

Binary file (PDF, 102 KB)

Download full output

Rename fields in the message header

GroupDocs.Viewer allows you to change how standard fields (such as From, To, Subject, and so on) are displayed in the email message header.

Default fields in the message header

Use the EmailOptions.FieldTextMap property to specify custom field labels. Static fields of the Field class allow you to access default email header fields, as shown in the example below.

using GroupDocs.Viewer;
using GroupDocs.Viewer.Options;
// ...

using (var viewer = new Viewer("sample.eml"))
{
    // Create an HTML file.
    var viewOptions = HtmlViewOptions.ForEmbeddedResources("output.html");
    // Specify custom field labels.
    viewOptions.EmailOptions.FieldTextMap[Field.From] = "Sender";
    viewOptions.EmailOptions.FieldTextMap[Field.To] = "Recipient";
    viewOptions.EmailOptions.FieldTextMap[Field.Sent] = "Date";
    viewOptions.EmailOptions.FieldTextMap[Field.Subject] = "Email subject";
    viewer.View(viewOptions);
}
Imports GroupDocs.Viewer
Imports GroupDocs.Viewer.Options
' ...

Module Program
    Sub Main(args As String())
        Using viewer = New Viewer("sample.eml")
            ' Create an HTML file.
            Dim viewOptions = HtmlViewOptions.ForEmbeddedResources("output.html")
            ' Specify custom field labels.
            viewOptions.EmailOptions.FieldTextMap(Field.From) = "Sender"
            viewOptions.EmailOptions.FieldTextMap(Field.[To]) = "Recipient"
            viewOptions.EmailOptions.FieldTextMap(Field.Sent) = "Date"
            viewOptions.EmailOptions.FieldTextMap(Field.Subject) = "Email subject"
            viewer.View(viewOptions)
        End Using
    End Sub
End Module

sample.eml is the sample file used in this example. Click here to download it.

HTML file (252 KB)

Download full output

Specify the date and time format and change the time zone

When rendering email messages, GroupDocs.Viewer formats date and time information in the message header based on the system’s date and time settings. If you want to change the default date and time format or specify the time zone offset, use the following properties:

using System;
using GroupDocs.Viewer;
using GroupDocs.Viewer.Options;
// ...

using (var viewer = new Viewer("sample.eml"))
{
    // Create an HTML file.
    var viewOptions = HtmlViewOptions.ForEmbeddedResources("output.html");
    // Apply a custom format to the date in the email message header.
    viewOptions.EmailOptions.DateTimeFormat = "MMMM dd, yyyy H:mm:ss zzz";
    // Specify the time zone offset. 
    viewOptions.EmailOptions.TimeZoneOffset = new TimeSpan(-7, 0, 0);
    viewer.View(viewOptions);
}
Imports System
Imports GroupDocs.Viewer
Imports GroupDocs.Viewer.Options
' ...

Module Program
    Sub Main(args As String())
        Using viewer = New Viewer("sample.eml")
            ' Create an HTML file.
            Dim viewOptions = HtmlViewOptions.ForEmbeddedResources("output.html")
            ' Apply a custom format to the date in the email message header.
            viewOptions.EmailOptions.DateTimeFormat = "MMMM dd, yyyy H:mm:ss zzz"
            ' Specify the time zone offset. 
            viewOptions.EmailOptions.TimeZoneOffset = New TimeSpan(-7, 0, 0)
            viewer.View(viewOptions)
        End Using
    End Sub
End Module

sample.eml is the sample file used in this example. Click here to download it.

HTML file (250 KB)

Download full output

Get mail message metadata before rendering

Sometimes it is necessary to know some metadata of the email message, loaded to the Viewer class, before its actual rendering through the Viewer.View() method. GroupDocs.Viewer for such cases provides the ViewInfo class that can be obtained by calling a Viewer.GetViewInfo() method. However, this ViewInfo class is common for all supported document formats and has no specific properties, which are present only in the mail messages.

Starting from the version 26.1 the GroupDocs.Viewer public API has a new class MailMessageViewInfo. This class is a direct inheritor of the common class ViewInfo, so it has all its properties, but also introduce the few new ones, specific for the mail messages:

  • Sent property of DateTime type — returns a date and time, stored in the mail message in its original form “as-is”, without adjusting it to the local date time and so on.
  • Subject of String type — return a subject of the mail message.
  • From of String type — returns a “From” email address.

To obtain a metadata of the given mail message, this mail message initially should be loaded to the Viewer class through its constructor, and then the Viewer.GetViewInfo() method should be called. This method then returns the instance of the ViewInfo class, which should be casted to the MailMessageViewInfo type. And that’s all! Source code is below:

using System;
using GroupDocs.Viewer;
using GroupDocs.Viewer.Options;
using GroupDocs.Viewer.Results;
// ...

using (Viewer viewer = new Viewer("sample.eml"))
{
    ViewInfo generalInfo = viewer.GetViewInfo(ViewInfoOptions.ForHtmlView());
    // explicit casting
    MailMessageViewInfo mailMessageInfo = (MailMessageViewInfo)generalInfo;
    Console.WriteLine("From: {0}; Subject: {1}; Sent: {2}.", mailMessageInfo.From, mailMessageInfo.Subject, mailMessageInfo.Sent);

    // Render to any output format if needed
    viewer.View(HtmlViewOptions.ForEmbeddedResources());
}
Imports System
Imports GroupDocs.Viewer
Imports GroupDocs.Viewer.Options
Imports GroupDocs.Viewer.Results
' ...

Using viewer As New Viewer("sample.eml")
    Dim generalInfo As ViewInfo = viewer.GetViewInfo(ViewInfoOptions.ForHtmlView())
    ' explicit casting
    Dim mailMessageInfo As MailMessageViewInfo = DirectCast(generalInfo, MailMessageViewInfo)
    Console.WriteLine("From: {0}; Subject: {1}; Sent: {2}.", mailMessageInfo.From, mailMessageInfo.Subject, mailMessageInfo.Sent)

    ' Render to any output format if needed
    viewer.View(HtmlViewOptions.ForEmbeddedResources())
End Using

sample.eml is the sample file used in this example. Click here to download it.

HTML file (251 KB)

Download full output

Email messages rendering demo

The View EML files online app is available to test and preview rendering output for email messages and other document formats.