How can I parse the winmail.dat file?

5.1k views Asked by At

Simple question. I have a C# application that access an IMAP server using the AE.Net.Mail library to retrieve messages and attached files. Sometimes, when a message is sent from Outlook the attachments are contained in a file named winmail.dat. That is a file in the TNEF format.

Is there a library or any other way for my application to "unpack" theses files so I can get the attachments?

6

There are 6 answers

1
Mathieu Pagé On BEST ANSWER

I've found this tool that can be controled from the command line to extract attacheemnts, from a TNEF file, but I would really prefer a library that would run "in process".

2
Bert Johnson On

I had the same problem and ended up creating an entire email library for .NET called OpaqueMail.

It has support for parsing TNEF-encoded files (e.g. Winmail.dat) via its TnefEncoding class.

Check out this example.

// Instantiate a TNEF Encoding object to process the byte array "tnefEncoddedBytes".
TnefEncoding tnefEncoding = new TnefEncoding(tnefEncodedBytes);

// Loop through the TNEF-encoded attachments, outputting their names, content types, and sizes.
foreach (MimePart mimePart in tnefEncoding.MimeAttachments)
{
    Console.WriteLine("MIME Part Name: " + mimePart.Name);
    Console.WriteLine("MIME Part Content Type: " + mimePart.ContentType);
    Console.WriteLine("MIME Part Size: " + mimePart.BodyBytes.Length);
}
0
Suresh On

If its for Linux platform, then you could use tnef tool

TNEF

Usage to get the Body from winmail.dat : tnef -w --save-body winmail.dat Usage to extract only the attachments from winmail.dat file: tnef winmail.dat

Tnef extracts the Body/atatchments into our current directory where we are running this tool. Or else we could give the -C DIR, --directory=DIR to save them into the specified path

0
Eric Cartman On

A quick search leads to this : Yerase's TNEF Stream Reader

The project has source code, you can either reproduce it in C# or make a small C# (or better, C++/CLI) wrapper to use it.

1
esskar On

TnefReader Class could be of help.

0
jstedfast On

I've just recently implemented TNEF support in MimeKit which is based on Microsoft's Exchange TNEF API, thus allowing full access to all of the data contained within.