Share via


How to check that outgoing Outlook Mail Item is with digital signature or encrypted from VSTO Add-in in C#?

How to capture whether user has checked to send Outlook Mail with digital signature or encrypted from VSTO Add-in in C# ?

We can use the following code snippet with VSTO SE Outlook Skeleton project to build sample add-in:

IMPORTANT NOTE: This add-in will only work if we are NOT using Word as default email editor.

/*NOTE: Following programming examples is for illustration only, without warranty either expressed or implied,  
including, but not limited to, the implied warranties of merchantability and/or fitness for a particular purpose.   
This sample code assumes that you are familiar with the programming language being demonstrated and the tools used
to create and debug procedures.*/

    1:  using System;
    2:  using System.Windows.Forms;
    3:  using Microsoft.VisualStudio.Tools.Applications.Runtime;
    4:  using Outlook = Microsoft.Office.Interop.Outlook;
    5:  using Office = Microsoft.Office.Core;
    6:   
    7:  namespace OutlookAddin1
    8:  {
    9:      public partial class ThisApplication
   10:      {
   11:          private void ThisApplication_Startup(object sender, System.EventArgs e)
   12:          {
   13:              this.ItemSend +=new Microsoft.Office.Interop.Outlook.ApplicationEvents_11_ItemSendEventHandler(ThisApplication_ItemSend);   
   14:          }
   15:   
   16:          private void ThisApplication_Shutdown(object sender, System.EventArgs e)
   17:          {
   18:          }
   19:          private Office.CommandBarButton EncButton = null;
   20:          private Office.CommandBarButton DigButton = null;
   21:          private void ThisApplication_ItemSend(object Item, ref bool Cancel)
   22:          {
   23:              Outlook.MailItem mItem = null;
   24:    
   25:              try
   26:              {
   27:                  mItem = Item as Outlook.MailItem;
   28:   
   29:                  EncButton = (Office.CommandBarButton) mItem.GetInspector.CommandBars.FindControl(Office.MsoControlType.msoControlButton , 718, Type.Missing, true);
   30:                  if (EncButton.State ==  Office.MsoButtonState.msoButtonDown )
   31:                  {
   32:                      MessageBox.Show ("Mail will be Encrypted");
   33:                  }
   34:   
   35:                  DigButton = (Office.CommandBarButton)mItem.GetInspector.CommandBars.FindControl(Office.MsoControlType.msoControlButton, 719, Type.Missing, true);
   36:                  if (DigButton.State == Office.MsoButtonState.msoButtonDown)
   37:                  {
   38:                      MessageBox.Show ("Mail will be Digitally signed");
   39:                  }
   40:                  
   41:              }
   42:              catch (Exception ex)
   43:              {
   44:                  System.Windows.Forms.MessageBox.Show(ex.Message);
   45:                  Cancel = true;
   46:              }
   47:   
   48:          }
   49:          #region VSTO generated code
   50:   
   51:          /// <summary>
   52:          /// Required method for Designer support - do not modify
   53:          /// the contents of this method with the code editor.
   54:          /// </summary>
   55:          private void InternalStartup()
   56:          {
   57:              this.Startup += new System.EventHandler(ThisApplication_Startup);
   58:              this.Shutdown += new System.EventHandler(ThisApplication_Shutdown);
   59:          }
   60:   
   61:          #endregion
   62:      }
   63:  }

Please refer to following article related to working with CommandBars in Outlook Solution:

How to Use CommandBars in Outlook Solutions
https://support.microsoft.com/kb/201095

The above article also have a Microsoft Excel Macro to generate a list of Outlook 2000/2003 CommandBar control IDs.

Comments