Load Email in C# and VB.NET

Besides retrieving an email from a server, GemBox.Email can also enable you to load an email from a physical file (when providing a file's path) or an in-memory file (when providing a file's stream) by using MailMessage.Load overload methods.

The following example shows how you can load a MailMessage object from a file and display its properties on your Console application by using C# or VB.NET code.

Load email in C# and VB.NET
Screenshot of loaded email's information
Upload your file (Drag file here)
using System;
using GemBox.Email;

class Program
{
    static void Main()
    {
        // If using Professional version, put your serial key below.
        ComponentInfo.SetLicense("FREE-LIMITED-KEY");

        // Load message from email file.
        MailMessage message = MailMessage.Load("%InputFileName%");

        // Read message information.
        Console.WriteLine($"Date: {message.Date}");
        Console.WriteLine($"Subject: {message.Subject}");
        Console.WriteLine($"From: {message.From}");
        Console.WriteLine($"To: {message.To}");

        if (message.Cc.Count > 0)
            Console.WriteLine($"Cc: {message.Cc}");

        if (message.Bcc.Count > 0)
            Console.WriteLine($"Bcc: {message.Bcc}");

        if (message.Attachments.Count > 0)
            Console.WriteLine($"Attachments: {message.Attachments.Count}");

        Console.WriteLine();
        if (string.IsNullOrEmpty(message.BodyHtml))
            Console.WriteLine(message.BodyText);
        else
            Console.WriteLine(message.BodyHtml);
    }
}
Imports System
Imports GemBox.Email

Module Program

    Sub Main()

        ' If using Professional version, put your serial key below.
        ComponentInfo.SetLicense("FREE-LIMITED-KEY")

        ' Load message from email file.
        Dim message As MailMessage = MailMessage.Load("%InputFileName%")

        ' Read message information.
        Console.WriteLine($"Date: {message.Date}")
        Console.WriteLine($"Subject: {message.Subject}")
        Console.WriteLine($"From: {message.From}")
        Console.WriteLine($"To: {message.To}")

        If (message.Cc.Count > 0) Then
            Console.WriteLine($"Cc: {message.Cc}")
        End If

        If (message.Bcc.Count > 0) Then
            Console.WriteLine($"Bcc: {message.Bcc}")
        End If

        If (message.Attachments.Count > 0) Then
            Console.WriteLine($"Attachments: {message.Attachments.Count}")
        End If

        Console.WriteLine()
        If (String.IsNullOrEmpty(message.BodyHtml)) Then
            Console.WriteLine(message.BodyText)
        Else
            Console.WriteLine(message.BodyHtml)
        End If

    End Sub
End Module

When using the MailMessage.Load(String) method, GemBox.Email will select the required MailMessageFormat based on the file's extension.

So, for a file named Example.msg, it will use MailMessageFormat.Msg and for a file named Example.eml it will use MailMessageFormat.Eml.

Want more?

Next example GitHub

Check the next example or select an example from the menu. You can also download our examples from the GitHub.


Like it?

Download Buy

If you want to try the GemBox.Email yourself, you can download the free version. It delivers the same performance and set of features as the professional version, but with some operations limited. To remove the limitation, you need to purchase a license.