Hello,
I running the example at: https://docs.aspose.com/email/python-net/working-with-messages-in-a-pst-file/#extracting-messages-form-pst-files using the example pst file provided Outlook.pst.
i.e.
from aspose.email.storage.pst import *
from aspose.email.mapi import MapiMessage
dataDir = “Examples/data/”
file = dataDir + “Outlook.pst”
pst = PersonalStorage.from_file(file)
folderInfo = pst.root_folder
messageInfoCollection = folderInfo.get_contents()
print(len(messageInfoCollection))
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)
This returns len = 0 and find no messages:
The example that traverses the folders here seems to work OK:
pst = PersonalStorage.from_file(pstfile)
folderInfoCollection = pst.root_folder.get_sub_folders()
for folderInfo in folderInfoCollection:
print("Folder: " + folderInfo.display_name)
print(“Total Items: " + str(folderInfo.content_count))
print(“Total Unread Items: " + str(folderInfo.content_unread_count))
print(”----------------------”)
@richardgmcmahon
It seems that you are encountering an issue where the message count returned from the PST file is zero. This can happen for a few reasons. Here are some steps you can take to troubleshoot and resolve the issue:
-
Check the PST File: Ensure that the Outlook.pst
file you are using actually contains messages. You can open it in Microsoft Outlook to verify that there are emails present.
-
Folder Structure: The code you provided retrieves messages from the root folder. If your messages are located in subfolders, you will need to traverse those subfolders as well. You can modify your code to include a recursive function that checks all folders and subfolders for messages.
-
Code Example: Here’s an example of how you can modify your code to extract messages from all folders:
from aspose.email.storage.pst import PersonalStorage
from aspose.email.mapi import MapiMessage
def extract_messages(folder_info, pst):
message_info_collection = folder_info.get_contents()
for message_info in message_info_collection:
mapi = pst.extract_message(message_info)
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)
# Recursively extract messages from subfolders
for subfolder in folder_info.get_sub_folders():
extract_messages(subfolder, pst)
dataDir = "Examples/data/"
file = dataDir + "Outlook.pst"
pst = PersonalStorage.from_file(file)
root_folder = pst.root_folder
extract_messages(root_folder, pst)
-
Error Handling: Make sure to include error handling in your code to catch any exceptions that may occur while accessing the PST file or extracting messages.
By following these steps, you should be able to extract messages from your PST file successfully. If you continue to experience issues, please double-check the contents of your PST file and ensure that your code is correctly set up to access all folders.
If you need further assistance, feel free to ask!
Thanks for the quick response. Your recursive example worked OK. Note I was using the Aspose provided example pst file and code.
Hello @richardgmcmahon,
We appreciate you bringing this to our attention and we will update the example.
You are right — typically, the root folder does not contain any messages. It usually only holds subfolders. Therefore, the example will always return 0 messages.
Thank you for the feedback.