---
title: "Write EML messages to MBOX in C#"
url: https://recursiveai.org/onemail/cookbook/write-eml-messages-to-mbox/
description: "Combine separate EML files into one MBOX archive with OneMail in C#. Write each message, reopen the archive, and check a body line starting with From."
version: "1.0.1"
---

# Write EML messages to MBOX in C#

**OneMail** is a free, open-source email library for .NET from Recursive AI. MIT or Apache-2.0, no native dependencies.

[NuGet](https://www.nuget.org/packages/RecursiveAI.OneMail) · [GitHub](https://github.com/recursive-ai-oss/onemail-csharp) · [Docs](https://recursiveai.org/onemail/docs/) · [Download example (.zip)](https://recursiveai.org/onemail/cookbook/write-eml-messages-to-mbox/write-eml-messages-to-mbox.zip)

Pack `a.eml` and `b.eml` from the zip into one mailbox.

```shell
dotnet add package RecursiveAI.OneMail
```

Requires [.NET 10](https://dotnet.microsoft.com/download/dotnet/10.0) (net10.0); .NET 9 and earlier aren’t supported.

```csharp
using RecursiveAI.OneMail.Stores.Mbox;
using var output = File.Create("mail.mbox");
var writer = new MboxWriter(output);
writer.Append(File.ReadAllBytes("a.eml"));
writer.Append(File.ReadAllBytes("b.eml"));
```

Unzip [the example](https://recursiveai.org/onemail/cookbook/write-eml-messages-to-mbox/write-eml-messages-to-mbox.zip), then `dotnet run`.

Writes or replaces `mail.mbox` with both messages.

## The full program

The example contains two fictional EML files. `a.eml` contains:

```text
From: Ada <ada@example.test>
Date: Thu, 19 Nov 2026 09:00:00 +0000
Subject: Arrival

From the platform, turn left.
```

`b.eml` contains:

```text
From: Ben <ben@example.test>
Date: Thu, 19 Nov 2026 09:05:00 +0000
Subject: Re: Arrival

See you there.
```

The program sets fixed envelope senders and UTC dates, closes the output, then reopens it to check both subjects and the first message’s text.

```csharp
using RecursiveAI.OneMail;
using RecursiveAI.OneMail.Stores.Mbox;

using (var output = File.Create("mail.mbox"))
{
    var writer = new MboxWriter(output);
    var date = new DateTimeOffset(2026, 11, 19, 9, 0, 0, TimeSpan.Zero);
    writer.Append(File.ReadAllBytes("a.eml"), "ada@example.test", date);
    writer.Append(File.ReadAllBytes("b.eml"), "ben@example.test", date.AddMinutes(5));
}

using var mailbox = Mail.Open("mail.mbox");
var messages = mailbox.Enumerate().ToArray();
if (messages.Length != 2 || messages[0].Subject != "Arrival" || messages[1].Subject != "Re: Arrival")
    throw new InvalidDataException("Unexpected messages in the mailbox.");

const string expectedLine = "From the platform, turn left.";
var body = messages[0].TextBody?.ReadText();
if (body?.TrimEnd('\r', '\n') != expectedLine)
    throw new InvalidDataException("The From line did not round-trip.");

foreach (var message in messages)
    Console.WriteLine($"Subject: {message.Subject}");
Console.WriteLine($"Body: {expectedLine}");
```

`dotnet run` prints:

```text
Subject: Arrival
Subject: Re: Arrival
Body: From the platform, turn left.
```

If an input file is missing, the read fails after `File.Create` has opened the output. Use a new path if you need to keep an existing archive.

## Variations

**Append to an existing archive.** Run the snippet at the top first to create `mail.mbox`. Then replace that entire snippet with the one below. Both use `mboxrd`, a variant that escapes body lines beginning with `From ` so they aren’t mistaken for message separators.

```csharp
using RecursiveAI.OneMail.Stores.Mbox;
using var stream = File.Open("mail.mbox", FileMode.Open,
    FileAccess.ReadWrite);
var options = new MboxWriterOptions { AppendToExisting = true };
var append = new MboxWriter(stream, options);
append.Append(File.ReadAllBytes("a.eml"));
```

This adds a third record, another copy of `a.eml`. `AppendToExisting` lets the writer add a missing separator before the new record.

## Related recipes

[Read an MBOX file](https://recursiveai.org/onemail/cookbook/read-mbox/) · [Parse an EML file](https://recursiveai.org/onemail/cookbook/parse-eml/)

---

## The short answer

Create an output stream and call `MboxWriter.Append` once for each `.eml` file. Pass the entire file, including its headers and body, not just the body. The writer adds a mailbox separator and handles body lines that could be mistaken for separators. The input file order becomes the message order in the archive.

## How do I convert multiple EML files into one MBOX file?

An EML file holds one email. MBOX puts several messages into a single stream and marks each one with an envelope line. If you concatenate two EML files without adding these lines, a reader has no MBOX record boundaries. The two `Append` calls in the snippet at the top do the packing; they don’t require you to parse or recreate each message first.

The writer’s envelope is a line like `From ada@example.test Thu Nov 19 09:00:00 2026`. It isn’t the `From:` header inside the email. The full program gives each call its own sender and UTC date so the envelope is repeatable. If you omit those arguments, the writer uses `MAILER-DAEMON` and the current time, not the sender and date in the message’s headers. See [OneMail’s mailbox writing rules](https://recursiveai.org/onemail/docs/mailboxes/#writing).

## How do I create an MBOX file without splitting a message at a From line?

In `mboxrd`, the writer’s default, the first message’s `From the platform, turn left.` becomes `>From the platform, turn left.` on disk. Reading the archive restores the original line. If the original had already started with `>From `, the writer would add another `>`; the reader removes one, not all of them. That distinction lets the reader recover the original prefix. The [variant table](https://recursiveai.org/onemail/docs/mailboxes/) also explains the variants that use `Content-Length` instead of relying on quoting alone. The writer refuses headers larger than 1 MiB for those length-based variants.

Use the default for this example. If the program that will import the result requires a specific variant, check its requirements before writing or extending an archive. Set the writer to match that variant; the append option adjusts the separator at the end, not the format of the records already stored there.

## How do I check an MBOX file after creating it?

Close the output first and reopen it as a mailbox, not as a single email. The full program checks for exactly two messages, compares each subject in order, and reads back the body line that starts with `From`. Checking only the file’s existence would miss both a broken boundary and a stray `>` showing up in the decoded text.

The archive can change how the message is stored, including adding a final line ending when needed. If you must keep an exact copy of each source file, keep the original EML files separately.
