---
title: "Open winmail.dat in C#"
url: https://recursiveai.org/onemail/cookbook/open-winmail-dat/
description: "Open a winmail.dat (TNEF) file in C# and save the attachments Outlook packed inside it. Works on the file alone or inside an email. Runnable code with OneMail 0.3.0-preview.2."
version: "0.3.0-preview.2"
---

# Open winmail.dat in C#

A `winmail.dat` attachment is TNEF, Outlook’s wrapper for attachments and rich text. `Mail.Open` reads it as one message whose `Attachments` are the files inside. The program below unpacks Apache POI’s public test file.

```shell
dotnet new console -o Winmail && cd Winmail
dotnet add package RecursiveAI.OneMail --version 0.3.0-preview.2
curl -LO https://raw.githubusercontent.com/apache/poi/REL_5_4_1/test-data/hmef/quick-winmail.dat
```

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

```csharp
using RecursiveAI.OneMail;

// winmail.dat is TNEF: Outlook's wrapper for attachments and rich text
using MailContainer tnef = Mail.Open("quick-winmail.dat");
Console.WriteLine($"Format: {tnef.Format}");

IMailItem msg = tnef.Enumerate().First();
Directory.CreateDirectory("winmail");
foreach (IMailAttachment a in msg.Attachments)
{
    string name = Path.GetFileName(a.FileName ?? "attachment.bin");
    string path = Path.Combine("winmail", name);
    using (Stream content = a.Open())
    using (FileStream file = File.Create(path))
        content.CopyTo(file);
    Console.WriteLine($"{path}  {a.Size:N0} bytes");
}
Console.WriteLine($"RTF body: {(msg.RtfBody is null ? "none" : "yes")}");
```

```text
Format: Tnef
winmail/quick.doc  19,968 bytes
winmail/quick.html  428 bytes
winmail/quick.pdf  18,638 bytes
winmail/quick.txt  235 bytes
winmail/quick.xml  143 bytes
RTF body: yes
```

All five files are byte-identical to the reference copies Apache POI keeps beside the test file. The rich-text body is in `RtfBody`.

## winmail.dat inside an email

You usually meet TNEF as an `application/ms-tnef` part of an `.eml`. You don’t need to find and unpack it: `Mail.Parse` and `Mail.Open` list the files inside it as the message’s attachments, in place of `winmail.dat`. Code for saving them: [Extract attachments from email](https://recursiveai.org/onemail/cookbook/extract-attachments/).

## Limits

- **A preview.** 0.3.0-preview.2: the API can change before 1.0.
- **TNEF inside MIME** is checked on generated messages only. The project says: “no TNEF-in-MIME message in the public corpora; checked on generated wrappers (64 messages)”.
- **Subject and sender.** A bare `winmail.dat` often carries no subject or sender: those live in the email around it. This one has neither.

Everything checked, and every known gap: [Fidelity and known gaps](https://recursiveai.org/onemail/docs/compatibility/). The format: [TNEF (winmail.dat)](https://recursiveai.org/onemail/docs/tnef/).
