![](/img/trans.png)
[英]How to encrypt an outlook mail created from Excel VBA (in Outlook > options > Encrypt)
[英]Encrypt Outlook Mail Programmatically via VBA
我正在尋找一種通過 Outlook 2013 中的 VBA 代碼加密和發送 Outlook 郵件的方法。
我希望我可以訪問郵件對象並調用類似“加密”的方法。
微軟表示,“Microsoft Outlook 對象模型不提供直接支持以編程方式簽署或加密郵件消息”,但可以為其構建解決方案。 ( https://support.microsoft.com/de-de/help/2636465/how-to-sign-or-encrypt-mail-messages-programmatically )
我知道我可以手動加密郵件,但我想以編程方式訪問它。 也許我可以像事件一樣調用,或者在設置此屬性時調用的東西。
我沒有任何證書。 有沒有辦法在不使用證書的情況下加密 Outlook 中的郵件?
您需要設置 PR_SECURITY_FLAGS 屬性。 有關示例,請參閱https://blogs.msdn.microsoft.com/dvespa/2009/03/16/how-to-sign-or-encrypt-a-message-programmatically-from-oom/ 。
這個信息出奇地難找。 如果上面的鏈接失效,這里是一個實現設置 PR_SECURITY_FLAGS 屬性的函數。
'---------------------------------------------------------------------------------------
' 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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.