Pieter van der Westhuizen

Outlook NewMail event: solution options

In part one of the Outlook NewMail unleashed series we took a closer look at the challenge of effectively handling a new e-mail in Outlook. We’ve seen that the obvious choice of NewMail, NewMailEx and Items.ItemAdd is simply not the answer. In this post we’ll investigate possible work-arounds and alternatives to NewMail, NewMailEx and Items.ItemAdd.

Extended MAPI

The first option in our line-up is to use Extended MAPI. As terrifying as extended MAPI might sound, you have no need to worry. Add-in Express provides a free .Net component to make it easier to access the extended MAPI features and subscribe to MAPI notifications, including when a new e-mail arrives. It also does not have the same limitations as the standard Outlook NewMailEx and NewMail events.

However, extended MAPI does have its limitations. In some cases the notification took a long time to be raised and in some cases it was not raised at all. The MAPI Store Accessor works in Outlook 2000 to Outlook 2010, unfortunately it does not support the 64-bit version of Outlook and Add-in Express is also not planning on introducing a 64-bit version.

So, in certain scenarios, extended MAPI might work for you. Third-party component such as Redemption are available to make accessing Extended MAPI a little easier. However, the limitations of delayed messages or messages not delivered will still remain. Of course, if you want more control and something more reliable why not consider creating your own solution?

Writing your own solution to check for new Outlook items

Another and probably the most reliable option are to write your own timer based solution that will periodically scan your Outlook folders and check for new items. The scanning algorithm can be started from the SyncEnd event, which according to MSDN: “Occurs immediately after Microsoft Outlook finishes synchronizing a user’s folders using the specified Send\Receive group.”

However, this event does not occur when Outlook is starting up and there may be instances where an e-mail land in the inbox after the synchronization has finished. In order to work around this, you could have a time-based method that scans your Outlook folders on a pre-set interval. This method can use the Restrict method of the Outlook Items object to filter e-mails that have been received after the last e-mail that was processed ReceivedDate/Time. Be careful though, it is imperative that your servers’ time is correct, a problem Renat and I can tell you is something than can bite you. : )

Also, if your users will be using different timezones, you might want to consider using a DASL query with the restrict method; DASL always performs date/time comparisons in UTC. The book by Randy Byrne and Ryan Gregg, Programming Applications for Microsoft Office Outlook 2007 has an entire chapter dedicated to searching Outlook data, with or without using DASL, and I highly recommend it. You can read a sample chapter on MSDN.

Ideally, you would want to save the EntryId, ReceivedTime and information such as Subject and the Sender’s email address to an external data source, which could be anything from a flat text file, xml file or database. You might be asking yourself why I need to store all that information. Wouldn’t the EntryId suffice? Well, unfortunately no. There might be a situation where the EntryId of the e-mail might change which could cause you to process the same e-mail more than once. For example, if the new e-mail is moved to another folder by an Outlook rule or the user. It can also happen that the NewMailEx event's EntryId parameter is a certain value, but changes if the email is moved to another folder.

The solution to the NewMail problem is certainly not an easy one, but if there is one team that can  help you solve it, it is without a doubt the guys from Add-in Express. In the next few weeks we’ll dig into some code and walk through the two solutions mentioned above.

Thank you for reading. Until next time, keep coding!

Outlook NewMail unleashed:

6 Comments

  • https://secure.gravatar.com/avatar/22af2666108e3373a6c0990d67efa9cd?s=32&d=https%3A%2F%2Fsecure.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D32&r=G VB Learner says:

    Hi All,

    I am new to VB and am trying something on the similar lines.

    What I attempt to do –
    —————————

    Whenever I open outlook, it should read all the unavailable inbox emails and fetch subject names of each of them. As already mentioned in multiple forums, the code to fetch the subject when placed inside Application Startup event runs ahead of the inbox sync up/refresh owing to which the new items still stay as new and are not processed by the application startup event.

    What I tried:
    —————

    1) Tried using the Syncobjects method in VB code. Here also the SyncStart event fires and the SyncEnd event fires(The later was initially not firing) in outlook 2010. Looks like it doesn’t work with Outlook 2013. I am looking for a code to run in Outlook 2013. Is there an alternative to SyncEnd event that marks the completion of Send/Receive (In 2010) in Outlook 2013.

    Have been trying multiple methods,but ended up with nothing concrete.Please let me know if there is a way to monitor the Syncstart operation for its completeness before executing the code for mail processing in outlook 2013 or any alternative to it if Sync end doesn’t fire in 2013.

    It would be of help if someone posts the complete code. Have already tried bits and pieces of the ones listed above and it didnt seem to help :( :( :(

    Thanks in Advance !!!!

  • Hello,

    You need to check the code posted at https://www.add-in-express.com/creating-addins-blog/2011/11/10/outlook-newmail-custom-solution/. To translate to VB.NET, you need to use any free C# to VB.NET converter available online, see e.g. http://www.developerfusion.com/tools/convert/csharp-to-vb/.

    > Please let me know if there is a way to monitor the Syncstart operation for its completeness before executing the code for mail processing in outlook 2013 or any alternative to it if Sync end doesn’t fire in 2013.

    You need to wait for the SyncEnd event. Note that by default, Add-in Express doesn’t connect to Sync* events; to change this behavior, see the HandleEvents property of the Outlook Events component (ADXOutlookAppEvents).

  • https://secure.gravatar.com/avatar/22af2666108e3373a6c0990d67efa9cd?s=32&d=https%3A%2F%2Fsecure.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D32&r=G VB Learner says:

    Thank you. Tried probing into the above but haven’t yet found the way out. Could you please elaborate more on the same. Should I have to Type some code to enable SyncEnd Event or something

  • https://secure.gravatar.com/avatar/ab4ec2858cfdf1e44dadf8c50fae314d?s=32&d=https%3A%2F%2Fsecure.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D32&r=G Dmitry Kostochko (Add-in Express Team) says:

    Hello,

    >> Should I have to Type some code to enable SyncEnd Event or something

    You need to enable sync events. Please set the HandleEvents property of the ADXOutlookAppEvents component to SyncObjects.

  • https://secure.gravatar.com/avatar/ad516503a11cd5ca435acc9bb6523536?s=32 VBLearner says:

    Hi,

    This is the code that works in Outlook 2010. It displays the message “End of Sync is complete” after Send/Receive is complete.

    This is already available as in many links.

    Dim WithEvents mysync As Outlook.SyncObject
    Private Sub Application_Startup()
    MsgBox “Hi”
    Set mysync = Application.Session.SyncObjects.Item(1)
    mysync.Start
    End Sub
    Private Sub mySync_SyncEnd()
    MsgBox “End of Sync is complete”
    End Sub

    //You need to enable sync events. Please set the HandleEvents property of the ADXOutlookAppEvents component to SyncObjects

    Where to add it and how to add it in above code ?

  • https://secure.gravatar.com/avatar/ab4ec2858cfdf1e44dadf8c50fae314d?s=32&d=https%3A%2F%2Fsecure.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D32&r=G Dmitry Kostochko (Add-in Express Team) says:

    Hi,

    >> Where to add it and how to add it in above code ?

    Sorry, I thought you were using Add-in Express and that is why suggested modifying the properties of our component. In pure VBA code this approach will not work.

    I suppose you have run into an MS Outlook 2013 bug, please read the following thread, Microsoft guys suggested to install a hotfix there:
    https://social.msdn.microsoft.com/Forums/office/en-US/7a20664c-7650-4d61-9d5f-13f1d929a8cd/outlook-2013-automatic-sendreceive-doesnt-work-with-the-sample-addin-below

    Hope this helps.

Post a comment

Have any questions? Ask us right now!