1. Introduction
GroupDocs.Viewer for .NET library lets you import the email message in any popular format, render it to the HTML, PNG, JPEG, and PDF, and also extract basic email message properties without actual rendering.
Actually, when working with email messages in GroupDocs.Viewer, users should be aware about two email-specific types:
Options.EmailOptionsclass – lets you control how the message is rendered (page size, date format, fields, etc.).Results.MailMessageViewInfoclass — is produced by GroupDocs.Viewer after calling aGetViewInfo()method and holds the metadata about the loaded email message.
Below is a very quick guide that shows how to render email messages and get their metadata.
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
Enum PageSize 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, then 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
Viewerinstance and specify the input email message file to its constructor by path or stream. - Call the
Viewer.GetViewInfomethod and then cast its result to theMailMessageViewInfotype to get metadata:Sent,Subject, andFromfields. - Use
EmailOptionsto change page size, date format, time-zone offset, or the text label shown for any field, then call theViewer.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.