MailBee.NET Objects 7.1

PstReader Class

Provides methods for parsing Outlook .PST message database file into a collection of folders and items.

For a list of all members of this type, see PstReader Members.

System.Object
   MailBee.Outlook.PstReader

public class PstReader

Thread Safety

Public static (Shared in Visual Basic) members of this type are safe for multithreaded operations. Instance members are not guaranteed to be thread-safe.

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.

Example

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   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.
[C#]
// 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
                }
            }
        }
    }
}
[Visual Basic]
' To use the code below, import these namespaces at the top of your code.
Imports System
Imports System.IO
Imports MailBee.Outlook
Imports MailBee.Mime

Class Sample
    Shared Sub Main(ByVal args() As String)
        Dim reader As PstReader = New PstReader("C:\OutlookMessageBase\Outlook.pst")

        Dim outPath As String = "C:\OutlookMessageBase\PstOutputDir"

        Dim pstColl As PstFolderCollection = reader.GetPstRootFolders(True)

        Dim fItem As PstFolder
        For Each fItem In pstColl
            Dim i As Integer = 0
            Console.WriteLine(":::    " + fItem.Name)

            Dim folderPath As String = Path.Combine(outPath, fItem.SafeName)
            If Not Directory.Exists(folderPath) Then
                Directory.CreateDirectory(folderPath)
            End If

            Dim item As PstItem
            For Each item In fItem.Items
                If Not item Is Nothing And item.PstType = PstItemType.Message Then
                    Dim msg As MailMessage = (CType(item, PstMessage)).GetAsMailMessage()

                    Console.WriteLine(msg.Subject)

                    i = i + 1
                    Dim fileName As String = 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 = Nothing
                    'GC.Collect() 'For big amount of large messages
                End If
            Next
        Next
    End Sub
End Class

Requirements

Namespace: MailBee.Outlook

Assembly: MailBee.NET (in MailBee.NET.dll)

See Also

PstReader Members | MailBee.Outlook Namespace