如何为Outlook MailItem设置加密标志?

问题描述 投票:0回答:1

我需要能够使用Excel VSTO加载项以编程方式发送加密的密码电子邮件,因此我编写了此静态方法来发送Outlook电子邮件:

private static Outlook.Application outlookApp;
private const string PR_SECURITY_FLAGS = "http://schemas.microsoft.com/mapi/proptag/0x6E010003";

public static void SendPasswordEmail(string emailAddress, string password, string subject, bool PGP)
        {
            GetOutlookApp();
            Outlook.MailItem eMail = (Outlook.MailItem)outlookApp.CreateItem(Outlook.OlItemType.olMailItem);
            // Check if the Email should be prepared to be sent with PGP or PKI encryption.
            if (PGP)
            {
                eMail.Subject = String.Concat("PGP: ", subject);
            }
            else
            {
                eMail.Subject = subject;
                // Set the Security Flags of the current MailItem to encrypted.
                // already tried 1,2
                eMail.PropertyAccessor.SetProperty("PR_SECURITY_FLAGS", 1);
            }
            String eMailBody = String.Format(@Properties.Resources.PasswordEmailBody, password);
            eMail.To = emailAddress;
            eMail.BodyFormat = Outlook.OlBodyFormat.olFormatHTML;
            eMail.HTMLBody = eMailBody;
            eMail.Send();
        }

我正在使用此方法来获取Outlook应用程序:

private static void GetOutlookApp()
        {
            // Check whether there is an Outlook process running.
            if (Process.GetProcessesByName("OUTLOOK").Count() > 0)
            {
                // If so, use the GetActiveObject method to obtain the process and cast it to an Application object.
                outlookApp = Marshal.GetActiveObject("Outlook.Application") as Outlook.Application;
            }
            else
            {
                // If not, create a new instance of Outlook and sign in to the default profile.
                outlookApp = new Outlook.Application();
                Outlook.NameSpace session = outlookApp.GetNamespace("MAPI");
                session.Logon("", "", false, true);
            }
        }

但是我似乎无法将MailItem的安全性标志设置为加密。没有异常抛出!只是Outlook不会以加密方式发送电子邮件!

我有这个运行良好的VBA代码:

Const PR_SECURITY_FLAGS = "http://schemas.microsoft.com/mapi/proptag/0x6E010003"
Dim prop As Long

Set MailItem = oApp.CreateItem(olMailItem)
MailItem.To = something
MailItem.Subject = something
MailItem.BodyFormat = olFormatPlain
MailItem.Body = something

// set encrypted flag
Set MailItem = Application.ActiveInspector.CurrentItem
prop = CLng(MailItem.PropertyAccessor.GETPROPERTY(PR_SECURITY_FLAGS))
ulFlags = ulFlags Or &H1 ' SECFLAG_ENCRYPTED
MailItem.PropertyAccessor.SetProperty PR_SECURITY_FLAGS, ulFlags
MailItem.SEND

您能告诉我这个代码块到底在做什么吗?

Set MailItem = Application.ActiveInspector.CurrentItem
prop = CLng(MailItem.PropertyAccessor.GETPROPERTY(PR_SECURITY_FLAGS))
ulFlags = ulFlags Or &H1 ' SECFLAG_ENCRYPTED
MailItem.PropertyAccessor.SetProperty PR_SECURITY_FLAGS, ulFlags

是否有可用的MAPI属性及其接受的值的引用或列表?

c# encryption outlook vsto
1个回答
0
投票

您必须使用PR_SECURITY_FLAGS属性的DASL名称,而不是属性名称。例如:

private const string PR_SECURITY_FLAGS = "http://schemas.microsoft.com/mapi/proptag/0x6E010003";
eMail.PropertyAccessor.SetProperty(PR_SECURITY_FLAGS, 1);

更多信息,请参见How to sign or encrypt a message programmatically from OOM


推荐问答