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
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.

See also


Next steps

GemBox.Email is a .NET component that enables you to read, write, receive, and send emails from your .NET applications using one simple API.

Download Buy