Home > Code > C# > Load, Parse or View MS Outlook MSG Files from a File or Stream

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

by sher azam   on May 28, 2013   Category: C#   |  Views: 2237    |  Points: 25   |  Gold 


This technical tip explains how to load a Microsoft Outlook Messagefile (*.msg). The MapiMessage class is used to load 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.

Sample Code for Outlook Message (MSG) File Loading, Viewing and Parsing

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)

Overview: Aspose.Email for .NET

Aspose.Email for .NET is a set of components allowing developers to easily implement email functionality within their ASP.NET web applications, web services & Windows applications. It Supports Outlook PST, EML, MSG & MHT formats. It allows developers to work with SMTP, POP3, FTP & MS Exchange servers. It supports mail merge, iCalendar, customized header & body, header information, embedded files, Twitter & many more. It makes it easy to work with HTML or plain text emails & their attachments.

More about Aspose.Email for .NET

- Homepage of Aspose.Email for .NET: http://www.aspose.com/.net/email-component.aspx

- Download Aspose.Email for .NET: http://www.aspose.com/community/files/51/.net-components/aspose.email-for-.net/default.aspx

- More Technical Tips by Aspose.Email for .NET: http://www.aspose.com/docs/display/emailnet/Programmers+Guide

Contact Information
Aspose Pty Ltd, Suite 163,
79 Longueville Road
Lane Cove, NSW, 2066
Australia
http://www.aspose.com/
sales@aspose.com
Phone: 888.277.6734
Fax: 866.810.9465



Post Code  |  Code Snippet Home

User Responses


No response found, be the first to review this code snippet.

Submit feedback about this code snippet

Please sign in to post feedback

Latest Posts