Difference between NewMail and NewMailEx events?

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

Difference between NewMail and NewMailEx events?
 
Esteban Astudillo


Guest


Hi,

What's the difference between the NewMail and NewMailEx events in the ADXOutlookAppEvents class? When would you use one or the other? They both have the same description so it's hard to figure it out.

Also, I was looking in the examples and I couldn't find one using either event. Have you created an example using one of these two events? In that case, can I have a copy of it?

Thanks in advance

Esteban
Posted 27 Feb, 2006 18:17:12 Top
Brad Smith




Posts: 49
Joined: 2005-11-22
NewMail is the event triggered when one (or more) new messages are received in the Inbox. The limitation is that the even doesn't include any information on the actual new message. And in an Exchange environment, you may get multiple new messages simultaneously but only get one NewMail event.

NewMailEx, on the other hand, includes a list of EntryIDs (in string form) for all the messages that have just arrived. You can then traverse that list and call GetItemFromID (that's a MAPISession method), if you need to perform some item-specific handling.
NewMailEx is only for Outlook2003 though. It's not available in the older versions of Outlook. If you need to support the old OL versions, you're out of luck.

Hopefully Sergey has a suitable sample kicking around somewhere.

Brad.
Posted 27 Feb, 2006 18:38:36 Top
Esteban Astudillo


Guest


Thank you Brad, your post is really useful and also opens a lot of questions for me.

I want to provide a solution compatible with Outlook 2000, so I'm going to focus for now on the NewMail event.

What would be the strategy in this case? Trap the event and then go thru the list of messages in the Inbox looking for new emails? Would this work if there are active Rules at the time (moving the mail item to a different folder)?

I would be surprised I'm the first person dealing with this and in fact I would imagine that this would have been one of the first issues ADX had to deal with, no?

Anyway, thank you again for your time and if anybody can recommned a general strategy on how to do this using the NewMail event it will be highly appreciated.

Esteban
Posted 27 Feb, 2006 18:49:40 Top
Esteban Astudillo


Guest


Well, looking at different examples I managed to ensemble one test project where I use the GetItemFromID method as recommended by Brad above. However I can't get to catch all events. I'm not using Exchange, so I thought I should be able to trap all instances of the event but when I sent two emails to the Outlook account the NewMailEx event is triggered only once. The EntryIDCollection contains only one value (the first one, in fact) and the event is not triggered for the second item either.

I will now try with the NewMail event but I haven?Â?Ð?ét found an example to get to the message once the event is triggered? Any hints?

Thanks in advance
Posted 27 Feb, 2006 23:38:43 Top
Sergey Grischenko


Add-in Express team


Posts: 7233
Joined: 2004-07-05
Hi Esteban.

Please look at the Microsoft Visual Basic Help in Outlook. I found the following information about the NewMailEx event.

NewMailEx:
Occurs when one or more new items are received in the Inbox. This event passes a list of entry IDs of all the items received in the Inbox since the last time the event was fired.

Example:
Public WithEvents outApp As Outlook.Application

Sub Intialize_Handler()
Set outApp = Application
End Sub

Private Sub outApp_NewMailEx(ByVal EntryIDCollection As String)
Dim mai As Object
Dim intInitial As Integer
Dim intFinal As Integer
Dim strEntryId As String
Dim intLength As Integer

intInitial = 1
intLength = Len(EntryIDCollection)
MsgBox "Collection of EntryIds: " & EntryIDCollection
intFinal = InStr(intInitial, EntryIDCollection, ",")
Do While intFinal <> 0
strEntryId = Strings.Mid(EntryIDCollection, intInitial, (intFinal - intInitial))
MsgBox "EntryId: " & strEntryId
Set mai = Application.Session.GetItemFromID(strEntryId)
MsgBox mai.Subject
intInitial = intFinal + 1
intFinal = InStr(intInitial, EntryIDCollection, ",")
Loop
strEntryId = Strings.Mid(EntryIDCollection, intInitial, (intLength - intInitial) + 1)
MsgBox strEntryId
Set mai = Application.Session.GetItemFromID(strEntryId)
MsgBox mai.Subject

End Sub


Posted 28 Feb, 2006 06:36:35 Top
Brad Smith




Posts: 49
Joined: 2005-11-22
I haven't dealt with this using ADX specifically, but what I normally do is set up an event handler for the actual Inbox folder, trapping the ItemAdd event for the Inbox itself. That event *does* get the IDispatch for the item just added. Luckily, for most of the stuff I have to worry about, this works for me. But I know ItemAdd is also documented as being triggered once for multiple messages so you'll have to test.

If you're stuck with NewMail; and I'm just guessing here, having not actually done it; but what I'd probably do is probably trap the one event and loop through the folder looking at all unread messages. I'm pretty sure that the add-in event handlers are triggered *before* your rules.

You could, in theory, write back a private custom property when handling a message (since the message is unread anyway, that won't hurt). You can then look for your "already processed" property on subsequent loops.

As you probably surmised, the uselessness of the old NewMail event is what triggered the addition of the NewMailEx event. But it took until OL2003 for them to add it. Supporting the old Outlook's remains as painful as ever. :-(
Posted 28 Feb, 2006 08:36:16 Top
Esteban Astudillo


Guest


Thank you both for your help!

Sergey, I will give it a try to the code you found (thank you for finding it for me anyway; I should have been able to find it myself). I'm wondering though if this moves me away from the ADX-style of programming the add-in. Is this your final recommendation? (I was expecting a sample code that would make use of ADX events). If this is the way of programming it I?Â?Ð?ém OK with that, but I wanted to double check with you.

Brad, I really appreciate your guidance. I will explore all your comments carefully. As before, your post opens more questions for me, but that's always a good thing :) (I will post new topics with these later)

Detecting new emails is a key part of my product, so I'm thinking that a nice alternative would be to have different versions of my plug-in, one for OL2003 and another for previous versions of OL. This carries its own set of issues, but that way at least I would be able to take advantage of the new events. Right?

Having said this, this approach only works if I have a *guarantee* that the NewMailEx event is either triggered for each new email or if the entryIDCollection parameter gets the list of all of them. However, I'm afraid I already proved that the list is incomplete. I want to confirm this myself but has anyone had issues with this NewMailEx event before? (In my test I was using OL 2003 and a POP3-based mail server. I got three emails but the event was fired once and the list of IDs included only one item)

Thanks again.

Cheers..
Posted 28 Feb, 2006 12:46:24 Top
Sergey Grischenko


Add-in Express team


Posts: 7233
Joined: 2004-07-05
Hi Esteban.

I'm wondering though if this moves me away from the ADX-style of programming the add-in. Is this your final recommendation? (I was expecting a sample code that would make use of ADX events). If this is the way of programming it I?Â?Ð?ém OK with that, but I wanted to double check with you.

Add-in Express doesn't provide any specific events. Any code that you can find in help or the Internet can be used in ADX with minimum changes.


Having said this, this approach only works if I have a *guarantee* that the NewMailEx event is either triggered for each new email or if the entryIDCollection parameter gets the list of all of them. However, I'm afraid I already proved that the list is incomplete. I want to confirm this myself but has anyone had issues with this NewMailEx event before? (In my test I was using OL 2003 and a POP3-based mail server. I got three emails but the event was fired once and the list of IDs included only one item)

I have rewritten the code to C#. In OL 2003 the NewMailEx event is fired for every email. I will make an example that supports all versions of Outlook and will give you a link to download.

private void adxOutlookEvents_NewMailEx(object sender, string entryIDCollection)
{
object obj = null;
Outlook.MailItem mail = null;
Outlook._NameSpace ns = null;

try
{
ns = OutlookApp.GetNamespace("MAPI");
string[] ids = entryIDCollection.Split(',');
for(int i=0; i<ids.Length; i++)
{
obj = null;
try
{
obj = ns.GetItemFromID(ids[i], Type.Missing);
if (obj is Outlook.MailItem)
{
mail = obj as Outlook.MailItem;
MessageBox.Show(mail.Subject);
}
}
finally
{
if (obj != null)
Marshal.ReleaseComObject(obj);
}
}
}
finally
{
if (ns != null)
Marshal.ReleaseComObject(ns);
}
}



Posted 01 Mar, 2006 07:13:12 Top
Sergey Grischenko


Add-in Express team


Posts: 7233
Joined: 2004-07-05
Hi Esteban.

I have just sent you an email with the example. Please check your inbox.
Posted 01 Mar, 2006 20:55:43 Top
Esteban Astudillo


Guest


Hi Sergey,

Thank you for preparing this example. I will try it right away..

Posted 02 Mar, 2006 11:17:57 Top