Working with large PST files
Contents
[
Hide
]
Performance may be degraded when processing large PST files. The following suggestions will help you to improve the performance of your app when processing large files.
Consider methods returning
IEnumerable
when traversing folders or messages in a pst.
using var pst = PersonalStorage.FromFile(@"storage.pst");
foreach (var folder in pst.RootFolder.EnumerateFolders())
foreach (var messageInfo in folder.EnumerateMessages())
{
// Do something with message
}
Prefer MessageInfo for accessing basic message properties.
foreach (var messageInfo in folder.EnumerateMessages())
{
Console.WriteLine($"Subject: {messageInfo.Subject}");
Console.WriteLine($"To: {messageInfo.DisplayTo}");
Console.WriteLine($"Importance: {messageInfo.Importance}");
Console.WriteLine($"Message Class: {messageInfo.MessageClass}");
}
Avoid using the ExtractMessage or EnumerateMapiMessages methods for all messages unless you need to have access to all properties.
Consider using EnumerateMessagesEntryId to easily retrieve all message IDs contained in a folder.
foreach (var id in folder.EnumerateMessagesEntryId())
{
// Use id to retrieve a property (ExtractProperty),
// extract a MapiMessage (ExtractMessage),
// extarct message attachments (ExtractAttachments),
// save msg to a stream(SaveMessageToStream).
}
Consider using ExtractProperty to read a single property that is missing in MessageInfo.
foreach (var msgId in folder.EnumerateMessagesEntryId())
{
var transportMessageHeaders =
pst.ExtractProperty(Convert.FromBase64String(msgId), KnownPropertyList.TransportMessageHeaders.Tag)
.GetString();
}
Consider using ExtractAttachments if only the attachments are required.
foreach (var msgId in folder.EnumerateMessagesEntryId())
{
var attachments = pst.ExtractAttachments(msgId);
}
Use seach criteria-based filtering to get the messages you require.
using var pst = PersonalStorage.FromFile(@"storage.pst");
var builder = new PersonalStorageQueryBuilder();
// Unread messages
builder.HasNoFlags(MapiMessageFlags.MSGFLAG_READ);
foreach (var folder in pst.RootFolder.EnumerateFolders())
{
var unread = folder.GetContents(builder.GetQuery());
}
Consider using SaveMessageToStream if it is necessary to save messages from pst.
Instead of using:
foreach (var id in folder.EnumerateMessagesEntryId())
{
var msg = pst.ExtractMessage(id);
msg.Save(@"message.msg");
}
Use:
foreach (var id in folder.EnumerateMessagesEntryId())
{
pst.SaveMessageToStream(id, File.OpenWrite(@"message.msg"));
}