Skip to content

Commit 1393b80

Browse files
VIEWERNET-5608 - Return mail message properties through specialized view info - Added new section to the article
1 parent 21a2887 commit 1393b80

1 file changed

Lines changed: 54 additions & 0 deletions

File tree

net/rendering-basics/render-email-messages.md

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -426,3 +426,57 @@ End Module
426426
The following image illustrates the result:
427427

428428
![Custom date-time format](/viewer/net/images/rendering-basics/render-email-messages/custom-date-time-format.png)
429+
430+
## Get mail message metadata before rendering
431+
432+
Sometimes it is necessary to know some metadata of the email message, loaded to the [`Viewer`](https://reference.groupdocs.com/viewer/net/groupdocs.viewer/viewer/) class, before its actual rendering through the [`Viewer.View()`](https://reference.groupdocs.com/viewer/net/groupdocs.viewer/viewer/view/#view) method. GroupDocs.Viewer for such cases provides the [`ViewInfo`](https://reference.groupdocs.com/viewer/net/groupdocs.viewer.results/viewinfo/) class that can be obtained by calling a [`Viewer.GetViewInfo()`](https://reference.groupdocs.com/viewer/net/groupdocs.viewer/viewer/getviewinfo/#getviewinfo) method. However, this [`ViewInfo`](https://reference.groupdocs.com/viewer/net/groupdocs.viewer.results/viewinfo/) class is common for all supported document formats and has no specific properties, which are present only in the mail messages.
433+
434+
Starting from the version 26.1 the GroupDocs.Viewer public API has a new class [`MailMessageViewInfo`](https://reference.groupdocs.com/viewer/net/groupdocs.viewer.results/mailmessageviewinfo/). This class is a direct inheritor of the common class [`ViewInfo`](https://reference.groupdocs.com/viewer/net/groupdocs.viewer.results/viewinfo/), so it has all its properties, but also introduce the few new ones, specific for the mail messages:
435+
436+
- `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.
437+
- `Subject` of `String` type — return a subject of the mail message.
438+
- `From` of `String` type — returns a “From” email address.
439+
440+
To obtain a metadata of the given mail message, this mail message initially should be loaded to the [`Viewer`](https://reference.groupdocs.com/viewer/net/groupdocs.viewer/viewer/) class through its [constructor](https://reference.groupdocs.com/viewer/net/groupdocs.viewer/viewer/viewer/#constructor_4), and then the [`Viewer.GetViewInfo()`](https://reference.groupdocs.com/viewer/net/groupdocs.viewer/viewer/getviewinfo/#getviewinfo) method should be called. This method then returns the instance of the [`ViewInfo`](https://reference.groupdocs.com/viewer/net/groupdocs.viewer.results/viewinfo/) class, which should be casted to the [`MailMessageViewInfo`](https://reference.groupdocs.com/viewer/net/groupdocs.viewer.results/mailmessageviewinfo/) type. And that’s all! Source code is below:
441+
442+
{{< tabs "Get-view-info-example">}}
443+
{{< tab "C#" >}}
444+
```csharp
445+
using System;
446+
using GroupDocs.Viewer;
447+
using GroupDocs.Viewer.Options;
448+
using GroupDocs.Viewer.Results;
449+
// ...
450+
451+
using (Viewer viewer = new Viewer("sample.eml"))
452+
{
453+
ViewInfo generalInfo = viewer.GetViewInfo(ViewInfoOptions.ForHtmlView());
454+
// explicit casting
455+
MailMessageViewInfo mailMessageInfo = (MailMessageViewInfo)generalInfo;
456+
Console.WriteLine("From: {0}; Subject: {1}; Sent: {2}.", mailMessageInfo.From, mailMessageInfo.Subject, mailMessageInfo.Sent);
457+
458+
// Render to any output format if needed
459+
viewer.View(HtmlViewOptions.ForEmbeddedResources());
460+
}
461+
```
462+
{{< /tab >}}
463+
{{< tab "VB.NET">}}
464+
```vb
465+
Imports System
466+
Imports GroupDocs.Viewer
467+
Imports GroupDocs.Viewer.Options
468+
Imports GroupDocs.Viewer.Results
469+
' ...
470+
471+
Using viewer As New Viewer("sample.eml")
472+
Dim generalInfo As ViewInfo = viewer.GetViewInfo(ViewInfoOptions.ForHtmlView())
473+
' explicit casting
474+
Dim mailMessageInfo As MailMessageViewInfo = DirectCast(generalInfo, MailMessageViewInfo)
475+
Console.WriteLine("From: {0}; Subject: {1}; Sent: {2}.", mailMessageInfo.From, mailMessageInfo.Subject, mailMessageInfo.Sent)
476+
477+
' Render to any output format if needed
478+
viewer.View(HtmlViewOptions.ForEmbeddedResources())
479+
End Using
480+
```
481+
{{< /tab >}}
482+
{{< /tabs >}}

0 commit comments

Comments
 (0)