Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Load, Parse or View MS Outlook MSG Files from a File or Stream

DZone's Guide to

Load, Parse or View MS Outlook MSG Files from a File or Stream

·
Free Resource
This technical tip explains how to load a Microsoft Outlook Messagefile (*.msg). The MapiMessage class is used to load Outlook MSG files, and provides several static loading functions for different scenarios. The code samples below show how to load MSG files from file or from stream. Some important steps for performing this task are Create an instance of MapiMessage from file, Get subject, Get from address, Get message body and Get attachments.
//Loading MSG Files

//[C#]

//Create an instance of MapiMessage from file
MapiMessage msg = MapiMessage.FromFile(@"d:\Test.msg");

//Get subject
Console.WriteLine("Subject:" + msg.Subject);

//Get from address
Console.WriteLine("From:" + msg.SenderEmailAddress);

//Get body
Console.WriteLine("Body" + msg.Body);

//Get recipients information
Console.WriteLine("Recipient: " + msg.Recipients);

//Get attachments
foreach (MapiAttachment att in msg.Attachments)
{
    Console.Write("Attachment Name: " + att.FileName);
    Console.Write("Attachment Display Name: " + att.DisplayName);
}


[VB.NET]

'Create an instance of MapiMessage from file
Dim msg As MapiMessage = MapiMessage.FromFile("d:\Test.msg")

'Get subject
Console.WriteLine("Subject:" + msg.Subject)

'Get from address
Console.WriteLine("From:" + msg.SenderEmailAddress)

'Get body
Console.WriteLine("Body" + msg.Body)

'Get recipients information
Console.WriteLine("Recipient: " & msg.Recipients.ToString())

'Get attachments
For Each att As MapiAttachment In msg.Attachments
    Console.Write("Attachment Name: " & att.FileName)
    Console.Write("Attachment Display Name: " & att.DisplayName)
Next att
 
//Loading MSG files from Stream

//[C#]

byte[] bytes = System.IO.File.ReadAllBytes(@"c:\test.msg");

using (MemoryStream stream =
        new System.IO.MemoryStream(bytes))
{
    stream.Seek(0, System.IO.SeekOrigin.Begin);
    //Create an instance of MapiMessage from file
    MapiMessage msg = MapiMessage.FromStream(stream);

    //Get subject
    Console.WriteLine("Subject:" + msg.Subject);

    //Get from address
    Console.WriteLine("From:" + msg.SenderEmailAddress);

    //Get body
    Console.WriteLine("Body" + msg.Body);

}
 
[VB.NET]

Dim bytes() As Byte = System.IO.File.ReadAllBytes("c:\test.msg")

Dim stream As MemoryStream = New System.IO.MemoryStream(bytes)

stream.Seek(0, System.IO.SeekOrigin.Begin)
'Create an instance of MapiMessage from file
Dim msg As MapiMessage = MapiMessage.FromStream(stream)

'Get subject
Console.WriteLine("Subject:" + msg.Subject)

'Get from address
Console.WriteLine("From:" + msg.SenderEmailAddress)

'Get body
Console.WriteLine("Body" + msg.Body)
Topics:

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}