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 you can load, modify and then save mail messages to an 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.

        // 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}");

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

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

        ' 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}")

        ' 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

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

    End Sub
End Module

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.