Load Email in C# and VB.NET

The following example shows how to load an email message from a file and display general message properties on console window.

Screenshot

Load Screenshot

See the full code below, use RUN EXAMPLE to execute.

Upload your file (Drag files here)

1using GemBox.Email;
2using GemBox.Email.Imap;
3using GemBox.Email.Mime;
4using GemBox.Email.Pop;
5using GemBox.Email.Smtp;
6using System;
7using System.Collections.Generic;
8using System.Globalization;
9using System.Text;
10
11class Sample
12{
13    [STAThread]
14    static void Main(string[] args)
15    {
16        // If using Professional version, put your serial key below.
17        ComponentInfo.SetLicense("FREE-LIMITED-KEY");
18
19        // Load message
20        MailMessage message = MailMessage.Load("Html.eml");
21        StringBuilder sb = new StringBuilder();
22
23        // Add date
24        sb.AppendLine(string.Format("Date: {0}", message.Date));
25
26        // Add addresses
27        sb.AppendLine(string.Format("From: {0}", message.From.ToString()));
28        sb.AppendLine(string.Format("To: {0}", message.To.ToString()));
29
30        if (message.Cc.Count > 0)
31            sb.AppendLine(string.Format("Cc: {0}", message.Cc.ToString()));
32
33        if (message.Bcc.Count > 0)
34            sb.AppendLine(string.Format("Bcc: {0}", message.Bcc.ToString()));
35
36        if (message.Attachments.Count > 0)
37            sb.AppendLine(string.Format("Attachments: {0}", message.Attachments.Count.ToString()));
38
39        // Add subject
40        sb.AppendLine(string.Format("Subject: {0}", message.Subject));
41
42        // Add message body
43        sb.AppendLine("------------------------------ BODY ------------------------------");
44        if (string.IsNullOrEmpty(message.BodyHtml))
45            sb.Append(message.BodyText);
46        else
47            sb.Append(message.BodyHtml);
48
49        Console.WriteLine(sb.ToString());
50    }
51}
1Imports System.Globalization
2Imports System.Text
3Imports GemBox.Email
4Imports GemBox.Email.Imap
5Imports GemBox.Email.Mime
6Imports GemBox.Email.Pop
7Imports GemBox.Email.Smtp
8
9Module Samples
10
11    Sub Main()
12
13        ' If using Professional version, put your serial key below.
14        ComponentInfo.SetLicense("FREE-LIMITED-KEY")
15
16        ' Load message
17        Dim message as MailMessage = MailMessage.Load("Html.eml")
18        Dim sb As New StringBuilder()
19
20        ' Add date
21        sb.AppendLine(String.Format("Date: {0}", message.Date))
22
23        ' Add addresses
24        sb.AppendLine(String.Format("From: {0}", message.From.ToString()))
25        sb.AppendLine(String.Format("To: {0}", message.To.ToString()))
26
27        If message.Cc.Count > 0 Then
28            sb.AppendLine(String.Format("From: {0}", message.Cc.ToString()))
29        End If
30
31        If message.Bcc.Count > 0 Then
32            sb.AppendLine(String.Format("From: {0}", message.Bcc.ToString()))
33        End If
34
35        If message.Attachments.Count > 0 Then
36            sb.AppendLine(String.Format("Attachments: {0}", message.Attachments.Count.ToString()))
37        End If
38
39        ' Add subject
40        sb.AppendLine(String.Format("Subject: {0}", message.Subject))
41
42        ' Add message body
43        sb.AppendLine("------------------------------ BODY ------------------------------")
44        If String.IsNullOrEmpty(message.BodyHtml) Then
45            sb.Append(message.BodyText)
46        Else
47            sb.Append(message.BodyHtml)
48        End If
49
50        Console.WriteLine(sb.ToString())
51
52    End Sub
53
54End Module

Check next sample or find out more about GemBox.Email and GemBox Software.