添加附件事件 Outlook 加载项

Add attachements event Outlook AddIn

我尝试在 mailItem 的文件附件之前获取 outlook 插件中的附件文件。

    private void Inspectors_NewInspector(Outlook.Inspector Inspector)
        {

            if (Inspector.CurrentItem is Outlook.MailItem)
            {

    Outlook.MailItem mail = (Outlook.MailItem)Inspector.CurrentItem;
                    Inspector.AttachmentSelectionChange += Inspector_AttachmentSelectionChange;
                    Application.AttachmentContextMenuDisplay += Application_AttachmentContextMenuDisplay;
                    mail.BeforeAttachmentAdd += Mail_BeforeAttachmentAdd;
                    mail.AttachmentAdd += Mail_AttachmentAdd;
                    mail.BeforeAttachmentWriteToTempFile += Mail_BeforeAttachmentWriteToTempFile;
                    mail.BeforeAttachmentSave += Mail_BeforeAttachmentSave;
}}

当我在 outlook 中创建一个新的电子邮件时,我的代码通过了这种方法,但是当我向我的电子邮件添加附件时,该事件从未被触发。

有什么想法吗?

您需要在 class 级别(全局范围)声明源对象,以防止它被垃圾收集器擦除,例如:

    Outlook.MailItem mail = null;
    Outlook.Inspector inspector = null;

    private void Inspectors_NewInspector(Outlook.Inspector Inspector)
    {
        inspector = Inspector;
        object oMail = inspector.CurrentItem;
        if (oMail is Outlook.MailItem)
        {

                mail = (Outlook.MailItem)oMail.CurrentItem;               
                inspector.AttachmentSelectionChange += Inspector_AttachmentSelectionChange;
                Application.AttachmentContextMenuDisplay += Application_AttachmentContextMenuDisplay;
                mail.BeforeAttachmentAdd += Mail_BeforeAttachmentAdd;
                mail.AttachmentAdd += Mail_AttachmentAdd;
                mail.BeforeAttachmentWriteToTempFile += Mail_BeforeAttachmentWriteToTempFile;
                mail.BeforeAttachmentSave += Mail_BeforeAttachmentSave;
        }
    }