NewMailEx vs. ItemAdd

M

Mark Rae

Hi,

I'm trying to use Outlook 2003's Application_NewMailEx event to copy all
incoming messages into various folders etc.

I've pretty much lifted the code directly from here:
http://msdn.microsoft.com/library/d...n-us/odc_ol2003_ta/html/odc_OLWhatsNew2k3.asp

The actual mechanism certainly works, but intermittently it won't pick up
one or more of the incoming messages.

E.g. if there is only one incoming message, then it works perfectly.
However, if there are quite a few, as is common when I launch Outlook first
thing in the morning and there could be up to 50 new emails, then typically
this event seems to process only about 80% of them at random.

Is this a known issue? Would I be better off using the ItemAdd event
instead?

Any assistance gratefully received.

Mark
 
K

Ken Slovak - [MVP - Outlook]

Both events will fail at around 16 or more items coming in at the same time.
It's a known thing due to the underlying MAPI notification limitations and
there are no real workarounds other than scanning the Inbox at intervals to
sweep items that might have been missed.
 
M

Mark Rae

Both events will fail at around 16 or more items coming in at the same
time. It's a known thing due to the underlying MAPI notification
limitations

I see...
there are no real workarounds other than scanning the Inbox at intervals
to sweep items that might have been missed.

What's the most efficient way of doing that? By that, I mean that scanning
the Inbox is simple enough, but how do you remember which MailItem objects
in the Inbox have been processed from one sweep to the next? I don't really
want any sort of database functionality here...
 
G

Guest

Hi

I've tried it with just 2 new emails (not 16) but event was raised just for
the second.
I think that this event raised only once per "Send/Recieve All" action.

Is this correct?
 
K

Ken Slovak - [MVP - Outlook]

Well, yes but so what? You get a string that is a collection of the
EntryID's of every email that came in during that operation, separated by
commas. So if you split the string on commas your array should hold the id's
of every item that came in.
 
G

Guest

If I had a comma separated string with the EntryID's I would be very pleased.
But, I'm taking just one EntryID. The EntryID of the most recent email.

Am I missing something?
 
K

Ken Slovak - [MVP - Outlook]

I guess so, if more than 1 email comes in and you only get one EntryID.

I myself never use NewMailEx, I always use ItemAdd.
 
G

Guest

I've tried ItemAdd event but I have the same behavior as NewMailEX event has.
When two emails added to Items collection of Inbox folder, the event raised
just for the one of them (the most recent).
 
K

Ken Slovak - [MVP - Outlook]

Unless more than 16 items come in at once you will get ItemAdd events for
each item that comes in. If you aren't then you are taking way too much time
in your ItemAdd handler and causing the event to be missed.

It's like working with microprocessors in an assembly language context, if
you take too much time in an interrupt service handler you will miss the
following interrupts for that interrupt request.
 
G

Guest

Hi Ken,

Just to build on this solution. Lets say that more than 16 items arrived
and I want to go and search for the missed items. However there are rules
setup that moves the items to different folders. Can you move through the
folder hierarchy when you do not know the folder names?

My aim is to setup a timer that does this every 5 minutes or just to scan if
there are any emails that the ItemAdd event might have missed. I have to be
able to scan accross different folders this includes default folders and
non-default folders, so the GetDefaultFolder() method is a no go.

Here is a code snippet of what I have:

NameSpace objNamespace = this.GetNamespace("MAPI");
Folders folders = objNamespace.Folders;
foreach (MAPIFolder folder in folders)
{
foreach (MailItem mail in folder.Items)
{
if (mail.FlagStatus == OlFlagStatus.olNoFlag)
{
MailItem temp = (MailItem)mail;
temp.FlagStatus = OlFlagStatus.olFlagComplete;
temp.Save();
this.newItems.Add(mail);
}
}
}
 
K

Ken Slovak - [MVP - Outlook]

Kill all the rules. Seriously, otherwise you're looking at a logistical
nightmare.

A contacts folder can have subfolders and any of those can be mail folders,
etc. So you'd really need a recursive procedure that starts at Inbox.Parent
(top of store) and checks each folder's Folders collection to see if it has
any subfolders (Folders.Count) and then dig down that path until no more
subfolders, then back to the next path. Then for each mail folder you
discover you'd need to process items.

One thing you can never depend on is when a rule will fully fire. For
example, you have a rule to move an item somewhere. You also get ItemAdd on
the Inbox and if the message has property X you want to move it somewhere.
In most cases the rule code is aware of the message, ItemAdd fires and you
move the message, then the rule fully fires and throws an error that the
item has been moved. Very disconcerting to the user.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top