Working with Large PST Files
Contents
[
Hide
]
Performance may be degraded when processing large PST files. The following solutions will help you improve the performance of your app when processing large storage files.
Prefer the enumerating methods when traversing the folders or the messages of a PST.
from aspose.email.storage.pst import PersonalStorage
with PersonalStorage.from_file("storage.pst") as pst:
for folder in pst.root_folder.enumerate_folders():
for message_info in folder.enumerate_messages():
# Do something with the message
print(message_info.subject)
Prefer MessageInfo for accessing the basic message properties.
from aspose.email.storage.pst import PersonalStorage
with PersonalStorage.from_file("storage.pst") as pst:
for folder in pst.root_folder.enumerate_folders():
for message_info in folder.enumerate_messages():
print(f"Subject: {message_info.subject}")
print(f"To: {message_info.display_to}")
print(f"Importance: {message_info.importance}")
print(f"Message Class: {message_info.message_class}")
Avoid calling
extract_message or enumerate_mapi_messages for all the messages unless you need access to all their properties.
Consider using
enumerate_messages_entry_id to retrieve all the message ids contained in a folder.
from aspose.email.storage.pst import PersonalStorage
with PersonalStorage.from_file("storage.pst") as pst:
for folder in pst.root_folder.enumerate_folders():
for entry_id in folder.enumerate_messages_entry_id():
# Use the id to retrieve a single property (extract_property),
# extract a MapiMessage (extract_message),
# extract the message attachments (extract_attachments),
# or save the message to a stream (save_message_to_stream).
pass
Consider using
extract_property to read a single property that is missing in MessageInfo.
import base64
from aspose.email.mapi import KnownPropertyList
from aspose.email.storage.pst import PersonalStorage
with PersonalStorage.from_file("storage.pst") as pst:
for folder in pst.root_folder.enumerate_folders():
for msg_id in folder.enumerate_messages_entry_id():
transport_message_headers = pst.extract_property(
base64.b64decode(msg_id),
KnownPropertyList.TRANSPORT_MESSAGE_HEADERS.tag)
Consider using
extract_attachments if only the attachments are required.
from aspose.email.storage.pst import PersonalStorage
with PersonalStorage.from_file("storage.pst") as pst:
for folder in pst.root_folder.enumerate_folders():
for msg_id in folder.enumerate_messages_entry_id():
attachments = pst.extract_attachments(msg_id)
Use search-based filtering to get only the messages you require.
from aspose.email.mapi import MapiMessageFlags
from aspose.email.storage.pst import PersonalStorage, PersonalStorageQueryBuilder
with PersonalStorage.from_file("storage.pst") as pst:
builder = PersonalStorageQueryBuilder()
# Unread messages
builder.has_no_flags(MapiMessageFlags.READ)
for folder in pst.root_folder.enumerate_folders():
unread = folder.get_contents(builder.get_query())
Consider using
save_message_to_stream when it is necessary to save the messages from a PST.
Instead of using:
from aspose.email.storage.pst import PersonalStorage
with PersonalStorage.from_file("storage.pst") as pst:
for folder in pst.root_folder.enumerate_folders():
for entry_id in folder.enumerate_messages_entry_id():
msg = pst.extract_message(entry_id)
msg.save("message.msg")
use:
from aspose.email.storage.pst import PersonalStorage
with PersonalStorage.from_file("storage.pst") as pst:
for folder in pst.root_folder.enumerate_folders():
for entry_id in folder.enumerate_messages_entry_id():
with open("message.msg", "wb") as stream:
pst.save_message_to_stream(entry_id, stream)
Aspose.Email for .NET additionally offers asynchronous PST operations —
CreateAsync, FromFileAsync, MergeWithAsync and SplitIntoAsync. These methods are based on .NET tasks and cancellation tokens and are not exposed in Aspose.Email for Python via .NET; use their synchronous counterparts instead.