How to capture the initial messages fetch at startup (Not NewMailEx)?

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

How to capture the initial messages fetch at startup (Not NewMailEx)?
 
developer_cp




Posts: 48
Joined: 2016-10-28
Guys, which event can I listen for the first batch of mails that comes in when Outlook started up?
When I start Outlook if there are left over mails on the server, say from yesterday night to the next day morning, those mails would come in but won't trigger via the NewMailEx event. If there an event I can listen to capture that?

Anything else that comes in after Outlook has successfully started will trigger the NewMailEx which is fine, but i need to hook on those very first batch.
Posted 28 Feb, 2017 11:44:19 Top
nwein




Posts: 510
Joined: 2011-03-28
I think you can find your solution in Pieter's post.
Posted 28 Feb, 2017 13:06:08 Top
developer_cp




Posts: 48
Joined: 2016-10-28
Well that sucks... I did read that article a while ago (and forgot about it), and also that's what we had in the Outlook code before.
However, having something that stays in the background to scan forever is just bad programming...

I was expecting there would be an event that would get fire for the first batch..
I've never have issue with subsequence mails that come in after the startup, NewMailEx always gets fire.

In that case I guess I can just stick with my current implementation and just add a 10 seconds delay at start up to mitigate the issue.

Instead of saving an xml file and continuously scan every folders, I just scan the last MailItem modified by my plugin, grab the timestamp, and then scan MailItems up to that timestamp... this approach don't require saving any external file, and the only drawback was that my query fires a bit too quickly before the first batch of mails arrive so it wouldn't pick it up sometimes, adding a delay can solve my problem, but i just dislike bad programming approach and wanted to stick with the event.


For those who is interested, here is how I did it with my approach.
I spawned this code in a BackgroundWorker and then kill the worker once there is no more MailItem that meets my criteria.
Therefore, no forever scanning required.

This grabs the last MailItem modify by my (or your) plugin

            string query = string.Format("SELECT TOP 1 EntryId, "{0}" FROM Folder WHERE " +
                                          "MessageClass Like '%MyPluginClassTag.%'" +
                                          " ORDER BY "{1}" DESC",
                                          mydsal, mydsal);
            RDOItems rdoItems = inbox.Items;
            ADODB.Recordset items = rdoItems.MAPITable.ExecSQL(query);
            ...
            var mytimestamp = items.Fields[mydsal].Value.ToString();
           // do something with mytimestamp


Then, use whatever you got to try to fetch whatever you need.
My code below in a nutshell, would grab 15 entries at a time and do my stuff, sleep for 10 seconds and grab another batch.
Previously i put the Thread.Sleep at the end of my loop, so at start up when my query ran too fast, it would miss the those first batch that came in...
Shifting the sleep to the beginning of the loop kind of fix the problem...

            while (!done) {
                 Thread.Sleep(10000);

                 string query = string.Format("SELECT TOP 15 EntryID, Subject FROM Folder WHERE " +
                                         "MessageClass Like 'IPM.Note%'" +
                                         "[ReceivedTime] >= '{0}' " + 
                                         "ORDER BY ReceivedTime DESC",
                                         mytimestamp);

                 // do whatever, if satisfy your need, set done to true.
                 // blah blah
                 done = true;
           }




I wish there is an event hook for that initial server sync... oh well.
Posted 28 Feb, 2017 14:14:58 Top
Andrei Smolin


Add-in Express team


Posts: 14106
Joined: 2006-05-11
Hello,

developer_cp writes:
However, having something that stays in the background to scan forever is just bad programming...


... or the last resort if there's no other way to achieve your goal.

Regards from Belarus (GMT+3),

Andrei Smolin
Add-in Express Team Leader
Posted 01 Mar, 2017 04:48:09 Top
developer_cp




Posts: 48
Joined: 2016-10-28
Is this a known issue with ADX library not being able to pick up the initial batch of mail that comes in and/or occasionally "miss" the incoming mails as described in the article? I mean if there is missing mails only when there are a lots of mails that come in, it sounds like a bug to me no?
Posted 01 Mar, 2017 10:23:49 Top
Andrei Smolin


Add-in Express team


Posts: 14106
Joined: 2006-05-11
Hello Eric,

The NewMail(Ex) event isn't generated for the emails that Exchange receives while Outlook is down. This issue belongs to the Outlook object model. Or, maybe to the Exchange + Outlook configuration. Anyway, since there is a workaround - you can manage the list of old emails and scan the mailbox for new ones - I don't think Microsoft will ever recognize this issue a bug.

Pieter also describes these well-known issues: 1) ItemAdd may not fire if a lot of emails is added to a folder, and 2) NewMail(Ex) may not fire if a lot of emails is received. These belongs to the Outlook object model as well.

Regards from Belarus (GMT+3),

Andrei Smolin
Add-in Express Team Leader
Posted 02 Mar, 2017 03:02:33 Top