Ho to determine if mailitem is encrypted in Outlook 365?

Dan Garkusha 96 Reputation points
2021-07-21T17:27:44.5+00:00

I am writing a VSTO Addin for Outlook 365.
I need to be able to determine in code if an email (MailItem) is encrypted.
I've tried examining the following properties - both indicate that the email is not encrypted but it clearly is:

mailItem.MessageClass -> "IPM.Note"  
mailItem.PropertyAccessor.GetProperty(PR_SECURITY_FLAGS) -> 0  

What am I doing wrong?

116766-outlook-encrypted-details.png

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,180 questions
Office Development
Office Development
Office: A suite of Microsoft productivity software that supports common business tasks, including word processing, email, presentations, and data management and analysis.Development: The process of researching, productizing, and refining new or existing technologies.
3,453 questions
Outlook Management
Outlook Management
Outlook: A family of Microsoft email and calendar products.Management: The act or process of organizing, handling, directing or controlling something.
4,863 questions
{count} votes

Accepted answer
  1. Dan Garkusha 96 Reputation points
    2021-07-27T16:57:32.817+00:00

    Thanks for your efforts, Timon.
    I am sure there is an answer somewhere out there...

    I've looked at everything there is in the MailItem object model using OutlookSpy

    The only properties that I found to be of any use are these two:

    On the encrypted email:

    mailItem.PropertyAccessor.GetProperty(PR_ICON_INDEX) -> 306  
    mailItem.PropertyAccessor.GetProperty(PR_BODY_W) -> "This message is protected with Microsoft Information Protection..."  
    

    118298-image.png


2 additional answers

Sort by: Most helpful
  1. Ayers, Chris 0 Reputation points
    2023-12-12T15:58:01.7666667+00:00

    Thanks for the insights on encryption. All the solutions I could find on checking encryption failed for me as well. Your solution of looking for the PR_ICON_INDEX worked, however I have found two values relating to encryption: 306 and 308. I will add them here if I detect anymore.

    Belt and braces(!) I also wrote my code to support the proptag http://schemas.microsoft.com/mapi/proptag/0x6E010003 in case I encounter any mails with this set.

    Function isEncrypted(inboxItem As Object) As Boolean
    
        ' Code from https://learn.microsoft.com/en-us/answers/questions/484515/ho-to-determine-if-mailitem-is-encrypted-in-outloo
    
        Dim PropAccessor As Outlook.PropertyAccessor
        On Error GoTo isEncryptedError
        
        isEncrypted = False
    
        Set PropAccessor = inboxItem.PropertyAccessor
        If (PropAccessor.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x10800003") = 306 Or _
            PropAccessor.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x10800003") = 308) Then '
                isEncrypted = True
        End If
    
        If PropAccessor.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x6E010003") And 1 <> 0 Then   '
            isEncrypted = True
        End If
    
    isEncryptedExit:
        Set PropAccessor = Nothing
        Exit Function
    
    isEncryptedError:
        isEncrypted = False
        Resume isEncryptedExit
                   
    End Function
    
    0 comments No comments

  2. Timon Yang-MSFT 9,571 Reputation points
    2021-07-22T03:10:32.81+00:00

    I checked many documents of Microsoft.Office.Interop.Outlook, and tried other packages (EWS), but unfortunately, I didn't find any package that has a property to indicate whether this email is encrypted or not.

    But when I tried it, I found that we can use its HtmlBody to determine whether the current email is encrypted. Normal emails show normal content, while encrypted emails look like this:
    116877-tempsnip.png
    We can check whether the body contains specific text. This method may not be very elegant, but it should work.

    You decide whether to use it.

    Part of the code I used:

                ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2013_SP1);  
                service.Credentials = new WebCredentials("test@outlook.com", "password");  
                service.Url = new Uri("https://outlook.office365.com/EWS/Exchange.asmx");  
      
                Mailbox mb = new Mailbox("test@outlook.com");  
                FolderId fid = new FolderId(WellKnownFolderName.Inbox, mb);  
                FindItemsResults<Item> findResults;  
                ItemView view = new ItemView(10);  
      
                findResults = service.FindItems(WellKnownFolderName.Inbox, view);  
                foreach (var item in findResults.Items)  
                {  
                    item.Load();  
                    Console.WriteLine(item.Subject);  
                    Console.WriteLine(item.Body);  
                    Console.WriteLine("~~~~~~~~~~~~~~~~~~~~~~~~~");  
                }  
                Console.Read();  
    

    Update(7/23):

    You can also use Microsoft.Office.Interop.Outlook to get the HtmlBody of the email.

    It seems that this library also doesn't provide a ready-made property for identifying whether the email is encrypted, so I can only do it in the same way, just switch the library used.

                Application outlookApplication = new Application();  
                NameSpace outlookNamespace = outlookApplication.GetNamespace("MAPI");  
      
                MAPIFolder theInbox = outlookNamespace.Folders[1];  
                MAPIFolder re = theInbox.Folders["InBox"];  
                Items mailItems = re.Items;  
                  
                var mail = mailItems.GetFirst();  
    

    117314-1.png


    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.