Save Email in C# and VB.NET

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

The following example shows how you can create a MailMessage object and save it to the desired format by using C# or VB.NET code.

Save email in C# and VB.NET
Screenshot of loaded email's information
using GemBox.Email;

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

        // Create new message.
        MailMessage message = new MailMessage(
            new MailAddress("sender@example.com", "Sender"),
            new MailAddress("receiver@example.com", "Receiver"));

        // Add subject and body.
        message.Subject = "Save Example by GemBox.Email";
        message.BodyText = "Hi 👋,\n" +
            "This message was created and saved with GemBox.Email.\n" +
            "Read more about it on https://www.gemboxsoftware.com/email";

        // Save message to email file.
        message.Save("Save.%OutputFileType%");
    }
}
Imports GemBox.Email

Module Program

    Sub Main()

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

        ' Create new message.
        Dim message As New MailMessage(
            New MailAddress("sender@example.com", "Sender"),
            New MailAddress("receiver@example.com", "Receiver"))

        ' Add subject and body.
        message.Subject = "Save Example by GemBox.Email"
        message.BodyText = "Hi 👋," & vbLf &
            "This message was created and saved with GemBox.Email." & vbLf &
            "Read more about it on https://www.gemboxsoftware.com/email"

        ' Save message to email file.
        message.Save("Save.%OutputFileType%")
    End Sub
End Module

When using the MailMessage.Save(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.