Read the complete article on how to read Outlook PST files in Java: https://blog.aspose.com/2022/03/01/read-ms-outlook-pst-files-in-java/
Last active
April 29, 2022 05:54
Star
You must be signed in to star a gist
Read MS Outlook PST Files in Java | Read Emails and Contacts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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"); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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