Partager via


How to sign or encrypt a message programmatically from OOM

Q: How do I signal to Outlook to sign and / or encrypt a message from the Outlook object model?

A: There is not a first-class object or property to do this within the object model.  However, you can set the MAPI property PR_SECURITY_FLAGS.  The code below demonstrates this using the PropertyAccessor object:

Sub SignAndEncrypt()

    Const PR_SECURITY_FLAGS = "schemas.microsoft.com/mapi/proptag/0x6E010003"

    Dim mi As MailItem

    Set mi = Application.ActiveInspector.CurrentItem

    Dim prop As Long

    prop = CLng(mi.PropertyAccessor.GetProperty(PR_SECURITY_FLAGS))

    ulFlags = ulFlags Or &H1 ' SECFLAG_ENCRYPTED

    ulFlags = ulFlags Or &H2 ' SECFLAG_SIGNED

    mi.PropertyAccessor.SetProperty PR_SECURITY_FLAGS, ulFlags

    Set mi = Nothing

End Sub

Comments

  • Anonymous
    August 18, 2010
    This was a GREAT help to me! At my agency they are both ON by default. I needed to set both to OFF via Access VBA. After some experimentation I found it can be done by scrapping ulFlags and prop and chaging the SetProperty call to.... objOutlookMsg.PropertyAccessor.SetProperty PR_SECURITY_FLAGS, -4 Note I'm also not DIM'ing or setting mi. I'm just using the objOutlookMsg object that's already set in my app. Anyway, thanks for posting this!

  • Anonymous
    December 04, 2013
    best method and shortest to get it done !!!

  • Anonymous
    October 12, 2014
    Nick, your code is very useful. Thanks for sharing

  • Anonymous
    May 03, 2018
    I have no exp with VBA but I think I got a Idea of what the code is doing. Is that also possible with PowerShell and Outlook 2010? If I follow the Schema URL, there is nothing - is that normal?

    • Anonymous
      May 03, 2018
      Worked well in PowerShell with Outlook 2010. Thank you! <3$PR_SECURITY_FLAGS = "http://schemas.microsoft.com/mapi/proptag/0x6E010003"$Outlook = New-object -ComObject Outlook.Application$Mail = $Outlook.CreateItem(0)$Mail.To = "mail@company.tld"$Mail.Subject ="Test"$Mail.Body ="Test Body"# 0 = nix, 1 = encrypt, 2 = sign, 3 = both!$Mail.PropertyAccessor.SetProperty($PR_SECURITY_FLAGS, 3)#$Mail.Send()$Mail.Display()