Skip to content

Instantly share code, notes, and snippets.

@aspose-com-gists
Last active April 29, 2022 05:54
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save aspose-com-gists/f41f10fa8737347ab9300d8b675d3da6 to your computer and use it in GitHub Desktop.
Read MS Outlook PST Files in Java | Read Emails and Contacts
// Load the Outlook PST file
PersonalStorage pst = PersonalStorage.fromFile("SampleContacts.pst");
// Get the Contacts folder
FolderInfo folderInfo = pst.getRootFolder().getSubFolder("Contacts");
// Loop through all the contacts in this folder
MessageInfoCollection messageInfoCollection = folderInfo.getContents();
for (int i = 0; i < messageInfoCollection.size(); i++) {
MessageInfo messageInfo = (MessageInfo) messageInfoCollection.get_Item(i);
// Get the contact information
MapiContact contact = (MapiContact) pst.extractMessage(messageInfo).toMapiMessageItem();
// Display some contents on screen
System.out.println("Name: " + contact.getNameInfo().getDisplayName() + "\n");
// Save to disk in MSG format
if (contact.getNameInfo().getDisplayName() != null) {
MapiMessage message = pst.extractMessage(messageInfo); // Get rid of illegal characters that cannot be used as a file name
String messageName = message.getSubject().replace(":", " ").replace("\\", " ").replace("?", " ").replace("/", " ");
message.save(messageName + ".msg");
}
}
// Load the Outlook PST file
PersonalStorage pst = PersonalStorage.fromFile(path);
// Get the Display Format of the PST file
System.out.println("Display Format: " + pst.getFormat());
// Get the folders and messages information
FolderInfo folderInfo = pst.getRootFolder();
// Call the recursive method to extract msg files from each folder
extractMsgFiles(folderInfo, pst);
// This is a recursive method to display contents of a folder
private static void extractMsgFiles(FolderInfo folderInfo, PersonalStorage pst) {
// display the folder name
System.out.println("Folder: " + folderInfo.getDisplayName());
System.out.println("==================================");
// Loop through all the messages in this folder
MessageInfoCollection messageInfoCollection = folderInfo.getContents();
for (MessageInfo messageInfo : messageInfoCollection) {
System.out.println("Saving message {0} ...." + messageInfo.getSubject());
// Get the message in MapiMessage instance
MapiMessage message = pst.extractMessage(messageInfo);
// Save this message to disk in msg format
message.save(message.getSubject().replace(":", " ") + ".msg");
// Save this message to stream in msg format
ByteArrayOutputStream messageStream = new ByteArrayOutputStream();
message.save(messageStream);
}
// Call this method recursively for each subfolder
if (folderInfo.hasSubFolders() == true) {
for (FolderInfo subfolderInfo : folderInfo.getSubFolders()) {
extractMsgFiles(subfolderInfo, pst);
}
}
}
// Load the Outlook PST file
PersonalStorage pst = PersonalStorage.fromFile("PersonalStorage.pst");
// Get the folders information
FolderInfoCollection folderInfoCollection = pst.getRootFolder().getSubFolders();
// Browse through each folder to display information
for (int i = 0; i < folderInfoCollection.size(); i++) {
FolderInfo folderInfo = (FolderInfo) folderInfoCollection.get_Item(i);
System.out.println("FolderId: " + folderInfo.getEntryIdString());
System.out.println("Folder: " + folderInfo.getDisplayName());
System.out.println("Total items: " + folderInfo.getContentCount());
System.out.println("Total unread items: " + folderInfo.getContentUnreadCount());
System.out.println("-----------------------------------");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment