Home > Code > C# > How to Check, Set, Remove or Update Password of PST Files inside .NET Apps

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

by sher azam   on Sep 02, 2015   Category: C#   |  Views: 865    |  Points: 25   |  Gold 


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

/// <summary>
/// Determines whether the specified PST is password protected.
/// </summary>
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;
}

/// <summary>
/// Determines whether the specified string is a valid password for the PST.
/// </summary>
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]

'<summary>
'Determines whether the specified PST is password protected.
'</summary>
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

'<summary>
'Determines whether the specified string is a valid password for the PST.
'</summary>
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.

- Homepage of Aspose.Email for .NET: http://www.aspose.com/.net/email-component.aspx

- Download Aspose.Email for .NET: http://www.aspose.com/community/files/51/.net-components/aspose.email-for-.net/default.aspx



Post Code  |  Code Snippet Home

User Responses


No response found, be the first to review this code snippet.

Submit feedback about this code snippet

Please sign in to post feedback

Latest Posts