article

How to Check, Set, Remove or Update Password of PST Files inside .NET Apps

Email
Submitted on: 9/2/2015 9:41:54 AM
By: Sherazam  
Level: Intermediate
User Rating: Unrated
Compatibility: C#, VB.NET
Views: 3043
 
     This technical tip explains how to .NET developers can Set and update password of a PST file using Aspose.Email. Mcrosoft 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, Remove/Reset the Password property from the PST and Setting/Changing PST Password

 
				This technical tip explains how to .NET developers can Set and update password of a PST file using Aspose.Email. Mcrosoft 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
•	Remove/Reset the Password property from the PST
•	Setting/Changing PST Password
The MapiPropertyTag.PR_PST_PASSWORD value from the MapiPropertyTag class is used to check if a file is password protected.The CRC-32 hash of the password string is stored in the PidTagPstPassword (tag = 0x67ff0003) property in the MessageStore. If this property exists and is nonzero, then the PST is password protected. The first code snippets shows two functions that check if the PST is password protected. It also shows how to check whether the provided password is correct or not. Removal of the PR_PST_PASSWORD property cannot be achieved as other properties are removed from a message store. Instead, we need to set its value to zero (0) in order to get it removed. The "Store" property of the PST class allows to access store properties of PST instead of MessageStoreProperties of PST in this case.
// Check PST for Password protection
//C# Code Sample
/// 
/// Determines whether the specified PST is password protected.
/// 
private static bool IsPasswordProtected(PersonalStorage pst)
{
// If the property exists and is nonzero, then the PST file is password protected.
if (pst.MessageStoreProperties.Contains(MapiPropertyTag.PR_PST_PASSWORD))
{
long passwordHash = pst.MessageStoreProperties[MapiPropertyTag.PR_PST_PASSWORD].GetLong();
return passwordHash != 0;
}
return false;
}
/// 
/// Determines whether the specified string is a valid password for the PST.
/// 
private static bool IsPasswordValid(string password, PersonalStorage pst)
{
// If the property exists and is nonzero, then the PST file is password protected.
if (pst.MessageStoreProperties.Contains(MapiPropertyTag.PR_PST_PASSWORD))
{
// The property value contains the CRC-32 hash of the password string of PST.
long passwordHash = pst.MessageStoreProperties[MapiPropertyTag.PR_PST_PASSWORD].GetLong();
return passwordHash != 0 && passwordHash == ComputeCrc32(Encoding.ASCII.GetBytes(password));
}
return false;
}
 
[VB.NET Code Sample]
'
'Determines whether the specified PST is password protected.
'
Private Function IsPasswordProtected(ByVal pst As PersonalStorage) As Boolean
'If the property exists and is nonzero, then the PST file is password protected.
If (pst.MessageStoreProperties.Contains(MapiPropertyTag.PR_PST_PASSWORD)) Then
Dim passwordHash As Long = pst.MessageStoreProperties.Item(MapiPropertyTag.PR_PST_PASSWORD).GetLong()
Return passwordHash <> 0
End If
Return False
End Function
'
'Determines whether the specified string is a valid password for the PST.
'
Private Function IsPasswordValid(ByVal password As String, ByVal pst As PersonalStorage) As Boolean
'If the property exists and is nonzero, then the PST file is password protected.
If (pst.MessageStoreProperties.Contains(MapiPropertyTag.PR_PST_PASSWORD)) Then
'The property value contains the CRC-32 hash of the password string of PST.
Dim passwordHash As Long = pst.MessageStoreProperties.Item(MapiPropertyTag.PR_PST_PASSWORD).GetLong()
Return passwordHash <> 0 AND passwordHash == ComputeCrc32(System.Text.ASCIIEncoding.ASCII.GetBytes(password))
End If
Return False
End Function
// Removing/Reseting the PR_PST_PASSWORD Property
 
//C# Code Sample
PersonalStorage pst = PersonalStorage.FromFile(@"test.pst");
if (pst.Store.Properties.Contains(MapiPropertyTag.PR_PST_PASSWORD))
{
MapiProperty property = new MapiProperty(MapiPropertyTag.PR_PST_PASSWORD, BitConverter.GetBytes((long)0));
pst.Store.SetProperty(property);
}
//[VB.NET Code Sample]
Dim pst As PersonalStorage = PersonalStorage.FromFile("test.pst")
If pst.Store.Properties.Contains(MapiPropertyTag.PR_PST_PASSWORD) Then
	Dim [property] As New MapiProperty(MapiPropertyTag.PR_PST_PASSWORD, BitConverter.GetBytes(CLng(0)))
	pst.Store.SetProperty([property])
End If
// Setting Password on PST Files
//C# Code Sample
using (PersonalStorage pst = PersonalStorage.Create(filename, FileFormatVersion.Unicode))
{
 // set the password
 string password = "qgHgjdLcv63";
 pst.Store.ChangePassword(password);
 // remove the password
 pst.Store.ChangePassword(null);
}
//[VB.NET Code Sample]
Using pst As PersonalStorage = PersonalStorage.Create(filename, FileFormatVersion.Unicode)
	' set the password
	Dim password As String = "qgHgjdLcv63"
	pst.Store.ChangePassword(password)
	' remove the password
	pst.Store.ChangePassword(Nothing)
End Using
Overview: Aspose.Email for .NET
Aspose.Email for .NET is a set of components allowing developers to easily implement email functionality within their ASP.NET web applications, web services & Windows applications. It Supports Outlook PST, EML, MSG & MHT formats. It allows developers to work with SMTP, POP3, FTP & MS Exchange servers. It supports mail merge, iCalendar, customized header & body, header information, embedded files, Twitter & many more. It makes it easy to work with HTML or plain text emails & their attachments.

More about Aspose.Email for .NET


Other 44 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this article (in the Intermediate category)?
(The article with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular article, please click here instead.)
 

To post feedback, first please login.