Over a million developers have joined DZone.

How to Check, Set, Remove, or Update PST File Passwords

Learn how to update and set PST file passwords in .NET apps.

· Web Dev Zone

Start coding today to experience the powerful engine that drives data application’s development, brought to you in partnership with Qlik.

This article explains how .NET developers can set and update the 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.

Create data driven applications in Qlik’s free and easy to use coding environment, brought to you in partnership with Qlik.

Topics:
.net 2.0 ,outlook ,vb.net ,c# ,web dev

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}