---
title: "Read an mbox file in C#"
url: https://recursiveai.org/onemail/cookbook/read-mbox/
description: "Read an mbox file in C# message by message: sender, date, subject and body. mboxo, mboxrd, mboxcl, mboxcl2 and Gmail Takeout exports. Runnable code with OneMail 0.3.0-preview.2."
version: "0.3.0-preview.2"
---

# Read an mbox file in C#

`Mail.Open` recognizes an mbox file from its content and `Enumerate()` returns its messages one by one. Each is an `IMailItem`, as from an `.eml` file. The program below reads a public mbox from Apache Tika’s tests: three posts from a Hadoop mailing list.

```shell
dotnet new console -o ReadMbox && cd ReadMbox
dotnet add package RecursiveAI.OneMail --version 0.3.0-preview.2
curl -LO https://raw.githubusercontent.com/apache/tika/4.2.0/tika-parsers/tika-parsers-standard/tika-parsers-standard-modules/tika-parser-mail-module/src/test/resources/test-documents/complex.mbox
```

Put this in `Program.cs` and run `dotnet run`:

```csharp
using RecursiveAI.OneMail;

// mboxo, mboxrd, mboxcl and mboxcl2 are told apart by content
using MailContainer mbox = Mail.Open("complex.mbox");
Console.WriteLine($"Format: {mbox.Format}");

foreach (IMailItem msg in mbox.Enumerate())
{
    Console.WriteLine($"{msg.Sent?.Value:u}  {msg.From.FirstOrDefault().Address}");
    Console.WriteLine($"  {msg.Subject}");
    string text = msg.TextBody?.ReadText() ?? "";
    string first = text.Split('\n').Select(l => l.Trim()).First(l => l != "");
    if (first.Length > 40) first = first[..40] + "…";
    Console.WriteLine($"  {text.Length} characters: {first}");
}
```

```text
Format: Mbox
2009-06-01 04:26:31Z  jothipn@yahoo-inc.com
  Re: question about when shuffle/sort start working
  621 characters: When a Mapper completes, MapCompletionEv…
2009-06-01 05:30:49Z  jianmin_woo@yahoo.com
  Re: question about when shuffle/sort start working
  1259 characters: Thanks a lot for your explanation, Jothi…
2009-06-01 06:03:13Z  jothipn@yahoo-inc.com
  Re: question about when shuffle/sort start working
  1732 characters: No you cannot raise this event yourself,…
```

The four mbox variants differ in how a `From ` line inside a body is escaped and whether lengths are stored. OneMail tells them apart and undoes the escaping, so bodies come back as written. A Gmail Takeout export is an mbox too, and `MboxMetadata` reads its `X-Gmail-Labels`. Use `await foreach (IMailItem msg in mbox)` to read asynchronously.

## Limits

- **A preview.** 0.3.0-preview.2: the API can change before 1.0.
- **MMDF isn’t supported**, as the project’s fidelity table states.

Maildir folders, Apple Mail `.emlx` files, Outlook Express `.dbx` files and Outlook for Mac `.olm` archives open with the same `Mail.Open` call. Everything checked, and every known gap: [Fidelity and known gaps](https://recursiveai.org/onemail/docs/compatibility/). The stores: [Mailboxes](https://recursiveai.org/onemail/docs/mailboxes/).
