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.
dotnet new console -o ReadMbox && cd ReadMbox dotnet add package RecursiveAI.OneMail --version 0.2.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:
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}"); }
Format: Mbox 2009-06-01 04:26:31Z [email protected] Re: question about when shuffle/sort start working 621 characters: When a Mapper completes, MapCompletionEv… 2009-06-01 05:30:49Z [email protected] Re: question about when shuffle/sort start working 1259 characters: Thanks a lot for your explanation, Jothi… 2009-06-01 06:03:13Z [email protected] 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.2.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 and Outlook Express .dbx files open with the same Mail.Open call. Everything checked, and every known gap: Fidelity and known gaps. The stores: Mailboxes.