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

时间:2020-01-03 14:23:48

标签: c# encryption outlook vsto

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

2020-01-03 16:08:39.606 1386-1386/example.example.com.myApplication E/AndroidRuntime: FATAL EXCEPTION: main
Process: example.example.com.myApplication, PID: 1386
java.lang.RuntimeException: java.lang.ClassCastException: java.util.ArrayList cannot be cast to java.io.File
    at android.app.job.JobServiceEngine$JobHandler.handleMessage(JobServiceEngine.java:112)
    at android.os.Handler.dispatchMessage(Handler.java:106)
    at android.os.Looper.loop(Looper.java:214)
    at android.app.ActivityThread.main(ActivityThread.java:7156)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:494)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:975)
 Caused by: java.lang.ClassCastException: java.util.ArrayList cannot be cast to java.io.File
    at example.example.com.myApplication.model.DocumentJob.callRestApi(DocumentJob.java:82)
    at example.example.com.myApplication.service.RestApiJobService.onStartJob(RestApiJobService.java:81)
    at android.app.job.JobService$1.onStartJob(JobService.java:62)
    at android.app.job.JobServiceEngine$JobHandler.handleMessage(JobServiceEngine.java:108)
    at android.os.Handler.dispatchMessage(Handler.java:106) 
    at android.os.Looper.loop(Looper.java:214) 
    at android.app.ActivityThread.main(ActivityThread.java:7156) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:494) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:975) 

我正在使用这种方法来获取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();
        }

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

我有运行良好的VBA代码:

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);
            }
        }

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

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

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

1 个答案:

答案 0 :(得分: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