1. Introduction

GroupDocs.Viewer for .NET permite importar a mensagem de e‑mail em qualquer formato popular, renderizá‑la para HTML, PNG, JPEG e PDF, e também extrair propriedades básicas da mensagem de e‑mail sem necessidade de renderização real.

Na prática, ao trabalhar com mensagens de e‑mail no GroupDocs.Viewer, os usuários devem estar cientes de dois tipos específicos de e‑mail:

  • classe Options.EmailOptions – permite controlar como a mensagem será renderizada (tamanho da página, formato de data, campos, etc.).
  • Results.MailMessageViewInfo — é gerado pelo GroupDocs.Viewer após a chamada ao método GetViewInfo() e contém os metadados da mensagem de e‑mail carregada.

A seguir, um guia rápido que demonstra como renderizar mensagens de e‑mail e obter seus metadados.


2. Load an email message and render it to all output formats

Very shord code example below shows loading an input email message “sample.eml” to the instance of the Viewer class and then rendering it to all 4 supported output formats: HTML, PDF, PNG, and JPEG. As you can see, at the beginning all 4 view option classes are instantiated, then “sample.eml” is loaded to the Viewer instance by its file path, and then 4 Viewer.View() calls are made.

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


HtmlViewOptions htmlOptions = HtmlViewOptions.ForEmbeddedResources("html-page{0}.html");
PdfViewOptions pdfOptions = new PdfViewOptions("Output.pdf");
PngViewOptions pngOptions = new PngViewOptions("image-page{0}.png");
JpgViewOptions jpegOptions = new JpgViewOptions("image-page{0}.jpeg");

using (Viewer viewer = new Viewer("sample.eml"))
{
    viewer.View(htmlOptions);
    viewer.View(pdfOptions);
    viewer.View(pngOptions);
    viewer.View(jpegOptions);
}

3. Tuning email options for rendering

Example above shows rendering with all options are set to their default values. However, there is an EmailOptions class, and it is available in all 4 view options (HtmlViewOptions, PdfViewOptions, PngViewOptions, JpgViewOptions) through the EmailOptions property. There is no need to manually create an insatnce of the EmailOptions class and assign it to the view options through the EmailOptions property — all this is already done, user just needs to adjust the property values if needed.

There are several properties in the EmailOptions class, explained below.

3.1. Page size

Controls the size of the output page. By default is Unspecified — the GroupDocs.Viewer adjusts the size to the most appropriate. Can be set in the next way, for example:

PdfViewOptions pdfOptions = new PdfViewOptions("Output.pdf");
pdfOptions.EmailOptions.PageSize = PageSize.A1;

3.2. Date and time format and time zone

Email messages usually have a “Sent” date. The DateTimeFormat property allows to control how this datetime value will be formatted, and TimeZoneOffset allows to apply a positive or negative shift to this datetime. Example below:

JpgViewOptions jpegOptions = new JpgViewOptions("image-page{0}.jpeg");
jpegOptions.EmailOptions.DateTimeFormat = "MM d yyyy HH:mm tt zzz"; // completely custom format
jpegOptions.EmailOptions.TimeZoneOffset = TimeSpan.FromHours(-5); // Add shift to 5 hours earlier

If you leave DateTimeFormat null or empty string, the GroupDocs.Viewer uses the system’s current format.

3.3. Adjusting fields

Mail message contains message body and fields like subject, sent datetime, sender etc. GroupDocs.Viewer allows users to override the labels of these fields through the FieldTextMap property, which maps a Field enum value (e.g., Field.Subject) to the text that appears in the rendered output. So the user can replace any entry or add new ones:

PngViewOptions pngOptions = new PngViewOptions("image-page{0}.png");
pngOptions.EmailOptions.FieldTextMap[Field.Subject] = "Email Subject";
pngOptions.EmailOptions.FieldTextMap[Field.Sent] = "Mail was sent at";
pngOptions.EmailOptions.FieldTextMap[Field.From] = "Sender";

4. Load an email and get its view information

It is possible to get some of the field values of the email message programmatically without rendering the email message itself. For doing that user should load the email message document to the Viewer instance and then use the Viewer.GetViewInfo() instance method, as shown 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());
}

It can be seen that Viewer.GetViewInfo() method returns an instance of the common ViewInfo class, which then is casted to the MailMessageViewInfo class. For now only the From, Subject, and Sent fields are supported.

Property Type Meaning
Sent DateTime The exact time the email was sent.
Subject string The text that appears in the email’s subject line.
From string The sender’s email address as a plain string.

All three properties are read-only.

5. Summary

  • Create a Viewer instance and specify the input email message file to its constructor by path or stream.
  • Call the Viewer.GetViewInfo method and then cast its result to the MailMessageViewInfo type to get metadata: Sent, Subject, and From fields.
  • Use EmailOptions to change page size, date format, time‑zone offset, or the text label shown for any field, then call the Viewer.View() method.

These steps give you the core email properties and let you control how the message appears when rendered. No extra libraries or complex code are required.

See Also

Get a free trial

You can download a free trial version of GroupDocs.Viewer for .NET from releases.groupdocs.com. You can also acquire a temporary license to try all features and functionalities without restrictions from here.