【问题标题】:Application_ItemSend not firing on replies/forwardingApplication_ItemSend 未在回复/转发时触发
【发布时间】:2021-06-12 23:42:57
【问题描述】:

在单独的窗口中撰写新电子邮件时,我的 Outlook 加载项无法正常工作。但是,当您在主窗口中回复或转发它时,我的用户提示不会出现。似乎在某些时候代码没有捕捉到 Outlook 吐出的事件。

如果有人有任何想法,我将不胜感激!

using System;
using System.Collections.Generic;
using Outlook = Microsoft.Office.Interop.Outlook;
using System.Windows.Forms;




namespace AutoMarkings
{
    public partial class ThisAddIn
    {
         private void ThisAddIn_Startup(object sender, System.EventArgs e)
        {
            this.Application.ItemSend += new Outlook.ApplicationEvents_11_ItemSendEventHandler(Application_ItemSend) 
        }

        void Application_ItemSend(object Item, ref bool Cancel)
        {
            Outlook.Inspector inspector = Application.ActiveInspector();

            if (inspector != null)
            {
                object item = inspector.CurrentItem;

                        Outlook.MailItem mail = Item as Outlook.MailItem;
                        string PR_SECURITY_FLAGS = @"http://schemas.microsoft.com/mapi/proptag/0x6E010003";
                        int sec = mail.PropertyAccessor.GetProperty(PR_SECURITY_FLAGS);

                    if (item is Outlook.MailItem)
                    {
                        DialogResult result = MessageBox.Show("SAMPLE MESSAGE", MessageBoxButtons.YesNoCancel);

                        if (result == DialogResult.Cancel)
                        {
                            Cancel = true;
                        }

                        else if (result == DialogResult.Yes)
                        {
                            var recips = mail.Recipients;
                            bool external = false;

                            for (int i = recips.Count; i >= 1; i--)
                            {
                                Outlook.Recipient recip = (Outlook.Recipient)recips[i];
                                Outlook.PropertyAccessor pa = recip.PropertyAccessor;
                                const string PR_SMTP_ADDRESS =
                                            "http://schemas.microsoft.com/mapi/proptag/0x39FE001E";

                                string smtpAddress =
                                    pa.GetProperty(PR_SMTP_ADDRESS).ToString();


                                if (!smtpAddress.Contains("@COMPANYDOMAIN"))
                                {
                                    external = true;
                                    break;
                                }
                            }

                            if (sec > 31)
                            {
                                sec = sec % 32;
                            }


                            if (external == true)
                            {
                                if ((sec == 0) || (sec == 2))
                                {
                                    MessageBox.Show("This email is being sent to an external address. Please encrypt the message and send again.");
                                    Cancel = true;
                                }
                            }

                            if (Cancel == false && mail != null)
                            {
                                var attachments = mail.Attachments;
                                string addToBody = "SAMPLE BODY";
                                List<string> list = new List<string>();

                                if (!mail.HTMLBody.Contains(addToBody)) mail.HTMLBody = (addToBody + "<br/>" + "<br/>" + mail.HTMLBody);

                                }
                            }
                        }
                    
                
            }
        }

       
        private void ThisAddIn_Shutdown(object sender, System.EventArgs e) { }

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

【问题讨论】:

    标签: c# outlook outlook-addin


    【解决方案1】:

    当然 - 您的代码正在检查 Application.ActiveInspector != null。如果您使用内联回复发送,则不会有检查员。

    而且您不需要检查器 - 您只使用它来检索 Inspector.CurrentItem - 确实不需要:正在发送的项目作为参数传递给您的事件 jhandler。

    【讨论】:

      【解决方案2】:

      正在发送的项目作为参数传递:

      void Application_ItemSend(object Item, ref bool Cancel)
      {
           object item = Item;
           //  your code goes there
      }
      

      更多信息请参见Application.ItemSend。

      注意,您可以使用Explorer.ActiveInlineResponse 属性,该属性返回一个项目对象,该对象表示资源管理器阅读窗格中的活动内联响应项,但不能在ItemSend 事件中使用。这里使用作为参数传递的项目。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-08-03
        • 1970-01-01
        • 1970-01-01
        • 2013-11-15
        • 1970-01-01
        相关资源
        最近更新 更多