Class PersonalStorage

PersonalStorage class

Provides functionality to access and manipulate the PST (Personal Storage Table) files used by Microsoft Outlook.

public class PersonalStorage : IDisposable

Constructors

NameDescription
PersonalStorage(TraversalExceptionsCallback)Initializes a new instance of the PersonalStorage class. Allows setting a callback method for handling exceptions that occur during PST traversal.

Properties

NameDescription
CanWrite { get; }Gets a value indicating whether the current pst supports writing.
Format { get; }Gets the file format.
IsUnicode { get; }Gets a value indicating whether the PST file format is Unicode. There are two versions of the PST file format: Unicode and ANSI.
RootFolder { get; }Gets the root folder of PST.
Store { get; }Gets the PST message store.

Methods

NameDescription
static Create(Stream, FileFormatVersion)Creates the PST in a stream.
static Create(string, FileFormatVersion)Creates the new PST file with the specified file name.
static Create(Stream, FileFormatVersion, bool)Creates the PST in a stream.
static Create(Stream, FileFormatVersion, CancellationToken)Creates the PST in a stream.
static Create(string, FileFormatVersion, CancellationToken)Creates the new PST file with the specified file name.
static Create(Stream, FileFormatVersion, bool, CancellationToken)Creates the PST in a stream.
static FromFile(string)Load PST from file.
static FromFile(string, bool)Load PST from file.
static FromFile(string, CancellationToken)Load PST from file.
static FromFile(string, PersonalStorageLoadOptions)Load PST from file.
static FromFile(string, bool, CancellationToken)
static FromFile(string, PersonalStorageLoadOptions, CancellationToken)Load PST from file.
static FromStream(Stream)Load PST from stream.
static FromStream(Stream, bool)Load PST from stream.
static FromStream(Stream, CancellationToken)Load PST from file.
static FromStream(Stream, PersonalStorageLoadOptions)Load PST from stream.
static FromStream(Stream, bool, CancellationToken)
static FromStream(Stream, PersonalStorageLoadOptions, CancellationToken)Load PST from file.
ChangeMessage(string, MapiPropertyCollection)Changes the message properties.
ConvertTo(FileFormat)Converts the current object to the specified format.
CreatePredefinedFolder(string, StandardIpmFolder)Creates the standard interpersonal message (IPM) folder.
CreatePredefinedFolder(string, StandardIpmFolder, bool)Creates the standard interpersonal message (IPM) folder.
DeleteItem(string)Deletes the item (folder or message) by it’s entryId
Dispose()Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
EnumerateMessages(string)Exposes the enumerator, which supports an iteration of messages in folder.
EnumerateMessages(string, int, int)Exposes the enumerator, which supports an iteration of messages in folder.
ExtractAttachments(MessageInfo)Extracts the attachments.
ExtractAttachments(string)Extracts the attachments.
ExtractMessage(byte[])Get the message from PST.
ExtractMessage(MessageInfo)Get the message from PST.
ExtractMessage(string)Get the message from PST.
ExtractProperty(byte[], long)Gets the specified property of item, without extract the item fully.
ExtractRecipients(MessageInfo)Extracts the recipients.
ExtractRecipients(string)Extracts the recipients.
FindAndExtractSoftDeletedItems()Finds and extracts soft-deleted messages from the PST.
FindMessages(string)Finds the identifiers of messages for for the current folder. It might be useful in case of reading corrupted pst when the GetContents and EnumerateMessages methods could throw an exception.
FindSubfolders(string)Finds the identifiers of subfolders for for the current folder. It might be useful in case of reading corrupted pst when the GetSubfolders and EnumerateFolders methods could throw an exception.
GetCategories()Retrieves a list of PST item categories, each containing a name and associated color.
GetFolderById(byte[])Gets the personal folder from PST.
GetFolderById(string)Gets the personal folder from PST.
GetParentFolder(byte[])Gets the parent folder of message.
GetParentFolder(string)Gets the parent folder of message.
GetPredefinedFolder(StandardIpmFolder)Gets the standard interpersonal message (IPM) folder from PST. Outlook can create a number of default folders, such as Outbox, Deleted Items, Sent Items etc.
Load(Stream)Load PST from stream. This method is used when a PersonalStorage object is created using the constructor.
Load(string)Load PST from file. This method is used when a PersonalStorage object is created using the constructor.
MergeWith(Stream[])Merges the pst storage with one or more other pst streams. Thus, the combined stream are sources.
MergeWith(string[])Merges the pst storage with one or more other pst files. Thus, the combined files are sources.
MoveItem(FolderInfo, FolderInfo)Moves a specified folder to a new parent folder within the current pst.
MoveItem(MessageInfo, FolderInfo)Moves a specified message to a new folder within the current pst.
SaveAs(Stream, FileFormat)Saves the current object to a specified file format in a stream.
SaveAs(string, FileFormat)Saves the current object to a specified file format in a different file.
SaveMessageToStream(string, Stream)Saves the message, with specified entryID, to a stream.
SplitInto(IList<MailQuery>, string)Splits the pst storage based on criteria.
SplitInto(long, string)Splits the pst storage into less sized parts.
SplitInto(IList<MailQuery>, string, string)Splits the pst storage based on criteria.
SplitInto(long, string, string)Splits the pst storage into less sized parts.
TryToGetFolderById(string, out FolderInfo)Gets the folder associated with the specified entry ID.
TryToSaveMessage(string, Stream)Saves the message, with specified entryID, to a stream.

Events

NameDescription
event ItemMovedOccurs when an item is moved to the another folder.
event StorageProcessedOccurs in splitting and merging operations when a new chunk of pst is created or the next file is processed and is to be merged.
event StorageProcessingOccurs before the srorage is processed. The event is raised before processing the next storage in merging or splitting operations.

Remarks

The PersonalStorage class encapsulates methods for creating, opening, and working with the contents of PST files including emails, appointments, contacts, tasks, and other personal information.

Examples

This code serves as a way to browse and extract messages from a PST file, printing metadata about each folder and message, extract and saving the actual messages as .msg files.

[C#]

// Open the PST file by creating an instance of PersonalStorage
using (var pst = PersonalStorage.FromFile("storage.pst"))
{
    // Retrieve the total number of items in the PST file
    var totalItemsCount = pst.Store.GetTotalItemsCount();
    
    // Write the total items count to the console
    Console.WriteLine($"Total items count: {totalItemsCount}");

    // Iterate through each subfolder within the root folder of the PST
    foreach (var folderInfo in pst.RootFolder.GetSubFolders())
    {
        // Write the display name of the folder to the console
        Console.WriteLine($"Folder: {folderInfo.DisplayName}");
        // Write the total number of items in the folder to the console
        Console.WriteLine($"Total items: {folderInfo.ContentCount}");
        // Write the count of unread items in the folder to the console
        Console.WriteLine($"Total unread items: {folderInfo.ContentUnreadCount}");

        // Enumerate through each message in the current folder
        foreach (var messageInfo in folderInfo.EnumerateMessages())
        {
            // Write the subject of the message to the console
            Console.WriteLine($"Subject: {messageInfo.Subject}");
            
            // Extract the full message object from the messageInfo
            var msg = pst.ExtractMessage(messageInfo);
            
            // Save the message as a .msg file, using its subject as the filename
            msg.Save($"{msg.Subject}.msg");
        }
    }
}

[Visual Basic]

' Open the PST file by creating an instance of PersonalStorage
Using pst As PersonalStorage = PersonalStorage.FromFile("storage.pst")
    ' Retrieve the total number of items in the PST file
    Dim totalItemsCount As Integer = pst.Store.GetTotalItemsCount()
    
    ' Write the total items count to the console
    Console.WriteLine($"Total items count: {totalItemsCount}")

    ' Iterate through each subfolder within the root folder of the PST
    For Each folderInfo In pst.RootFolder.GetSubFolders()
        ' Write the display name of the folder to the console
        Console.WriteLine($"Folder: {folderInfo.DisplayName}")
        ' Write the total number of items in the folder to the console
        Console.WriteLine($"Total items: {folderInfo.ContentCount}")
        ' Write the count of unread items in the folder to the console
        Console.WriteLine($"Total unread items: {folderInfo.ContentUnreadCount}")

        ' Enumerate through each message in the current folder
        For Each messageInfo In folderInfo.EnumerateMessages()
            ' Write the subject of the message to the console
            Console.WriteLine($"Subject: {messageInfo.Subject}")
            
            ' Extract the full message object from the messageInfo
            Dim msg As MailItem = pst.ExtractMessage(messageInfo)
            
            ' Save the message as a .msg file, using its subject as the filename
            msg.Save($"{msg.Subject}.msg")
        Next
    Next
End Using

See Also