Parsing Outlook Message Files
Contents
[
Hide
]
Using Aspose.Email for Java, developers can not only load but also parse contents from Outlook message files.
- To load MSG files from disk, use the MapiMessage class' static fromFile method.
- To parse MSG file contents, the MapiMessage exposes a number of methods.
This topic shows how to loaded and then parsed and MSG file to display its contents.
Aspose.Email for Java 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 getRecipients() method 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 to load an MSG file from the fromFile static method.
- Display the sender name, subject, and body from the MSG file using getSenderName(), getSubject() and getBody() methods.
- Call the getRecipients() method exposed by the MapiRecipient class 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 = Utils.getSharedDataDir(ParsingOutlookMessageFiles.class) + "outlook/";
//Instantiate an MSG file to load an MSG file from disk
MapiMessage outlookMessageFile = MapiMessage.fromFile(dataDir + "message.msg");
//Display sender's name
System.out.println("Sender Name : " + outlookMessageFile.getSenderName());
//Display Subject
System.out.println("Subject : " + outlookMessageFile.getSubject());
//Display Body
System.out.println("Body : " + outlookMessageFile.getBody());
//Display Recipient's info
System.out.println("Recipients : \n");
//Loop through the recipients collection associated with the MapiMessage object
for (int i = 0; i < outlookMessageFile.getRecipients().size(); i++) {
//Set a reference to the MapiRecipient object
MapiRecipient rcp = (MapiRecipient) outlookMessageFile.getRecipients().get_Item(i);
//Display recipient email address
System.out.println("Email : " + rcp.getEmailAddress());
//Display recipient name
System.out.println("Name : " + rcp.getDisplayName());
//Display recipient type
System.out.println("Recipient Type : " + rcp.getRecipientType());
}
Try it out!
Parse email files online with the free Aspose.Email Parser App.