dotnet add package RecursiveAI.OneMailRequires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.
using RecursiveAI.OneMail; using RecursiveAI.OneMail.Sources; string[] paths = args.Length == 0 ? ["message.data", "outlook.data", "archive.data"] : args; foreach (string path in paths) { using var source = FileSource.Open(path); var format = MailFormatSniffer.Sniff(source); Console.WriteLine($"{path}: {format}"); }
message.data: Eml outlook.data: Msg archive.data: Pst
message.data starts with From: alex@example.com, a text header. outlook.data starts with the compound-file signature D0 CF 11 E0 A1 B1 1A E1 and has an Outlook property stream. archive.data starts with the four ASCII bytes !BDN. Both binary inputs contain the same synthetic note as the EML, with the subject “Pickup at noon.”
If the file exists but no supported signature or header matches, Sniff returns MailFormat.Unknown; opening a nonexistent path with FileSource.Open throws FileNotFoundException.
Paste the code above into Program.cs of a new dotnet new console project and dotnet run, or unzip the example and dotnet run.
Use the three .data files from the zip, or pass your own file with dotnet run -- <your file>.
Variations
Pass your own path without editing the array. Here’s a run on one of the bundled inputs:
dotnet run -- outlook.dataoutlook.data: MsgRelated recipes
The short answer
Open a FileSource and pass it to MailFormatSniffer.Sniff. It returns an enum such as MailFormat.Eml, MailFormat.Msg, or MailFormat.Pst without using the file name to decide. Use that result to choose how you’ll handle the file. If you already intend to read its messages, Mail.Open does format detection as part of opening the container and exposes the result in Format.
How do you identify a mail file type without an extension?
A renamed file still has its original contents. The example names all three files .data to make that visible. FileSource.Open supplies access to the bytes and Sniff reads the evidence it needs. This is useful when deciding whether a single message or a store was supplied: EML and MSG represent individual items, but PST is a container that can hold messages in folders. There’s no need to rename the file before passing it to Mail.Open.
What file signatures distinguish EML, MSG, and PST?
| Format | What OneMail looks for |
|---|---|
| EML | A header field, such as From: or Subject:, on the first line. There’s no fixed binary signature. |
| MSG | The compound-file signature and a root stream named __properties_version1.0. The signature alone is not enough. |
| PST | !BDN at the start. If the two-byte client marker at offset 8 is SO, OneMail reports Ost instead. |
For MSG, the format check in the source reads the compound-file directory. A compound file without the required root stream is reported as CompoundFile, not Msg. For EML, the rule recognizes a header-shaped line; it doesn’t establish that every header or MIME part is valid.
Why can an EML file show up as Unknown?
A message starting with a blank line or body text has no initial header for the sniffer to distinguish it from an ordinary text file. If you already know that the input should be raw email, Mail.Parse is the explicit MIME reader and can read a message without that header block. Don’t use that fallback for every unknown upload: the sniffer makes no claim about the input in that case.
The opposite can also happen: recognizing a format doesn’t mean the file is readable. For example, the PST marker can still match when the rest of the store is damaged. The shared view docs explain that Mail.Open throws InvalidDataException for a recognized MSG, PST, or OST that it can’t read at all. Keep detection and successful parsing separate when you report errors.