Working with Messages in a PST File
Adding Messages to PST Files
Create a New PST File and Add Subfolders showed how to create a PST file and add a subfolder to it. With Aspose.Email you can add messages to subfolders of a PST file that you have created or loaded. This article adds two messages from disk to the Inbox subfolder of a PST. Use the PersonalStorage and FolderInfo classes to add messages to PST files. To add messages to a PST file’s Inbox folder:
- Create an instance of the FolderInfo class and load it with the contents of the Inbox folder.
- Add messages from disk to the Inbox folder by calling the FolderInfo.AddMessage() method. The FolderInfo class exposes the AddMessages method that enables to add large number of messages to the folder, reducing I/O operations to disc and improving performance. A complete example can be found below, in Adding Bulk Messages.
The code snippets below shows how to add messages to a PST subfolder called Inbox.
Adding Bulk Messages
Adding individual messages to a PST implies more I/O operations to disc and hence may slow down performance. For improved performance, messages can be added to the PST in bulk mode to minimize I/O operations. The AddMessages(IEnumerable
Loading Messages from Disc
The following code snippet shows you how to loading messages from disc.
IEnumerable Implementation
The following code snippet shows you how to IEnumerable Implementation.
Adding Messages from Other PST
For adding messages from the another PST, use the FolderInfo.EnumerateMapiMessages() method that returns IEnumerable
Get Messages Information from an Outlook PST File
In Read Outlook PST File and Get Folders and Subfolders Information, we discussed loading an Outlook PST file and browse its folders to get the folder names and the number of messages in them. This article explains how to read all the folders and subfolders in the PST file and display the information about messages, for example, subject, sender, and recipients. The Outlook PST file may contain nested folders. To get message information from these, as well as the top-level folders, use a recursive method to read all the folders. The following code snippet shows you how to reads an Outlook PST file and display the folder and message contents recursively.
Extracting Messages Form PST Files
This article shows how to read Microsoft Outlook PST files and extract messages. Below is a code snippet showing how to extract messages from a PST file.
from aspose.email.storage.pst import *
from aspose.email.mapi import MapiMessage
pst = PersonalStorage.from_file("Outlook.pst")
folderInfo = pst.root_folder
messageInfoCollection = folderInfo.get_contents()
for messageInfo in messageInfoCollection:
mapi = pst.extract_message(messageInfo)
print("Subject: " + mapi.subject)
print("Sender name: " + mapi.sender_name)
print("Sender email address: " + mapi.sender_email_address)
print("To: ", mapi.display_to)
print("Cc: ", mapi.display_cc)
print("Bcc: ", mapi.display_bcc)
print("Delivery time: ", str(mapi.delivery_time))
print("Body: " + mapi.body)
Delete Messages from PST Files
Add Messages to PST Files showed how to add messages to PST files. It is, of course, also possible to delete items (contents) from a PST file and it may also be desirable to delete messages in bulk. Items from a PST file can be deleted using the FolderInfo.DeleteChildItem() method. The API also provides FolderInfo.DeleteChildItems() method to delete items in bulk from the PST file.
Deleting Messages from PST Files
This articles shows how to Use the FolderInfo class to access specific folders in a PST file. To delete messages from the Sent subfolder of a previously loaded or created PST file:
- Create an instance of the FolderInfo class and load it with the contents of the sent subfolder.
- Delete messages from the Sent folder by calling the FolderInfo.DeleteChildItem() method and passing the MessageInfo.EntryId as a parameter. The following code snippet shows you how to delete messages from a PST file’s Sent subfolder.
Delete Items in Bulk from PST File
Aspose.Email API can be used to delete items in bulk from a PST file. This is achieved using the DeleteChildItems() method which accepts a list of Entry ID items referring to the items to be deleted. The following code snippet shows you how to delete Items in bulk from PST file.
Search Messages and Folders in a PST by Criterion
Personal Storage (PST) files can contain a huge amount of data and searching for data that meets a specific criteria in such large files needs to include multiple check points in the code to filter the information. With the PersonalStorageQueryBuilder class, Aspose.Email makes it possible to search for specific records in a PST based on a specified search criteria. A PST can be searched for messages based on search parameters such as sender, receiver, subject, message importance, presence of attachments, message size, and even message ID. The PersonalStorageQueryBuilder can also be used to search for subfolders.
Searching Messages and Folders in PST
The following code snippet shows you how to use the PersonalStorageQueryBuilder class to search for contents in a PST based on different search criteria. For example, it shows searching a PST based on:
- Message importance.
- Message class.
- Presence of attachments.
- Message size.
- Unread messages.
- Unread messages with attachments, and
- folders with specific subfolder name.
Searching for a String in PST with the Ignore Case Parameter
The following code snippet shows you how to search for a string in PST with the ignore case parameter.
Move Items to Other Folders of PST File
Aspose.Email makes it possible to move items from a source folder to another folder in the same Personal Storage (PST) file. This includes:
- Moving a specified folder to a new parent folder.
- Moving a specified messages to a new folder.
- Moving the contents to a new folder.
- Moving subfolders to a new parent folder.
The following code snippet shows you how to move items such as messages and folders from a source folder to another folder in the same PST file.
Updating Message Properties in a PST File
It’s sometimes required to update certain properties of messages such as changing the subject, marking message importance and similarly others. Updating a message in a PST file, with such changes in the message properties, can be achieved using the FolderInfo.ChangeMessages method. This article shows how to update messages in bulk in a PST file for changes in the properties. The following code snippet shows you how to update properties of messages in bulk mode for multiple messages in a PST file.
Updating Custom Properites in a PST File
Sometimes its required to mark items that are processed with in the PST file. Aspose.Email API allows to achieve this using the MapiProperty and MapiNamedProperty. The following methods are helpful in achieving this.
- ctor MapiNamedProperty(long propertyTag, string nameIdentifier, Guid propertyGuid, byte[] propertyValue)
- ctor MapiNamedProperty(long propertyTag, long nameIdentifier, Guid propertyGuid, byte[] propertyValue)
- FolderInfo.ChangeMessages(MapiPropertyCollection updatedProperties) - changes all messages in folder
- PersonalStorage.ChangeMessage(string entryId, MapiPropertyCollection updatedProperties) - change message properties
Extract Attachments without Extracting Complete Message
Aspose.Email API can be used to extract attachments from PST messages without extracting the complete message first. The ExtractAttachments method of IEWSClient can be used to do this. The following code snippet shows you how to extract attachments without extracting complete message.
Adding Files to PST
Microsoft Outlook’s key functionality is managing emails, calendars, tasks, contacts and journal entries. In addition, files can also be added to a PST folder and the resulting PST keeps record of the documents added. Aspose.Email provides the facility to add files to a folder in the same way in addition to adding messages, contacts, tasks and journal entries to PST. The following code snippet shows you how to add documents to a PST folder using Aspose.Email.