Read Outlook PST File and Get Folders and SubFolders Information
Aspose.Email for .NET provides an API for reading Microsoft Outlook PST files. You can load a PST file from disk or stream into an instance of the PersonalStorage class and get the information about its contents, for example, folders, subfolders, and messages. The API also provides the capability to include search folders while traversing for messages from the PST folders.
Loading a PST File
An Outlook PST file can be loaded in an instance of the PersonalStorage class. The following code snippet shows you how to load the PST file.
// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-.NET | |
// Load the Outlook PST file | |
PersonalStorage personalStorage = PersonalStorage.FromFile(dataDir + @"PersonalStorage.pst"); |
Displaying Folders Information
After loading the PST file in the PersonalStorage class, you can get the information about the file display name, root folder, subfolders and messages count. The following code snippet shows you how to display the name of PST file, folders and the number of messages in the folders.
// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-.NET | |
// The path to the File directory. | |
string dataDir = RunExamples.GetDataDir_Outlook(); | |
string dst = dataDir + "PersonalStorage.pst"; | |
// load PST file | |
PersonalStorage personalStorage = PersonalStorage.FromFile(dst); | |
// Get the folders information | |
FolderInfoCollection folderInfoCollection = personalStorage.RootFolder.GetSubFolders(); | |
// Browse through each folder to display folder name and number of messages | |
foreach (FolderInfo folderInfo in folderInfoCollection) | |
{ | |
Console.WriteLine("Folder: " + folderInfo.DisplayName); | |
Console.WriteLine("Total items: " + folderInfo.ContentCount); | |
Console.WriteLine("Total unread items: " + folderInfo.ContentUnreadCount); | |
Console.WriteLine("-----------------------------------"); | |
} |
Get user-defined folders only
PST/OST files may contain folders which were created by a 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.
// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-.NET | |
using (PersonalStorage pst = PersonalStorage.FromFile(dataDir + "Outlook.pst")) | |
{ | |
PersonalStorageQueryBuilder queryBuilder = new PersonalStorageQueryBuilder(); | |
queryBuilder.OnlyFoldersCreatedByUser.Equals(true); | |
FolderInfoCollection subfolders = pst.RootFolder.GetSubFolders(queryBuilder.GetQuery()); | |
foreach (FolderInfo folder in subfolders) | |
{ | |
Console.WriteLine(folder.DisplayName); | |
} | |
} |
Parsing 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. The following code snippet shows you how to parse searchable folders.
// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-.NET | |
public static void Run() | |
{ | |
WalkAllFolders(); | |
} | |
// Open an OST and get the name and | |
// parent name for all folders in the file | |
private static void WalkAllFolders() | |
{ | |
string dataDir = RunExamples.GetDataDir_Outlook(); | |
string path = dataDir + "PersonalStorage.pst"; | |
byte[] buffer = File.ReadAllBytes(path); | |
using (Stream s = File.OpenRead(path)) | |
{ | |
PersonalStorage pst = PersonalStorage.FromStream(s); | |
// One sample of the sub-folder EntryId which could not be retrieved in Finder sub-folder | |
FolderInfo target = pst.GetFolderById("AAAAAB9of1CGOidPhTb686WQY68igAAA"); | |
IList<string> folderData = new List<string>(); | |
WalkFolders(pst.RootFolder, "N/A", folderData); | |
} | |
} | |
// Walk the folder structure recursively | |
private static void WalkFolders(FolderInfo folder, string parentFolderName, IList<string> folderData) | |
{ | |
string displayName = (string.IsNullOrEmpty(folder.DisplayName)) ? "ROOT" : folder.DisplayName; | |
string folderNames = string.Format("DisplayName = {0}; Parent.DisplayName = {1}", displayName, parentFolderName); | |
folderData.Add(folderNames); | |
if (displayName == "Finder") | |
{ | |
Console.WriteLine("Test this case"); | |
} | |
if (!folder.HasSubFolders) | |
{ | |
return; | |
} | |
FolderInfoCollection coll = folder.GetSubFolders(FolderKind.Search | FolderKind.Normal); | |
foreach (FolderInfo subfolder in coll) | |
{ | |
WalkFolders(subfolder, displayName, folderData); | |
} | |
} |
Retrieving Parent Folder Information from MessageInfo
The following code snippet shows you how to retrieve parent folder information from MessageInfo.
// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-.NET | |
using (PersonalStorage pst = PersonalStorage.FromFile(fileName)) | |
{ | |
foreach (FolderInfo folder in pst.RootFolder.GetSubFolders()) | |
{ | |
foreach (MessageInfo msg in folder.EnumerateMessages()) | |
{ | |
FolderInfo folderInfo = pst.GetParentFolder(msg.EntryId); | |
} | |
} | |
} |
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.
using (var currentPst = new PersonalStorage((exception, 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. And exceptions and corrupted items will be handled by the callback method.
using (PersonalStorage pst = new PersonalStorage((exception, itemId) => { /* Exception handling code. */ }))
{
if (pst.Load(@"test.pst"))
{
GetAllMessages(pst, pst.RootFolder);
}
}
private static void GetAllMessages(PersonalStorage pst, FolderInfo folder)
{
foreach (var messageEntryId in folder.EnumerateMessagesEntryId())
{
MapiMessage message = pst.ExtractMessage(messageEntryId);
}
foreach (var subFolder in folder.GetSubFolders())
{
GetAllMessages(pst, subFolder);
}
}