Recursive AI

Open winmail.dat in C#

All onemail recipes

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.

Terminal
dotnet new console -o Winmail && cd Winmail
dotnet add package RecursiveAI.OneMail --version 0.2.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:

C#
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.

Limits

  • A preview. 0.2.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. The format: TNEF (winmail.dat).