0
votes

In my OLK 2016 addin i have defined

private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
   this.Application.NewMail += new Microsoft.Office.Interop.Outlook.ApplicationEvents_11_NewMailEventHandler(ThisApplication_NewMail);
}

In ThisApplication_NewMail, i have some code that does some categorization with that mail when it arrives, depending on sender, and such things. This works just fine. However, I also have defined in OLK some "move email" type rules (the mail is moved in folder nested in the Inbox, not outside the mailbox), which, on occasion, do apply to the mails that should be categorized by my addin. Breakpointing on the method's entry does not happen, which indicates the manually defined rules process the mail before my code (for lack of a better informed technical explanation).

So I am looking for a way in which I can either write the code differently in order to process with my code before the manual defined rules, or define a priority between the two, or something in those lines.

One way i could think on working around it is to simply enumerate all the folders in the inbox and run my IFs there, however, this might end up with a performance penalty since there are a lot of unread mails to be processed.

Anyone has a better idea on how to achieve my scenario? Pointers are sufficient, not asking for the code necessarily.

2

2 Answers

0
votes

All events are asynchronous, plus server side rules are almost guaranteed to run before your code does.

Try to set up Items.ItemAdd event handlers on the Inbox folder, Junk Email, and all folders that the rules point to.

0
votes

First of all, the NewMail event doesn't give you a context which email is received. It is fired when one or more new email messages are received in the Inbox. If you want to process items that arrive in the Inbox, consider using the ItemAdd event on the collection of items in the Inbox. The ItemAdd event passes a reference to each item that is added to a folder.

Also, I'd suggest trying the NewMailEx event which is fired for once for every received item that is processed by Microsoft Outlook. The NewMailEx event fires when a new message arrives in the Inbox and before client rule processing occurs. You can use the Entry ID returned in the EntryIDCollection array to call the NameSpace.GetItemFromID method and process the item. Use this method with caution to minimize the impact on Outlook performance. However, depending on the setup on the client computer, after a new message arrives in the Inbox, processes like spam filtering and client rules that move the new message from the Inbox to another folder can occur asynchronously. You should not assume that after these events fire, you will always get a one-item increase in the number of items in the Inbox.