rouletteroulette has posted 628 posts at DZone. View Full User Profile

How to Parse and view Outlook .msg Files programmatically

10.08.2008
| 39683 views |
  • submit to reddit

This article shows that how can you parse and view Outlook .msg files using your own code in C# applications where Microsoft Office Outlook is not required for it. A separate Outlook msg Viewer Demo (attached with this article) is also available to guide you for parsing and reading Outlook .msg files using Aspose.Network library.

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

More about Aspose.Network for .NET

Aspose.Network is a suite of .NET components for network programming with support for .NET logging framework, Microsoft Exchange Server and allows parsing an Outlook document with drag & drop features. It provides new iCalendar engine and SSL support for SMTP, POP3 & IMAP protocols with other mail merge features. You can import & export emails in MHT & EML formats. It supports all the features of SMTP, MIME, S/MIME, POP3, FTP, WhoIs, DNS, ICMP, IMAP, HTTP, SOCKS 4/4A & SOCKS 5 components.

Legacy
Article Resources: 
Published at DZone with permission of its author, rouletteroulette rouletteroulette.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)