简体   繁体   中英

Encrypt Outlook Mail Programmatically via VBA

I am looking for a way to encrypt and send Outlook mail via VBA code in Outlook 2013.

I was hoping I could access the mail object and call something like an "encrypt" Method.

Microsoft says, that "The Microsoft Outlook object model does not provide direct support to sign or encrypt mail messages programmatically", but it is possible to build a solution for it. ( https://support.microsoft.com/de-de/help/2636465/how-to-sign-or-encrypt-mail-messages-programmatically )

I know I can encrypt mails manually, but I would like to access it programmatically. Perhaps I can call like an event or something that is called when this property is set.

I do not have any Certificates. Is there even a way to encrypt mails in Outlook without using Certificates?

You will need to set the PR_SECURITY_FLAGS property. See https://blogs.msdn.microsoft.com/dvespa/2009/03/16/how-to-sign-or-encrypt-a-message-programmatically-from-oom/ for an example.

This information is surprisingly hard to find. In case the above link dies, here is a function that implements setting the PR_SECURITY_FLAGS property.

'---------------------------------------------------------------------------------------
' Procedure : Mailitem_SignEncr
' Date      : 2019-06-11
' Author    : Andre 
' Purpose   : Set security flags for an Outlook Mailitem
'
' Source: https://blogs.msdn.microsoft.com/dvespa/2009/03/16/how-to-sign-or-encrypt-a-message-programmatically-from-oom/
' Parameters:
' oItem: If your code runs in Outlook VBA, you can use this to get the current mail: Set oItem = Application.ActiveInspector.CurrentItem
'        Otherwise you get this object when creating the new mail item.
' doSign: Digital Signature. +1 = ON, -1 = OFF, 0 = leave default
' doEncr: Encryption.        +1 = ON, -1 = OFF, 0 = leave default
'---------------------------------------------------------------------------------------
'
Public Sub Mailitem_SignEncr(oItem As Outlook.MailItem, doSign As Long, doEncr As Long)

    Const PR_SECURITY_FLAGS = "http://schemas.microsoft.com/mapi/proptag/0x6E010003"
    Const SECFLAG_ENCRYPTED As Long = &H1
    Const SECFLAG_SIGNED As Long = &H2

    Dim SecFlags As Long

    ' Get current flags value
    SecFlags = oItem.PropertyAccessor.GetProperty(PR_SECURITY_FLAGS)

    ' Turn flags on/off

    If doSign > 0 Then
        ' ON
        SecFlags = SecFlags Or SECFLAG_SIGNED
    ElseIf doSign < 0 Then
        ' OFF
        SecFlags = SecFlags And (Not SECFLAG_SIGNED)
    Else
        ' leave this flag as it is
    End If

    If doEncr > 0 Then
        SecFlags = SecFlags Or SECFLAG_ENCRYPTED
    ElseIf doEncr < 0 Then
        SecFlags = SecFlags And (Not SECFLAG_ENCRYPTED)
    End If

    ' and set the modified flags
    oItem.PropertyAccessor.SetProperty PR_SECURITY_FLAGS, SecFlags

End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM