使用Inspector以编程方式加密Outlook电子邮件 [英] Programmatically encrypt Outlook email using Inspector

查看:174
本文介绍了使用Inspector以编程方式加密Outlook电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将C#与Outlook对象模型一起使用(由于许可的原因,我无法选择使用赎回),并且在发送电子邮件之前,我无法通过编程方式对电子邮件进行加密.

I'm using C# with Outlook Object Model (Redemption is not an option for me due to licensing) and I'm having difficulty programmatically encrypting an email message before sending it.

我可以成功获取对CommandBarButton的引用,该引用应该表示Encrypt按钮(根据在线示例,该ID为718),但是我无法以编程方式将其按下.我尝试同时使用CommandBarButton Execute()方法和SendKeys(不确定sendkeys在此上下文中是否有效).所有debug.writeline语句均显示该按钮处于msoButtonUp状态.

I can successfully get a reference to the CommandBarButton which supposedly represents the Encrypt button (Id 718 according to online examples), but I cannot programmatically depress it. I tried using both the CommandBarButton Execute() method and using SendKeys (not sure if sendkeys is even valid in this context). All debug.writeline statements show that the button is in the msoButtonUp state.

我已经玩了好几天了,似乎无法正常工作.任何建议将不胜感激!

I've been playing with this for days and cannot seem to get it working. Any advice would be much appreciated!

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

推荐答案

通过反复试验弄清楚了它.主要问题似乎是在显示MailItem之前我正在使用Inspector.在开始时将呼叫添加到Display即可解决.对于任何有兴趣的人,以下是对我有用的代码:

Figured it out by trial and error. The main problem seemed to be that I was using the Inspector before I had displayed the MailItem. Adding the call to Display at the beginning solved it. For anyone interested, here is the code that worked for me:

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

这篇关于使用Inspector以编程方式加密Outlook电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆