如何创建一个 Outlook 加载项,在用户尝试回复域外的发件人时提醒用户?

How do I create an Outlook add-in that alerts the user when they attempt to reply to senders that are outside of their domain?

我是 c# 编码的新手。我目前正在尝试创建一个 Outlook 加载项,以在用户尝试回复不是来自“@abc.com”的任何人时提示和提醒用户。例如,如果 'ben@abc.com' 试图回复 'jack@def.com',则会提示 window 提醒 Ben 警告他“您将要回复的人不是来自 '@abc.com'。选项 'ok' 和 'cancel'.

我在网上参考了下面的代码,但是这个加载项只允许我编辑我要发送的电子邮件的字段值。我什至无法弄清楚如何解决和实施代码来处理回复。我曾尝试在线研究并看到过类似 .reply() 的方法,但我对如何应用它们感到困惑。

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

首先,您只跟踪 Inspectors.NewInspector 事件。但在大多数情况下,回复将是内联的——你还需要 Explorer.InlineResponse 事件(其中 Explorer 来自 Application.ActiveExplorer,它在启动时可以为 null,所以你还需要 Application.Explorers.NewExplorer 事件)。

其次,您需要遍历 mailItem.Recipients 集合中的所有收件人,并针对每个 Recipient,检查 Recipient.Address 属性(您可以测试比赛)。

收听 send event,因为无论 inline 回复或 inspector 级别回复都会触发。

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

然后可以按如下方式实现,

/// 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.
}

您可以在此处的 MS 博客中阅读此活动 Item_Send