PstReader Class
Provides methods for parsing Outlook .PST message database file into a collection of folders and items.
Inheritance Hierarchy
SystemObject
  MailBee.OutlookPstReader

Namespace: MailBee.Outlook
Assembly: MailBee.NET (in MailBee.NET.dll) Version: 11.2.0 build 590 for .NET 4.5
Syntax
public class PstReader

The PstReader type exposes the following members.

Constructors
  NameDescription
Public methodPstReader(Stream)
Creates an instance of PstReader class and loads .PST database from a stream.
Public methodPstReader(String)
Creates an instance of PstReader class and opens the specified .PST file.
Public methodPstReader(Stream, String)
Creates a new .PST reader and loads .PST database from a stream.
Public methodPstReader(String, String)
Creates a new .PST reader, unlocks it and loads .PST database from the specified file.
Top
Methods
  NameDescription
Public methodClose
Closes the opened .PST stream.
Public methodEquals
Determines whether the specified object is equal to the current object.
(Inherited from Object.)
Protected methodFinalize
Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection.
(Inherited from Object.)
Public methodGetFolderByID
Gets the PST folder by its ID.
Public methodGetHashCode
Serves as the default hash function.
(Inherited from Object.)
Public methodGetItemByID
Gets the PST item by its ID.
Public methodCode exampleGetPstRootFolders(Boolean)
Gets the list of root folders which are present in the .PST file or stream.
Public methodGetPstRootFolders(Boolean, String)
Gets the list of root folders which are present in the .PST file or stream with specified delimiter which is used as a hierarchy delimiter of separate levels of folder names.
Public methodGetType
Gets the Type of the current instance.
(Inherited from Object.)
Protected methodMemberwiseClone
Creates a shallow copy of the current Object.
(Inherited from Object.)
Public methodToString
Returns a string that represents the current object.
(Inherited from Object.)
Top
Properties
  NameDescription
Public propertyStatic memberLicenseKey Obsolete.
Assigns the license key.
Public propertyTrialDaysLeft
Gets the number of days left to the date of the trial license key expiration.
Top
Remarks

PstReader class parses a .PST file, extracts its tree-like structure (folders and other items like messages, contacts, tasks, etc), and provides access to the extracted data.

You can parse very large .PST files with this method as it does not parse the file at once. Instead, it allows you to iterate though the collection of folders or items at any nesting level, processing items one-by-one and thus reducing memory usage.

Prior to creating instances of this class, the correct license key must be set. See MailBee.Global.LicenseKey property for details.

Examples
This sample enumerates Outlook .PST file as a folder tree and then saves all objects as .EML files in their respective folders. This way, you get the same filesystem structure as you had in .PST file.
Note Note
In this sample, it's assumed the license key is already set outside (such as in app.config file). Otherwise, use MailBee.Global.LicenseKey property to unlock the component.
// To use the code below, import these namespaces at the top of your code.
using System;
using System.IO;
using MailBee.Outlook;
using MailBee.Mime;

class Sample
{
    static void Main(string[] args)
    {
        PstReader reader = new PstReader(@"C:\OutlookMessageBase\Outlook.pst");

        string outPath = @"C:\OutlookMessageBase\PstOutputDir";

        PstFolderCollection pstColl = reader.GetPstRootFolders(true);

        foreach (PstFolder fItem in pstColl)
        {
            int i = 0;
            Console.WriteLine(":::    " + fItem.Name);

            string folderPath = Path.Combine(outPath, fItem.SafeName);
            if (!Directory.Exists(folderPath))
            {
                Directory.CreateDirectory(folderPath);
            }

            foreach (PstItem item in fItem.Items)
            {
                if (item != null && item.PstType == PstItemType.Message)
                {
                    MailMessage msg = ((PstMessage) item).GetAsMailMessage();

                    Console.WriteLine(msg.Subject);

                    string fileName = Path.Combine(folderPath, string.Format("{0}_{1}.eml",
                        PstMessage.MakeStringSafeForFileName(msg.Subject), i++));

                    // Charset fix.
                    msg.Parser.CharsetMetaTagMode = CharsetMetaTagProcessing.RemoveCharsetMetaTag;
                    msg.Charset = "UTF-8";
                    msg.EncodeAllHeaders(System.Text.Encoding.UTF8, HeaderEncodingOptions.None);

                    msg.SaveMessage(fileName);

                    //msg = null;
                    //GC.Collect(); // For big amount of large messages
                }
            }
        }
    }
}
See Also