Home > Software design >  How do I create an Outlook add-in that alerts the user when they attempt to reply to senders that ar
How do I create an Outlook add-in that alerts the user when they attempt to reply to senders that ar

Time:03-31

I am new to coding in c#. I am currently trying to create an Outlook add-in to prompt and alert users if they are to attempt to reply to anyone that is not from "@abc.com". For example if '[email protected]' is to trying to reply to '[email protected]', a window to alert Ben will be prompted warning him "You are about to reply to someone that is not from '@abc.com'." with the options of 'ok' and 'cancel'.

I referred online for the code below but this add-in only allows me to edit the field values of the email I am trying to send. I am unable to even figure out how to address and implement the code to deal with replying. I have tried researching online and have seen methods like .reply() but I am confused as to how to apply them.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using Outlook = Microsoft.Office.Interop.Outlook;
using Office = Microsoft.Office.Core;

namespace FirstOutlookAddIn
{

    public partial class ThisAddIn
    {

        Outlook.Inspectors inspectors;
        private void ThisAddIn_Startup(object sender, System.EventArgs e)
        {
            inspectors = this.Application.Inspectors;
            inspectors.NewInspector  =
            new Microsoft.Office.Interop.Outlook.InspectorsEvents_NewInspectorEventHandler(Inspectors_NewInspector);
        }


        void Inspectors_NewInspector(Microsoft.Office.Interop.Outlook.Inspector Inspector)
        {
            Outlook.MailItem mailItem = Inspector.CurrentItem as Outlook.MailItem;
            if (mailItem != null)
            {
                if (mailItem.EntryID == null)
                {
                    mailItem.To = "Testing for Recipient.";
                    mailItem.Subject = "Currently testing add-in for Subject.";
                    mailItem.Body = "Currently testing add-in for Body.";
                    mailItem.CC = "Testing for CC.";
                }

            }

        }

            private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
        {
            // Note: Outlook no longer raises this event. If you have code that 
            //    must run when Outlook shuts down, see https://go.microsoft.com/fwlink/?LinkId=506785
        }

        #region VSTO generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InternalStartup()
        {
            this.Startup  = new System.EventHandler(ThisAddIn_Startup);
            this.Shutdown  = new System.EventHandler(ThisAddIn_Shutdown);
        }
    
        #endregion
    }
}

CodePudding user response:

Firstly, you are only tracking Inspectors.NewInspector event. But in most cases, replies will be inline - you also need Explorer.InlineResponse event (where Explorer comes from Application.ActiveExplorer, which can be null on startup, so you'd also need Application.Explorers.NewExplorer event).

Secondly, you will need to loop through all recipients in the mailItem.Recipients collection, and for each Recipient, check the Recipient.Address property (which you can test for the match).

CodePudding user response:

Listen to send event, as it will be triggered irrespective of inline replies or inspector level replies.

this.Application.ItemSend  = new Outlook.ApplicationEvents_11_ItemSendEventHandler(Application_ItemSend);

then implementation of this can be done as below,

/// param : item -> mail item to be sent
/// param : cancel -> allows you to cancel sending mail in outlook. By default value is false. 
private void Application_ItemSend(object item, ref bool cancel){
   Outlook.MailItem mail = (Outlook.MailItem)item;
   // read "mail.To" and check for your logic; 
   // if To list has other domains, then show the warning prompt maybe like below,
   var result = MessageBox.Show("yourmessage","title",MessageBoxButtons.YesNo);
   if(result == DialogResult.No)
     cancel = true; // setting this to `true` will stop sending an email out.
}

you can read on this event in MS blog here Item_Send

  • Related