MS 交换服务器捕获的 VSTO 加载项 BeforeAttachmentAddEvent

VSTO add-in BeforeAttachmentAddEvent catched by MS exchange server

我正在写 Outlook VSTO add-in,我想在添加附件之前进行大小检查,如果文件太大,我想将它上传到云端,所以代码如下:

    public partial class ThisAddIn
    {
        private void ThisAddIn_Startup(object sender, EventArgs e)
        {
            Application.Inspectors.NewInspector += InspectorsOnNewInspector;
        }

        private void InspectorsOnNewInspector(Inspector inspector)
        {
            if (inspector.CurrentItem is MailItem mailItem)
            {
                mailItem.BeforeAttachmentAdd += MailItemOnBeforeAttachmentAdd;
            }
        }

        private void MailItemOnBeforeAttachmentAdd(Attachment attachment, ref bool cancel)
        {
            // check and upload
            cancel = true;
        }

        private void ThisAddIn_Shutdown(object sender, 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>
        [SuppressMessage("ReSharper", "ArrangeThisQualifier")]
        [SuppressMessage("ReSharper", "RedundantDelegateCreation")]
        [SuppressMessage("ReSharper", "RedundantNameQualifier")]
        private void InternalStartup()
        {
            this.Startup += new System.EventHandler(ThisAddIn_Startup);
            this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);
        }

        #endregion
    }

问题是在文件超过 MS Exchange 中配置的大小限制之前一切正常。发生这种情况时,我会收到一条通知消息,并且在单击“确定”后 mailItem.BeforeAttachmentAdd 事件不会触发。我该如何处理?

None 的事件处理程序将工作超过几秒钟 - 在 Application.Inspectors.NewInspector 的情况下,您将事件处理程序设置在临时变量(由编译器创建)或局部变量(当您设置 mailItem.BeforeAttachmentAdd 事件处理程序时)。

引发事件的对象必须是活动的 - 将这些对象存储在全局 (class) 级别以确保它们不会被垃圾收集器收集。

此外,没有特定/保证的事件顺序,但我想 Outlook 总是会获得第一选择。最坏的情况是,您可以修补 Outlook window 的 IDropTarget 实现并提供您自己的实现。如果从功能区插入附件,您无能为力...