使用Inspector以编程方式加密Outlook电子邮件

问题描述:

我使用C#与Outlook对象模型(由于授权,兑换不是我的选项),而且我在发送邮件之前编程加密电子邮件时遇到困难。使用Inspector以编程方式加密Outlook电子邮件

我可以成功地获得对CommandBarButton的引用,该引用应该表示加密按钮(根据在线示例的Id 718),但我无法以编程方式将其压缩。我尝试使用CommandBarButton Execute()方法和使用SendKeys(不知道在这种情况下,如果sendkeys甚至是有效的)。所有debug.writeline语句都显示该按钮处于msoButtonUp状态。

我一直在玩这个天,似乎无法得到它的工作。任何意见将不胜感激!

Outlook.MailItem emailToSend; 
... 
Microsoft.Office.Core.CommandBarButton cbb = null; 
cbb =(CommandBarButton)emailToSend.GetInspector.CommandBars["Standard"].FindControl(Type.Missing, 718, Type.Missing, true, false); 

if (cbb != null) { 
    //it is not null in debugger  
    if (cbb.Enabled) { 
    //make sure digital signature is on 
    cbb.Visible = true; 
    Debug.WriteLine("State was: " + cbb.State.ToString()); //all debug calls return msoButtonUp 
    cbb.SetFocus(); 
    SendKeys.SendWait("{ENTER}"); 
    Debug.WriteLine("State was: " + cbb.State.ToString()); 
    SendKeys.SendWait("~"); 
    Debug.WriteLine("State was: " + cbb.State.ToString()); 
    cbb.Execute(); 
    Debug.WriteLine("State was: " + cbb.State.ToString()); 
    } 
}    
+0

一些额外的信息:当我尝试cbb.State = MsoButtonState.msoButtonDown;我用HRESULT E_FAIL得到了一个运行时COM异常。 –

通过反复试验找出它。主要的问题似乎是我在显示MailItem之前使用了Inspector。在开头添加对显示的呼叫解决了它。任何有兴趣,这里是为我工作的代码:

private static void addOutlookEncryption(ref Outlook.MailItem mItem) { 
     CommandBarButton encryptBtn; 
     mItem.Display(false); 
     encryptBtn = mItem.GetInspector.CommandBars.FindControl(MsoControlType.msoControlButton, 718, Type.Missing, Type.Missing) as CommandBarButton; 
     if (encryptBtn == null) { 
      //if it's null, then add the encryption button 
      encryptBtn = (CommandBarButton)mItem.GetInspector.CommandBars["Standard"].Controls.Add(Type.Missing, 718, Type.Missing, Type.Missing, true); 
     } 
     if (encryptBtn.Enabled) { 
      if (encryptBtn.State == MsoButtonState.msoButtonUp) { 
       encryptBtn.Execute(); 
      } 
     } 
     mItem.Close(Outlook.OlInspectorClose.olDiscard); 
    } 

实际上,有一个更好的方法以编程方式加密,签名,加密+号,或确保两者都不是。你可以做到这一点,而不必显示邮件项目。下面的文章说明了如何使用邮件项目的性质:

http://support.microsoft.com/kb/2636465?wa=wsignin1.0

例如,在C#中,如果MITEM是您的邮件项,然后将下面的代码将关闭签名和加密:

mItem.PropertyAccessor.SetProperty("http://schemas.microsoft.com/mapi/proptag/0x6E010003", 0);