如何创建 Outlook 加载项,以便在用户尝试回复域外发件人时向用户发出警报?

发布于 2025-01-18 07:41:15 字数 2496 浏览 1 评论 0原文

我是 C# 编码新手。我目前正在尝试创建一个 Outlook 加载项,以便在用户尝试回复非“@abc.com”的任何人时提示和提醒用户。例如,如果 '[email protected]' 是尝试回复 ' [电子邮件受保护]',系统会弹出一个警告 Ben 的窗口,警告他“您即将回复非来自 '@abc.com' 的人。”有“确定”和“取消”选项。

我在网上引用了下面的代码,但此加载项仅允许我编辑我尝试发送的电子邮件的字段值。我什至无法弄清楚如何处理和实现处理回复的代码。我尝试过在线研究并看到过像 .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
    }
}

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

古镇旧梦 2025-01-25 07:41:15

首先,您只是跟踪Inspectors.newinspector事件。但是在大多数情况下,答复将是内联的 - 您还需要explorer.inlineresponse event(其中explorer来自application.active> application.activeeviveexplorer,它可以null null null在启动时,您还需要application.explorers.newexplorer event)。

其次,您需要在mailItem.recipients集合中循环循环,对于每个收件人,请检查coce> contectient.address.address property(您可以测试比赛)。

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).

简单 2025-01-25 07:41:15

监听 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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文