【问题标题】:Programmatically encrypt Outlook email using Inspector使用 Inspector 以编程方式加密 Outlook 电子邮件
【发布时间】:2014-12-19 01:51:19
【问题描述】:

我将 C# 与 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());
  }
}              

【问题讨论】:

  • 一些附加信息:当我尝试 cbb.State = MsoButtonState.msoButtonDown;我得到一个带有 HRESULT E_FAIL 的运行时 COM 异常。

标签: c# outlook office-interop outlook-addin office-automation


【解决方案1】:

实际上有更好的方法来以编程方式加密、签名、加密 + 签名,或者两者都不确保。而且您可以做到这一点,而不必显示邮件项目。以下文章展示了如何使用邮件项的属性:

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

例如,在 C# 中,如果 mItem 是您的邮件项目,那么以下代码将关闭签名和加密:

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

【讨论】:

    【解决方案2】:

    通过反复试验想通了。主要问题似乎是我在显示 MailItem 之前使用了 Inspector。在开始时添加对 Display 的调用解决了它。对于任何有兴趣的人,这里是对我有用的代码:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-06-15
      • 1970-01-01
      • 2018-05-08
      • 2012-05-30
      • 1970-01-01
      • 2013-05-01
      • 1970-01-01
      相关资源
      最近更新 更多