none
NewMailEx event not firing on receipt of simultaneous emails RRS feed

  • Question

  • Hello all,

    I have written a procedure in Outlook VBA using the NewMailEx event which should fire when a new mail item is received in the inbox. It works well when it works, but I have noticed that if I receive several emails simultaneously, only one of them is caught by the NewMailEx event, the others just bypass the macro.

    I have several accounts in Outlook and a good example of when the macro fails to fire is if I am sent an email with more than one of my email addresses in the "To" line. This means that I might receive three or four emails simultaneously and only one of them is caught by the NewMailEx event.

    I am using Outlook 2010 32-bit on Windows 7 64-bit if that makes any difference.

    Thanks.

    Wednesday, October 23, 2013 3:52 PM

Answers

  • As a general rule of thumb, you want the event handlers to return as soon as possible.

    Since Outlook objects cannot be used in separate threads, one solution is to save the event arguments (new mail entry ids) in a list and start a timer. When the timer event fires (you will be out of the event handler by then), you can retrieve the entry ids from the list and run any necessary processing.


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.5 is now available!

    Thursday, October 24, 2013 2:47 PM

All replies

  • Are you sure your NewMailEx handler is not receiving multiple entry ids separated by ";"?

    Also keep in mind that MAPI events can be dropped under heavy loads. What does your event handler do? Would it work if you were to comment out all of the code in it?


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.5 is now available!

    Wednesday, October 23, 2013 4:43 PM
  • Hello Dmitry Streblechenko

    Thanks for your suggestion - it didn't resolve my issue, but it did make me realise something else.

    The procedure is designed to catch emails which come in with zip file attachments (an anti-virus device) and the way it works is if an email is found with a zip file attachment, a message box pops up with the message details and asks if you want to delete the email. The procedure is a bit rough and ready but it works for me.

    The thing that I hadn't noticed before was that the procedure only fails to run when two or more emails with zip file attachments come in simultaneously. In other words, if two or more emails come through without a zip file, the procedure runs OK for all of them until it meets one which has got a zip file. Then it displays the message, and it's at the point the message is displayed that the procedure fails to run for any further emails received.

    Your suggestion has given me a few ideas on how to proceed, but if you've got any other suggestions please let me know!

    Thanks for your help.

    Thursday, October 24, 2013 10:24 AM
  • As a general rule of thumb, you want the event handlers to return as soon as possible.

    Since Outlook objects cannot be used in separate threads, one solution is to save the event arguments (new mail entry ids) in a list and start a timer. When the timer event fires (you will be out of the event handler by then), you can retrieve the entry ids from the list and run any necessary processing.


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.5 is now available!

    Thursday, October 24, 2013 2:47 PM
  • I'd also set a flag when in the event handler and clear it when exiting the handler. I'd check the flag when it came time to process the id's so if in the handler I'd wait to process the id's until not in the handler.

    Ken Slovak MVP - Outlook

    Thursday, October 24, 2013 2:56 PM
    Moderator
  • That's a good idea. But if your event handler does not show any dialogs, the timer will not fire: it only fires when the application (Outlook) is idle and sits waiting for user input.

    Also, you might want to use the Timer class from the System.Windows.Forms namespace rather that System.Threading: the latter fires on a secondary thread, which can and will cause problems.


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.5 is now available!

    Friday, October 25, 2013 10:21 PM