Save Email in C# and VB.NET

Besides sending an email to a server, GemBox.Email also allows you to save an email message programmatically to a physical file when providing a file's path, or to an in-memory file when providing a file’s stream. For this it is necessary to use one of the MailMessage.Save methods. You can choose to save the email message in EML or MSG format.

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

Also, note that you can save an email attachment to a file or a stream using the Attachment.Save methods.

The following example shows how you can create a ZIP file that contains email attachments, each attachment is saved into the ZIP archive stream.

Save email attachments in C# and VB.NET
Screenshot of saved email attachments
Upload your file (Drag file here)
using System.IO;
using System.IO.Compression;
using GemBox.Email;

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

        var message = MailMessage.Load("%InputFileName%");
        string zipName = Path.ChangeExtension("%InputFileName%", ".zip");

        using (var archiveStream = File.OpenWrite(zipName))
        using (var archive = new ZipArchive(archiveStream, ZipArchiveMode.Create))
        {
            foreach (var attachment in message.Attachments)
            {
                string attachmentName = attachment.FileName ?? Path.GetRandomFileName();
                var entry = archive.CreateEntry(attachmentName);

                using (var entryStream = entry.Open())
                    attachment.Save(entryStream);
            }
        }
    }
}
Imports System.IO
Imports System.IO.Compression
Imports GemBox.Email

Module Program

    Sub Main()

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

        Dim message = MailMessage.Load("%InputFileName%")
        Dim zipName As String = Path.ChangeExtension("%InputFileName%", ".zip")

        Using archiveStream = File.OpenWrite(zipName)
            Using archive = New ZipArchive(archiveStream, ZipArchiveMode.Create)

                For Each attachment In message.Attachments
                    Dim attachmentName As String = If(attachment.FileName, Path.GetRandomFileName())
                    Dim entry = archive.CreateEntry(attachmentName)

                    Using entryStream = entry.Open()
                        attachment.Save(entryStream)
                    End Using
                Next

            End Using
        End Using

    End Sub
End Module

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