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

Mbox (Mboxrd) files are a collection of email messages, which are concatenated and stored as plain text in a single file.

GemBox.Email provides an advanced API for working with the Mbox File format from your C# or VB.NET application. The API represents the collection of email messages with the MailMessageCollection object.

Besides basic features like loading (via MailMessageCollection.Load methods), modifying and saving (via MailMessageCollection.Save methods), you can use different methods 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 example below shows how to load, modify, and save email messages to a Mbox file.

Working with MBox files in C# and VB.NET
Screenshot of loaded emails from MBox collection
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

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