2012-05-03 109 views
2

我使用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異常。 –

回答

1

通過反覆試驗找出它。主要的問題似乎是我在顯示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); 
    } 
2

實際上,有一個更好的方法以編程方式加密,簽名,加密+號,或確保兩者都不是。你可以做到這一點,而不必顯示郵件項目。下面的文章說明了如何使用郵件項目的性質:

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

例如,在C#中,如果MITEM是您的郵件項,然後將下面的代碼將關閉簽名和加密:

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