Parsing Outlook Message Files

[ ]

Aspose.Email for .NET provides the MapiMessage class that is used to open and parse an MSG file. As there may be many recipients in an MSG file, the MapiMessage class exposes the Recipients property that returns a MapiRecipientCollection which represents a collection of MapiRecipient objects. The MapiRecipient object further exposes methods for working with recipient attributes.

The following sequence of steps serves this purpose:

  1. Create an instance of the MapiMessage class using the MapiMessage.FromFile static method.
  2. Display the sender name, subject, and body from the MSG file using SenderName, Subject and Body properties.
  3. Use the Recipients property to get a reference to the collection of MapiRecipient objects associated with the MSG file.
  4. Loop through the MapiRecipientCollection collection to display contents for each MapiRecipient object through its public methods.
// The path to the resource directory.
string dataDir = RunExamples.GetDataDir_Email();

//Instantiate an MSG file to load an MSG file from disk
var outlookMessageFile = MapiMessage.FromFile(dataDir + "message.msg");
//Display sender's name
Console.WriteLine("Sender Name : " + outlookMessageFile.SenderName);
//Display Subject
Console.WriteLine("Subject : " + outlookMessageFile.Subject);
//Display Body
Console.WriteLine("Body : " + outlookMessageFile.Body);
//Display Recipient's info
Console.WriteLine("Recipients : \n");

//Loop through the recipients collection associated with the MapiMessage object
foreach (var rcp in outlookMessageFile.Recipients)
	//Display recipient email address
	Console.WriteLine("Email : " + rcp.EmailAddress);
	//Display recipient name
	Console.WriteLine("Name : " + rcp.DisplayName);
	//Display recipient type
	Console.WriteLine("Recipient Type : " + rcp.RecipientType);