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

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);
}

The following image demonstrates the result:

Render an email message to HTML

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 resource name in the output file name.
    var viewOptions = HtmlViewOptions.ForExternalResources("output.html", "output/resource_{0}", "output/resource_{0}");
    viewer.View(viewOptions);
}

The result is shown below. External resources are placed in a separate folder.

Place HTML resources 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);
}

The following image demonstrates the result:

Render an email message to PDF

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.png");
    // Set width and height.
    viewOptions.Width = 800;
    viewOptions.Height = 1000;
    viewer.View(viewOptions);
}

The following image demonstrates the result:

Render an email message to PNG

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.jpg");
    // Set width and height.
    viewOptions.Width = 800;
    viewOptions.Height = 1000;
    viewer.View(viewOptions);
}

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.Letter;
    viewer.View(viewOptions);
}

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);
}

The following image illustrates the result:

Custom fields in the message header

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 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);
}

The following image illustrates the result:

Custom date-time format