How to Check, Set, Remove or Update Password of PST Files inside .NET Apps
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
<br/>
<br/>
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
<br/>
<br/>
Protection of a PST, Remove/Reset the Password property from the PST and setting/Changing PST Password. The MapiPropertyTag.PR_PST_PASSWORD value from the
<br/>
<br/>
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 =
<br/>
<br/>
0x67ff0003) property in the MessageStore. If this property exists and is nonzero, then the PST is password protected.
<br/>
//your code here...// 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
Url: http://www.aspose.com/.net/email-component.aspx
Language: C# | User: Sheraz Khan | Created: Sep 2, 2015 | Tags: password protect PST files Work with PST Password Protection Properties Check PST for Password protection Remove PST file Password in C# .NET Email API Resetting PST Password in .NET work with Outlook Personal Storage (PST) files