Reading an Outlook Message (MSG) file

Office Automation

To use Office Automation objects for Microsoft Outlook, you need to add references to Microsoft Office and Microsoft Office Interop for Outlook libraries to your project.

Programming Samples

C#


 // Add the namespaces

using Microsoft.Office;

using Microsoft.Office.Interop.Outlook;

// Create a new Application Class

Application app = new Application();

// Create a MailItem object

MailItem item = (MailItem)outlook.CreateItemFromTemplate(@"d:\temp\test.msg", Type.Missing);

// Access different fields of the message

System.Console.WriteLine(string.Format("Subject:{0}", item.Subject));

System.Console.WriteLine(string.Format("Sender Email Address:{0}", item.SenderEmailAddress));

System.Console.WriteLine(string.Format("SenderName:{0}", item.SenderName));

System.Console.WriteLine(string.Format("TO:{0}", item.To));

System.Console.WriteLine(string.Format("CC:{0}", item.CC));

System.Console.WriteLine(string.Format("BCC:{0}", item.BCC));

System.Console.WriteLine(string.Format("Html Body:{0}", item.HTMLBody));

System.Console.WriteLine(string.Format("Text Body:{0}", item.Body));



Aspose.Email for .NET

The following code snippet does the same thing as the code above using Aspose.Email for .NET.

To access the Aspose.Email.Outlook objects, you need to add a reference to Aspose.Email to your project.

Programming Samples

C#


 // Add the namespace for Aspose.Email.Outlook

using Aspose.Email.Outlook;

// Create a new object of type MapiMessage

MapiMessage msg = MapiMessage.FromFile(@"d:\temp\test.msg");

// Access the fields of the message

System.Console.WriteLine(string.Format("Subject:{0}", msg.Subject));

System.Console.WriteLine(string.Format("Sender Email Address:{0}", msg.SenderEmailAddress));

System.Console.WriteLine(string.Format("SenderName:{0}", msg.SenderName));

System.Console.WriteLine(string.Format("TO:{0}", msg.DisplayTo));

System.Console.WriteLine(string.Format("CC:{0}", msg.DisplayCc));

System.Console.WriteLine(string.Format("BCC:{0}", msg.DisplayBcc));

System.Console.WriteLine(string.Format("Text Body:{0}", msg.Body));

System.Console.WriteLine(string.Format("Rtf Body:{0}", msg.BodyRtf));