Working with large PST files
Contents
[
Hide
]
Performance may be degraded when processing large PST files. The following suggestions will help improve the performance of your app when processing large files.
Consider methods returning
Iterable
when traversing folders or messages in a pst.
try (PersonalStorage pst = PersonalStorage.fromFile("storage.pst")) {
for (FolderInfo folder : pst.getRootFolder().enumerateFolders())
for (MessageInfo messageInfo : folder.enumerateMessages()) {
// Do something with message
}
}
Prefer MessageInfo for accessing basic message properties.
for (MessageInfo messageInfo : folder.enumerateMessages()) {
System.out.println("Subject: " + messageInfo.getSubject());
System.out.println("To: " + messageInfo.getDisplayTo());
System.out.println("Importance: " + messageInfo.getImportance());
System.out.println("Message Class: " + messageInfo.getMessageClass());
}
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.
for (String id : 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.
for (String msgId : folder.enumerateMessagesEntryId()) {
String transportMessageHeaders =
pst.extractProperty(org.apache.commons.codec.binary.Base64.decodeBase64(msgId),
KnownPropertyList.TRANSPORT_MESSAGE_HEADERS.getTag()).getString();
}
Consider using ExtractAttachments if only the attachments are required.
for (String msgId : folder.enumerateMessagesEntryId()) {
MapiAttachmentCollection attachments = pst.extractAttachments(msgId);
}
Use seach criteria-based filtering to get the messages you require.
try (PersonalStorage pst = PersonalStorage.fromFile("storage.pst")) {
PersonalStorageQueryBuilder builder = new PersonalStorageQueryBuilder();
// Unread messages
builder.hasNoFlags(MapiMessageFlags.MSGFLAG_READ);
for (FolderInfo folder : pst.getRootFolder().enumerateFolders()) {
MessageInfoCollection unread = folder.getContents(builder.getQuery());
}
}
Consider using SaveMessageToStream if it is necessary to save messages from pst.
Instead of using:
for (String id : folder.enumerateMessagesEntryId()) {
MapiMessage msg = pst.extractMessage(id);
msg.save("message.msg");
}
Use:
for (String id : folder.enumerateMessagesEntryId()) {
try (OutputStream fos = new FileOutputStream("message.msg")) {
pst.saveMessageToStream(id, fos);
}
}