Programmatically encrypt Outlook email using Inspe

2019-02-26 04:15发布

问题:

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.

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

回答1:

There's actually a better way to programmatically encrypt, sign, encrypt + sign, or ensure neither. And you can do it without having to display the mail item. The following article shows how, using a property of the mail item:

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

For example, in C#, if mItem is your mail item, then the following code would turn off signing and encryption:

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


回答2:

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