1. Introduction
GroupDocs.Viewer for .NET تسمح لك باستيراد رسالة البريد الإلكتروني بأي تنسيق شائع، وتحويلها إلى HTML أو PNG أو JPEG أو PDF، وكذلك استخراج خصائص الرسالة الأساسية دون الحاجة إلى عرضها فعليًا.
في الواقع، عند العمل مع رسائل البريد الإلكتروني في GroupDocs.Viewer، يجب أن يكون المستخدمون على علم بنوعين خاصين بالبريد الإلكتروني:
- فئة
Options.EmailOptions– تتيح لك التحكم في طريقة عرض الرسالة (حجم الصفحة، تنسيق التاريخ، الحقول، إلخ). - فئة
Results.MailMessageViewInfo— يتم إنشاؤها بواسطة GroupDocs.Viewer بعد استدعاء طريقةGetViewInfo()وتحتوي على البيانات الوصفية للرسالة البريدية التي تم تحميلها.
فيما يلي دليل سريع يوضح كيفية عرض رسائل البريد الإلكتروني والحصول على بياناتها الوصفية.
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 instantiate an EmailOptions object and assign it to the view options via the EmailOptions property. All this is already done; the 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 |
الوقت الدقيق الذي تم إرسال البريد فيه. |
Subject |
string |
النص الموجود في سطر موضوع البريد. |
From |
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.