Read Outlook PST File and Get Folders and Subfolders Information

Load a PST File

An Outlook PST file can be loaded into an instance of the PersonalStorage class. A code snippet for loading the PST file is given below:

Displaying Folders Information

After loading the PST file into the PersonalStorage class, you can get the information about the file display name, root folder, subfolders and messages count. The following code snippet displays the name of a PST file, folders and number of messages in the folders:

Get user-defined folders only

A PST/OST files may contain folders that were created by the user. Aspose.Email provides the ability to access only user-defined folders by using the PersonalStorageQueryBuilder.OnlyFoldersCreatedByUser property. You can set the PersonalStorageQueryBuilder.OnlyFoldersCreatedByUser property to true to get only user-defined folders. The following code snippet demonstrates the use of PersonalStorageQueryBuilder.OnlyFoldersCreatedByUser to get user-defined folders.

Parse Searchable Folders

A PST/OST may contain searchable folders in addition to the normal type of folders. Aspose.Email provides the FolderKind enumerator for specifying the messages from such search folders with EnumerateFolders and GetSubFolders methods.

Retrieve Parent Folder Information from MessageInfo

The following code snippet shows you how to retrieve parent folder information from MessageInfo.

PST file traversal API

The traversal API allows extracting all PST items as far as possible, without throwing out exceptions, even if some data of the original file is corrupted. The following steps show how to use this API.

Use PersonalStorage constructor and load method instead of fromFile method.

The constructor allows defining a callback method.

try (PersonalStorage currentPst = new PersonalStorage(new TraversalExceptionsCallback() {
    public void invoke(TraversalAsposeException exception, String itemId) {
        // Exception handling code.
    }
})) {
    //
}

Loading and traversal exceptions will be available through the callback method.

The load method returns ‘true’ if the file has been loaded successfully and further traversal is possible. If a file is corrupted and no traversal is possible, ‘false’ is returned.

if (currentPst.load(inputStream))

This allows to open and traverse even corrupted PST files without throwing out exceptions. Both exceptions and corrupted items will be handled by the callback method.

try (PersonalStorage pst = new PersonalStorage(new TraversalExceptionsCallback() {
    public void invoke(TraversalAsposeException exception, String itemId) {
        // Exception handling code.
    }
})) {
    if (pst.load("test.pst")) {
        getAllMessages(pst, pst.getRootFolder());
    }
}

private static void getAllMessages(PersonalStorage pst, FolderInfo folder) {
    for (String messageEntryId : folder.enumerateMessagesEntryId()) {
        MapiMessage message = pst.extractMessage(messageEntryId);
    }
    for (FolderInfo subFolder : folder.getSubFolders()) {
        getAllMessages(pst, subFolder);
    }
}