Working with PST Password Protection Properties
Microsoft Outlook lets users password protect PST files to restrict access to them. Aspose.Email can detect password protection on a PST file. This article shows how to:
- Check for Password Protection of a PST
- Read Password Protected PST Files
- Validate Password in Password Protected PST
- Add/Change/Remove Password in PST Files
Check for Password protection
To check if a PST file is protected with a password, use the is_password_protected method of the MessageStore class as shown in the code sample below:
import aspose.email as ae
pst = ae.storage.pst.PersonalStorage.from_file("my.pst")
print(f"The storage is password protected - {pst.store.is_password_protected}")
Read Password Protected PST Files
You can read password-protected files just like regular unprotected pst files. The following code snippet allows you to access each individual message with the possibility of its further processing:
import aspose.email as ae
pst = ae.storage.pst.PersonalStorage.from_file("my.pst")
for folder in pst.root_folder.get_sub_folders():
for msg in folder.enumerate_messages():
# do something
Validate Password in Password Protected PST
To check if a password in a PST file is valid, Aspose.Email provides the is_password_valid(password) method of the MessageStore class. It takes the string password as a parameter and returns True if the password is correct and False if it is incorrect.
The following code snippet demonstrates the use of is_password_valid(password) method.
import aspose.email as ae
pst = ae.storage.pst.PersonalStorage.from_file("my.pst")
print(f"Password is valid - {pst.store.is_password_valid('Password1')}")
Add/Change/Remove Password in PST Files
The change_password(password) method of the MessageStore class is used to manipulate passwords in PST files. The following code sample shows how to add, change or remove a password:
import aspose.email as ae
pst = ae.storage.pst.PersonalStorage.create("SetPasswordOnPST_out.pst", ae.storage.pst.FileFormatVersion.UNICODE)
# Add or change the password
password = "Password1"
pst.store.change_password(password)
# Remove the password
pst.store.change_password(None)