---
title: "Detect EML, MSG, and PST file formats by content in C#"
url: https://recursiveai.org/onemail/cookbook/detect-email-file-format-from-content/
description: "Identify EML messages, Outlook MSG items, and PST stores from their contents instead of the filename extension, using OneMail’s C# format sniffer."
version: "1.0.1"
---

# Detect EML, MSG, and PST file formats by content 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/detect-email-file-format-from-content/detect-email-file-format-from-content.zip)

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

```text
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](https://recursiveai.org/onemail/cookbook/detect-email-file-format-from-content/detect-email-file-format-from-content.zip) 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:

```shell
dotnet run -- outlook.data
```

```text
outlook.data: Msg
```

## Related recipes

- [Read an Outlook MSG file](https://recursiveai.org/onemail/cookbook/read-msg-file/)
- [Read messages from a PST file](https://recursiveai.org/onemail/cookbook/read-pst-file/)
- [Read an EML file](https://recursiveai.org/onemail/cookbook/parse-eml/)

## 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](https://github.com/recursive-ai-oss/onemail-csharp/blob/fe2e40ab5837ca7e9e9641c1dcdfccf209094f89/src/OneMail/Msg/MsgFile.cs#L71-L97) 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](https://recursiveai.org/onemail/docs/shared-view/) 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.
