In this article, we will show you how to parse an outlook msg file in your C# application. We will use Aspose.Network library for this purpose. Aspose.Network is a .NET library that is available in the form of a dll. You can use this library to view msg files in windows, web, console or any .NET based application. The trial version of library can be downloaded from http://www.aspose.com/categories/utility-components/aspose.network-for-.net/default.aspx and the source code for the msg viewer demo can be downloaded from http://www.aspose.com/community/files/54/utility-components/aspose.network/entry130065.aspx.
Introduction
This article is about parsing and viewing outlook msg files. You need some api for parsing outlook msg format, if you have some requirement of manipulating msg files in your project/application. Or, if you do not have Microsoft Outlook installed on your system, you can build your own viewer to get the msg file contents.
Outlook Msg Viewer Demo
We have created a simple demo application that can be used to parse and view msg files. The UI can be seen in the figure below:
The left pane shows the drives and folders in your system, just similar to windows explorer. You can browse the folders to show/filter the msg files. Only msg files will appear in the top listview control in the corresponding folder in the treeview. As you can see from the screenshot above, subject, from, to and cc fields are shown in the top list.
If you click on any msg/item in the list, the corresponding message will open and the details can be seen in the UI below the list view control. We have used labels to display the subject, to, cc and from.
At the bottom pane, we have used tab controls to show other message details like text body, rtf body, message header, attachments and MAPI properties.
If you click on the “Attachments” tab, it will show the list of attachments in the msg file (if any). You can also export/save the attachments to your system by selecting an attachment and clicking on the “Save” button. It will open the Save File Dialog. You can browse your desired folder and save the file there. The following screenshot shows the “Attachments” tab view.
How to Parse and View Msg File contents Programmatically
In this section, we will present the code that we used in the demo to show the msg file contents.
Loading an Msg File
Aspose.Network library provides MapiMessage class for loading and parsing msg files. You can load the msg file using a single line of code by calling FromFile() static method and passing the path of the msg file.
[C#]
MapiMessage msg = MapiMessage.FromFile(“C:\\SomeFolder\\SomeMsgFile.msg”);
[VB.NET]
Dim msg As MapiMessage = MapiMessage.FromFile(“C:\\SomeFolder\\SomeMsgFile.msg”)
Getting the From, To, Cc and Subject from Msg File
MapiMessage class exposes properties and collections for getting subject, to, cc and from. Following is the sample code for getting these properties.
[C#]
// subject
if (msg.Subject != null)
Console.WriteLine(msg.Subject);
else
Console.WriteLine("no subject");
// sender
if (msg.SenderEmailAddress != null)
Console.WriteLine(msg.SenderEmailAddress);
else
Console.WriteLine("No sender");
// to
if (msg.DisplayTo != null)
Console.WriteLine(msg.DisplayTo);
else
Console.WriteLine("No one in To");
// cc
if (msg.DisplayCc != null)
Console.WriteLine(msg.DisplayCc);
else
Console.WriteLine("No one in cc");
[VB.NET]
' subject
If Not msg.Subject Is Nothing Then
Console.WriteLine(msg.Subject)
Else
Console.WriteLine("no subject")
End If
' sender
If Not msg.SenderEmailAddress Is Nothing Then
Console.WriteLine(msg.SenderEmailAddress)
Else
Console.WriteLine("No sender")
End If
' to
If Not msg.DisplayTo Is Nothing Then
Console.WriteLine(msg.DisplayTo)
Else
Console.WriteLine("No one in To")
End If
' cc
If Not msg.DisplayCc Is Nothing Then
Console.WriteLine(msg.DisplayCc)
Else
Console.WriteLine("No one in cc")
End If
Getting the Text Body and Rtf Body of the Message
We can get Text and Rtf body of the message by using properties of MapiMessage class. Following the sample code to get these.
[C#]
// text body
if (msg.Body != null)
Console.WriteLine(msg.Body);
else
Console.WriteLine("no text body.");
// rtf body
if (msg.BodyRtf != null)
Console.WriteLine(msg.BodyRtf);
else
Console.WriteLine("No Rtf body.");
[VB.NET]
' text body
If Not msg.Body Is Nothing Then
Console.WriteLine(msg.Body)
Else
Console.WriteLine("no text body.")
End If
' rtf body
If Not msg.BodyRtf Is Nothing Then
Console.WriteLine(msg.BodyRtf)
Else
Console.WriteLine("No Rtf body.")
End If
Getting the Attachments From Msg File and Saving to Disk
MapiMessage class provides the Attachments collection for getting all the attachments in the message (msg) file. MapiMessage.Attachments property returns the object of type MapiAttachmentCollection. You can use a foreach loop to iterate through the attachments collection and list the attachments. Attachment class contains the Save() method for saving the individual attachment to the disk. Following is the sample code to get the list of attachments and saving to disk.
[C#]
// iterate through the Attachments collection
foreach (MapiAttachment att in msg.Attachments)
{
try
{
// show attachment name on screen
Console.WriteLine(att.LongFileName);
// save in local drive/folder
att.Save(att.LongFileName);
}
catch (Exception ex) { Console.WriteLine(ex.Message;) }
}
[VB.NET]
' iterate through the Attachments collection
For Each att As MapiAttachment In msg.Attachments
Try
' show attachment name on screen
Console.WriteLine(att.LongFileName)
' save in local drive/folder
att.Save(att.LongFileName)
Catch ex As Exception
Console.WriteLine(ex.Message;)
End Try
Next att
Getting the MAPI Properties of the Msg File
You can get the MAPI Properties from the Msg file using the “Properties” collection of the MapiMessage class. Following is the sample code to get all the MAPI properties present in the message file.
[C#]
try
{
Aspose.Network.Outlook.MapiProperty mapi = Aspose.Network.Outlook.MapiProperty)dictionaryEntry.Value;
if (mapi.Name.Trim().Length > 0)
{
// display name of MAPI property
Console.WriteLine(mapi.Name);
// display value of the MAPI property
Console.WriteLine(mapi.ToString());
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
[VB.NET]
Try
Dim mapi As Aspose.Network.Outlook.MapiProperty = Aspose.Network.Outlook.MapiProperty)dictionaryEntry.Value
If mapi.Name.Trim().Length > 0 Then
' display name of MAPI property
Console.WriteLine(mapi.Name)
' display value of the MAPI property
Console.WriteLine(mapi.ToString())
End If
Catch e As Exception
Console.WriteLine(e.Message)
End Try