Work with winmail.dat (TNEF Parser)


When an Outlook user composes and sends a message using either Rich Text Format or HTML Format, Outlook automagically generates a file, winmail.dat, and attaches it to the end of the email. The winmail.dat contains the rich text body and original attachments. EAGetMail provides the TNEF parser to decode the winmail.dat. With this feature, you application has the highly compatibility with the outlook email.

Example

[Visual Basic, C#] The following example demonstrates how to decode the winmail.dat attachment. To get the full samples of EAGetMail, please refer to Samples section.

[Visual Basic]
Dim oMail As New Mail("TryIt")
oMail.Load("c:\test.eml", False)

Dim atts() As Attachment = oMail.Attachments
Dim tempFolder As String = "c:\temp"
Dim count As Integer = atts.Length
If (Not System.IO.Directory.Exists(tempFolder)) Then
    System.IO.Directory.CreateDirectory(tempFolder)
End If

For i As Integer = 0 To count - 1
    Dim att As Attachment = atts(i)
    'this attachment is in OUTLOOK RTF format (TNEF), decode it here.
    If (String.Compare(att.Name, "winmail.dat") = 0) Then
        Dim tatts() As Attachment
        Try
            tatts = Mail.ParseTNEF(att.Content, True)
            Dim y As Integer = tatts.Length
            For x As Integer = 0 To y - 1
                Dim tatt As Attachment = tatts(x)
                Dim tattname As String = String.Format("{0}\{1}", tempFolder, tatt.Name)
                tatt.SaveAs(tattname, True) 
            Next
        Catch ep As Exception
            MessageBox.Show(ep.Message)
        End Try
    Else
        Dim attname As String = String.Format("{0}\{1}", tempFolder, att.Name)
        att.SaveAs(attname, True)
    End If
Next


[C#] Mail oMail = new Mail("TryIt"); oMail.Load("c:\\test.eml", false); Attachment [] atts = oMail.Attachments; int count = atts.Length; string tempFolder = "c:\\temp"; if(!System.IO.Directory.Exists(tempFolder)) System.IO.Directory.CreateDirectory(tempFolder); for(int i = 0; i < count; i++) { Attachment att = atts[i]; //this attachment is in OUTLOOK RTF format (TNEF), decode it here. if(String.Compare(att.Name, "winmail.dat") == 0) { Attachment[] tatts = null; try { tatts = Mail.ParseTNEF(att.Content, true); } catch(Exception ep) { MessageBox.Show(ep.Message); continue; } int y = tatts.Length; for(int x = 0; x < y; x++) { Attachment tatt = tatts[x]; string tattname = String.Format("{0}\\{1}", tempFolder, tatt.Name); tatt.SaveAs(tattname , true); } continue; } string attname = String.Format("{0}\\{1}", tempFolder, att.Name); att.SaveAs(attname , true); }

Parse TNEF (winmail.dat) by DecodeTNEF method

In EAGetMail 4.5, a new method named DecodeTNEF is introduced. It is easier to parse TNEF attachment. Please have a look at the following example codes:

[Visual Basic - Parse Attachment]
Imports EAGetMail

Public Sub ParseAttachment()
    Dim oMail As New Mail("TryIt")
    oMail.Load("c:\test.eml", False)

    ' Decode winmail.dat (TNEF) automatically
    oMail.DecodeTNEF()

    Dim atts() As Attachment = oMail.Attachments
    
    Dim tempFolder As String = "c:\temp"
    Dim count As Integer = atts.Length
    If (Not System.IO.Directory.Exists(tempFolder)) Then
        System.IO.Directory.CreateDirectory(tempFolder)
    End If

    For i As Integer = 0 To count - 1
        Dim att As Attachment = atts(i)
        Dim attname As String = String.Format("{0}\{1}", tempFolder, att.Name)
        att.SaveAs(attname, True)
    Next
End Sub


[C# - Parse Attachment] using System; using System.Collections; using EAGetMail; public void ParseAttachment() { Mail oMail = new Mail("TryIt"); oMail.Load("c:\\test.eml", false); // Decode winmail.dat (TNEF) automatically oMail.DecodeTNEF(); Attachment [] atts = oMail.Attachments; int count = atts.Length; string tempFolder = "c:\\temp"; if(!System.IO.Directory.Exists(tempFolder)) System.IO.Directory.CreateDirectory(tempFolder); for(int i = 0; i < count; i++) { Attachment att = atts[i]; string attname = String.Format("{0}\\{1}", tempFolder, att.Name); att.SaveAs(attname , true); } }
[C++ - Parse Attachment] using namespace System; using namespace EAGetMail; Void ParseAttachment() { Mail ^oMail = gcnew Mail("TryIt"); oMail->Load("c:\\test.eml", false); // Decode winmail.dat (TNEF) automatically oMail->DecodeTNEF(); array<Attachment^> ^atts= oMail->Attachments; int count = atts->Length; String^ tempFolder = "c:\\temp"; if(!System::IO::Directory::Exists(tempFolder)) System::IO::Directory::CreateDirectory(tempFolder); for(int i = 0; i < count; i++) { Attachment ^att = atts[i]; String ^attname = String::Format("{0}\\{1}", tempFolder, att->Name); att->SaveAs(attname, true); } }
[JScript - Parse Attachment] function ParseAttachment() { var oMail:Mail = new Mail("TryIt"); oMail.Load("c:\\test.eml", false); // Decode winmail.dat (TNEF) automatically oMail.DecodeTNEF(); var atts:Attachment[] = oMail.Attachments; var count:int = atts.Length; var tempFolder:String = "c:\\temp"; if(!System.IO.Directory.Exists(tempFolder)) System.IO.Directory.CreateDirectory(tempFolder); for(var i:int = 0; i < count; i++) { var att:Attachment = atts[i]; var attname:String = String.Format("{0}\\{1}", tempFolder, att.Name); att.SaveAs(attname , true); } }

See Also

Using EAGetMail POP3 and IMAP4 Component
User Authentication and SSL Connection
Enable TLS 1.2 on Windows XP/2003/2008/7/2008 R2
Using Gmail IMAP4 OAUTH
Using Gmail/GSuite Service Account + IMAP4 OAUTH
Using Office365 EWS OAUTH
Using Office365 EWS OAUTH in Background Service
Using Hotmail IMAP4 OAUTH
Digital Signature and E-mail Encryption/Decryption
Unique Identifier (UIDL) in POP3 and IMAP4 protocol
Parse Bounced Email (delivery-report)
EAGetMail Namespace References
EAGetMail POP3 and IMAP4 Component Samples

Online Tutorials

Parse winmail.dat (TNEF/MAPI) in C# - Tutorial
Parse winmail.dat (TNEF/MAPI) in VB.NET - Tutorial
Parse winmail.dat (TNEF/MAPI) in C++/CLI - Tutorial