none
Set event handler for MailItem.Send in Outlook Addin RRS feed

  • Question

  • I'm trying to react to when a specific mailItem gets sent. I'm aware of the existence of the "Application.ItemSend" event but my understanding is that this will be triggered when ANY mailItem gets sent. I need to know when a specific mailItem is being sent. Currently, I have a wrapper class for each Inspector window that gets created, and each wrapper is associated with an item. 

    Here's a snippet of what I'm trying to do:

    var mail = _inspector.CurrentItem as MailItem;
    mail.Send += MySendEventHandler;

    This fails to compile with the message: "Cannot assign to 'Send' because it is a 'method group'". I read in a separate thread that I can flag the mailItem using the CustomProperties property, but this would be sub-optimal for my purposes, and would only use this as a last resort.


    Tuesday, October 31, 2017 1:30 AM

Answers

  • Hello Jeremy,

    There are two kind of Send - the event and method. To distinguish them you need to cast the MailItem object to the ItemEvents_10_Event interface:

    ((Outlook.ItemEvents_10_Event)mailItem).Send += new Microsoft.Office.Interop.Outlook.ItemEvents_10_SendEventHandler(MySendEventHandler);


    profile for Eugene Astafiev at Stack Overflow, Q&A for professional and enthusiast programmers

    Tuesday, October 31, 2017 11:04 AM

All replies

  • Hello Jeremy,

    There are two kind of Send - the event and method. To distinguish them you need to cast the MailItem object to the ItemEvents_10_Event interface:

    ((Outlook.ItemEvents_10_Event)mailItem).Send += new Microsoft.Office.Interop.Outlook.ItemEvents_10_SendEventHandler(MySendEventHandler);


    profile for Eugene Astafiev at Stack Overflow, Q&A for professional and enthusiast programmers

    Tuesday, October 31, 2017 11:04 AM
  • Hi Eugene, thank you for the solution above.

    On a somewhat related note, I'm having an issue with the mailItem.PropertyChange event. It appears as though when an email gets sent, this event gets triggered multiple times per property, which does not seem correct. So I am wondering if I'm setting up the even incorrectly.

    _mailItem.PropertyChange += MailItemPropertyChanged;

    When the email gets sent, this event gets triggered multiple times for each property. More specifically, it gets triggered for the "To" property three times, and three more times each for the "CC" and "BCC" property. I can post this in a separate thread if you'd like.

    Tuesday, October 31, 2017 11:49 PM