Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

Outlook does not fire the AttachmentAdd event except for one email when multiple emails are composed at the same time

Ask Question

I am writing an Outlook add-in and I am hooking into the AttachmentAdd event of the mailItem. It works as expected when one email is composed but when more than one emails are being composed at the same time, it seems the AttachmentAdd event fires only for the first email to which the attachment is being added.

I am not sure if it has anything to do with the RCW going out of scope.

I am using Outlook 365 64 bit, if that helps.

Here is the code:

public partial class MyAddIn
    private void OutlookAddIn_Startup(object sender, System.EventArgs e)
            Application.ItemLoad += NewItem_Load;
    private void NewItem_Load(object item)
        Outlook.MailItem newMailItem = null;
        newMailItem = item as Outlook.MailItem;
        if (newMailItem != null)
            newMailItem.AttachmentAdd -= MailItem_AttachmentAdd;
            newMailItem.AttachmentAdd += MailItem_AttachmentAdd;
    // This gets called only for one email.
    private void MailItem_AttachmentAdd(Outlook.Attachment attachment)
       // Do some stuff here.
    private void OutlookAddIn_Shutdown(object sender, System.EventArgs e)
        Application.ItemLoad -= NewItem_Load;
        // Log
    protected override Microsoft.Office.Core.IRibbonExtensibility CreateRibbonExtensibilityObject()
        return new MyRibbon(this);
    #region VSTO generated code
    private void InternalStartup()
        this.Startup += new System.EventHandler(OutlookAddIn_Startup);
        this.Shutdown += new System.EventHandler(OutlookAddIn_Shutdown);
        AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;

You are setting up events the event handler on a local variable (newMailItem), that will be released at some point after NewItem_Load() exits.

The variable must stay alive to raise events. You could move the declaration to the class level, but you can have more than one open item. Try to create a wrapper class that holds MailItem as its member and store that wrapper in a list.

I did think about it, but the problem is that I do not know when to release the mailItem reference. It does work when I keep the mailItem in a list. Also, in this case, I am hooking into the ItemLoad event when I really want to hook into the new mail created event. – Shakti Prakash Singh Mar 19, 2021 at 7:34 BTW is there an event for when reply/replyall/forward is hit and the email loads from within the explorer? – Shakti Prakash Singh Mar 21, 2021 at 7:58 Yes. Explorer.InlineResponse and Explorer.InlineResponseClose events. You can see these events live in OutlookSpy (dimastr.com/outspy) – Dmitry Streblechenko Mar 21, 2021 at 19:05

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.