Parsing Outlook Message Files
Contents
[
Hide
]
Using Aspose.Email for .NET, developers can not only load but also parse contents from Outlook message files.
- To load MSG files from disk, use the static MapiMessage.Load method of the MapiMessage class.
- To parse MSG file contents, the MapiMessage exposes a number of methods and properties.
This topic shows how to load and parse an MSG file to display its contents.
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:
- Create an instance of the MapiMessage class using the MapiMessage.Load static method.
- Display the sender name, subject, and body from the MSG file using SenderName, Subject and Body properties.
- Use the Recipients property to get a reference to the collection of MapiRecipient objects associated with the MSG file.
- 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.Load(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);
}
Try it out!
Parse email files online with the free Aspose.Email Parser App.