Load and Save an Mbox Message Collection in C# and VB.NET

GemBox.Email provides an advanced API for working with the Mbox (Mboxrd) file format from your C# or VB.NET application. Mbox files are a collection of email messages, and GemBox.Email represents that collection with the MailMessageCollection object.

Besides basic features like loading (via MailMessageCollection.Load methods), modifying and saving (via MailMessageCollection.Save methods), different methods can be used for loading, which speeds up the process.

For example, you can use MailMessageCollection.LazyLoad for lazy message iteration or MailMessageCollection.LoadHeaders for reading just message headers.

The following example shows how mail messages can be loaded, modified and then saved to an Mbox file.

Screenshot of loaded emails from MBox collection
Working with MBox files in C# and VB.NET
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 collection.
        MailMessageCollection messages = MailMessageCollection.Load("%InputFileName%");

        // Display message information.
        foreach (MailMessage message in messages)
        {
            Console.WriteLine($"From: {message.From}");
            Console.WriteLine($"Subject: {message.Subject}");
            Console.WriteLine($"Body: {message.BodyText}");
            Console.WriteLine();
        }

        // Create new message.
        MailMessage newMessage = new MailMessage("sender@example.com", "receiver@example.com")
        {
            Subject = "Test email message with a text body",
            BodyText = "This is a test message with a text body."
        };

        // Add message to collection.
        messages.Add(newMessage);

        // Save message collection.
        messages.Save("Modified Collection.mbox");
    }
}
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 collection.
        Dim messages As MailMessageCollection = MailMessageCollection.Load("%InputFileName%")

        ' Display message information.
        For Each message As MailMessage In messages
            Console.WriteLine($"From: {message.From}")
            Console.WriteLine($"Subject: {message.Subject}")
            Console.WriteLine($"Body: {message.BodyText}")
            Console.WriteLine()
        Next

        ' Create new message.
        Dim newMessage As New MailMessage("sender@example.com", "receiver@example.com") With
        {
            .Subject = "Test email message with a text body",
            .BodyText = "This is a test message with a text body."
        }

        ' Add message to the collection
        messages.Add(newMessage)

        ' Save message collection
        messages.Save("Modified Collection.mbox")

    End Sub
End Module

Check next example or download examples from GitHub.