Outlook ADXMAPIStoreAccessor OnNewMail

Add-in Express™ Support Service
That's what is more important than anything else

Outlook ADXMAPIStoreAccessor OnNewMail
When is it safe to .Move a MailItem? 
Tony Gravagno


Guest


I'm using the ADXMAPIStoreAccessor (.NET4/Outlook 2010) to intercept new inbound e-mail. The intent is to sort the mail items into different folders based on criteria like From Name or Address, To, CC, Subject, and various headers. I already have this functionality in VBA with Redemption and want to migrate it into an AddIn.

When the OnNewMail event is fired, I may want to move it to another folder.
Is the mail item already in the Inbox when this event is executed?
I'm guessing ADXMAPINewMailNotificationEventArgs.StoreId should be Inbox.

Is Outlook._MailItem.Move() valid within this handler?
I'm not sure if ADXMAPIStoreAccessor requires that an operation like this be performed in some special way in order to ensure the object is modified - and I don't want to move the object out from under some other handler.

Should I wait for some other event to query and move a folder item?

Since we're here, I've been through the blogs discussing the NewMail, NewMailEx, and logging inbound mail to make sure it's not missed by some process. Does ADXMAPIStoreAccessor_OnNewMail eliminate the need for other methods to capture new mail? The information available isn't clear on this. Just how effective is OnNewMail and when should we attempt to overcome its inadequacies?

Thanks!
Posted 16 Apr, 2012 19:36:47 Top
Eugene Astafiev


Guest


Hi Tony,

First of all, please take a look at the http://www.add-in-express.com/creating-addins-blog/2008/04/25/newmail-itemadd-outlook-events/ article on our technical blog. It describes the OnNewMail event of the MAPIStoreAccessor component in depth.

Please be aware that Extended MAPI provides http://msdn.microsoft.com/en-us/library/cc842079.aspx. The New Mail notification has the following description:

A message has been delivered and is waiting to be processed.


It means that some Outlook rules may be applied to the just received mail item (after the event handler finishes its job). So, you need to move the item in the code of the OnNewMail event handler. Do you get any exceptions or error messages?

FYI The http://msdn.microsoft.com/en-us/library/cc765897.aspx structure is passed to the OnNewMail event event notification handler by the MAPI subsystem. I.e. you need to use the ParentId instead (the StoreId should be empty).
Posted 17 Apr, 2012 03:15:37 Top
Tony Gravagno


Guest


Eugene, I just finished testing with this code. Of course you are 100% correct. :) After running this through debug it occurs to me that it doesn't matter if new e-mail has been stored in the Inbox yet. Moving the mail item is done with item.Move(toFolderObj). It's irrelevant where the object is because we're not moving it from anywhere. We're just operating on the object and telling it where it will be.

I have read your "Handling NewMail.." article several times (and pretty much every other blog available on this site). I guess material like that becomes more clear after we write code to implement what we've learned. This is what I understand from that article:

Use the StoreAccessor to process new items, and feel confident that with a local PST file it "should" get everything. However, for Exchange, some backup code is required which may find unprocessed emails in the Inbox and process them. Those unprocessed items are the effect of the sync issue mentioned in the article.


The following article documents a process which I believe would serve as a good backup as indicated above:
http://www.add-in-express.com/creating-addins-blog/2011/11/10/outlook-newmail-custom-solution/

Rather than using a timer and a local datastore to automatically detect unprocessed items in the Inbox, I'm going to provide a button which allows the user to manually process whatever items they select in any file.

In summary, the StoreAccessor has been a great help. So far it has allowed me to replace VBA code which used CDO and Redemption. Thanks!
Posted 18 Apr, 2012 06:24:45 Top
Eugene Astafiev


Guest


You are always welcome, Tony! :-)
Posted 19 Apr, 2012 01:36:04 Top