none
NewMailEx event and its entryid collection RRS feed

  • Question

  • Hi All

    In my last project I was exposed to the NewMailEx event because of my need to handle mail from three different mailboxes under the same Outlook profile.  This event did indeed fire for newly arriving mail in all three mailboxes - that's the "giveth part".  The "taketh away" part is the reality that when the event is fired for the second mailbox the entryid collection that arrives with the interrupt belongs to the "first" store - why?  It is apparent that the Oulook model knows what mailbox has caused the interrupt so why doesn't it deliver the associated data space (the Store) of that mailbox that created the interrupt!  It seems that would be easier to do then switching back to "first" store.  I can only speculate that the interrupt mechanism is subordinate to the Store level and consequently is sends the "default, first" Stores' "data space".  The solution of course is to search all three Stores - not impressive!

    Respectfully,

    saahg


    Gordon Haas

    Tuesday, April 7, 2015 3:17 PM

Answers

  • As a workaround you may create a rule and assign a VBA macro sub which should be in the following format:

    public sub test(mail as MailItem)

       ' do whatever you need

    end sub

    There is no need to call the GetItemFromId method in that case. The incoming mail item is passed as a parameter.

    • Marked as answer by saahg Wednesday, April 8, 2015 1:08 PM
    Wednesday, April 8, 2015 6:21 AM

All replies

  • What makes you think you get an entry ide from the first store?

    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.5 is now available!


    Tuesday, April 7, 2015 3:26 PM
  • Hello Gordon,

    What Outlook version are you talking about?

    Anyway, MSDN states the following for the NewMailEx event:

    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.

    Also you may find the following series of articles helpful:

    Outlook NewMail event unleashed: the challenge (NewMail, NewMailEx, ItemAdd)

    Outlook NewMail event: solution options

    Outlook NewMail event and Extended MAPI: C# example

    Outlook NewMail unleashed: writing a working solution (C# example)

    Tuesday, April 7, 2015 3:27 PM
  • Hi Dimitry

    Because my test showed all item association was relative to the "first" store.  I was using the following kind of construct to acquire the "mail items" Set mitm = ns.GetItemFromID(arr(i)) where ns was the namespace.session object and arr(i) was the "delivered" entryid collection.  The mitm objects where the mail items from the first store.  If I am seeing something incorrectly please correct me because I thought it was very strange that the delivered collection was not mapped to the corresponding Store (the second one in this case).

    Respectfully,

    saahg


    Gordon Haas

    Tuesday, April 7, 2015 4:17 PM
  • Hi Eugene

    The version I am working with is 2010.  What is the difference between the "NameSpace.GetItmeFromID" method and the "set mitm = ns.GetItemFromID(arr(i))" where ns is the NameSpace.Session object, the latter being the command that I am using.  My command sets mitm to the items of the "first store".  I must admit that I am still not solid on the "levels" of operation those being Application, Namespace, etc so I may well not be seeing the difference between your reference and mine - please inform.  Thanks in advance.

    Respectfully,

    saahg


    Gordon Haas

    Tuesday, April 7, 2015 4:35 PM
  • Sometimes the GetItemFromID method may fail. You need to touch stores to get them initialized properly. 

    We had already discussed the same question on the Error when opening some Outlook MailItems with GetItemFromID using EntryID from Outlook Spy forum thread. Initialize all stores in the profile to make sure they were loaded correctly. You can simply iterate over stores to initialize them (i.e. touch them in any way). If not, you need to specify the second optional parameter explicitly.

    Tuesday, April 7, 2015 4:36 PM
  • So which item would it return? The new item from the first store? Or some old item?

    What kind of store are you using? Keep in mind that NewMailEx event will not fire for the delegate Exchange mailboxes.


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.5 is now available!

    Tuesday, April 7, 2015 6:42 PM
  • We are talking about the same method. Does it fire exceptions or error messages in the code? Did you try to debug?
    Tuesday, April 7, 2015 9:27 PM
  • Hi Dimitry

    It would return an old item from the " First Store".  It sounds like you must refresh the "Stores" in order to insure that the correct one is "in place".  If this is had been done then more than likely the correct item would have surfaced.

    There is no Exchange Server involved.  This is a simple Outlook Profile on a Windows 7 system - three mailboxes.  The firing of the NewMailEx event has never been a problem.  It fires reliably but the collection - well that's another story.  Now that I have learned about the "Stores" nuance I am sure I can find the correct item.  What I did as a work around is simply scan all three stores.  I know ahead of time what the subject is going to be - it is a very controlled environment.

    Respectfully,

    saahg


    Gordon Haas

    Tuesday, April 7, 2015 11:15 PM
  • Hi Eugene

    I thought we were talking about the same method.  No exceptions or error messages just data from the first Store.  It sounds like your added info regarding the "Stores needing to be touched" is the fix.  I thought it was very strange that the event would fire on the second mailbox and present the "hooks" to the first mailbox but being completely new to this event I thought it must have been its characteristic.  I have seen a lot of unusual results concerning the Outlook Object model, i.e. different amounts of data exposed on the same object depending upon the event.  I once asked Ken Slovak if there was book that would identify the exposure level per event on mail items, etc.  His reply was "none that I know of".  So I just thought it was one of those nuances of the model.

    Respectfully,

    saahg


    Gordon Haas

    Tuesday, April 7, 2015 11:24 PM
  • How do you set up the event handler? I had no problem running the following script in OutlookApy (click Application button, go to the Script tab, paste the script, receive new message). I always get the right item.

    sub Application_newmailex(entryids)
      'Outlook really passes one id at a time
      set item = Application.Session.GetItemFromID(entryids)
      MsgBox(item.Subject & " - " & item.Parent.Store.DisplayName)
    end sub


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.5 is now available!

    Tuesday, April 7, 2015 11:37 PM
  • Hi Dimitry

    I'm not sure what environment you are testing.  If I were to check the mail from the first mailbox I always got the correct item.  It was when a message for the second mailbox arrived that the item fetched from the collection "set" failed.  Your setup is quite different from mine in that I am using an array to collect the "entryid set" which I thought was every message in the "inbox".  Here is my setup minus some dim's.

     

    Private Sub Application_NewMailEx(ByVal EntryIDCollection As String)

      Set ns = Application.Session
        On Error Resume Next 
        arr = Split(EntryIDCollection, ",")
        For i = 0 To UBound(arr)
            Set m = ns.GetItemFromID(arr(i))
            If UCase(m.Subject) = "PHOTOS" Then
                Debug.Print "doing a photo copy"
                Set atth = m.Attachments
                atthCnt = atth.Count
                Debug.Print "attachment count is "; atthCnt
                If atthCnt > 0 Then
                   For j = atthCnt To 1 Step -1
                       atthFile = atth.item(j).FileName
                       If InStr(1, atthFile, ".txt", vbTextCompare) > 0 Then
                          GoTo noimage
                       End If
                       fl = "c:\users\gordon2\documents\intrphotos\" + atthFile
                       Debug.Print "saving file "; fl
                       atth.item(j).SaveAsFile fl
    noimage:
                   Next
                End If
                m.Delete
                Set m = Nothing
                Set atth = Nothing
            End If
        Next

    It appears as though you are getting one id from the collection.  It was my understanding that the passed argument on this event was an "array" and thus the arr() array in my setup.  As I said from the beginning this is my first exposure to this event and that is why I am trying to understand how it is suppose to work and what is the architecture of it.

    Respectfully,

    saahg


    Gordon Haas

    Wednesday, April 8, 2015 2:41 AM
  • Have you configured Outlook accounts to deliver emails to the single Inbox folder?

    See Deliver all POP3 Accounts to one Outlook Inbox for more information.

    Wednesday, April 8, 2015 6:13 AM
  • As a workaround you may create a rule and assign a VBA macro sub which should be in the following format:

    public sub test(mail as MailItem)

       ' do whatever you need

    end sub

    There is no need to call the GetItemFromId method in that case. The incoming mail item is passed as a parameter.

    • Marked as answer by saahg Wednesday, April 8, 2015 1:08 PM
    Wednesday, April 8, 2015 6:21 AM
  • HI Eugene

    To answer the other post the Outlook configuration is 3 separate independent mailboxes.

    I do have a workaround in place.  It causes a little more work but because of the nature of the mail it is trivial.  The task that was on my plate was to create three mailboxes under Outlook for my client's three veterinary clinics to which they could send photos of the pets and then upload them to the veterinary practice management system.  The data mass will be small to say the least most probably initially 10 emails per box a day.  

    My workaround was to simply scan all three stores whenever the NewMailEx event fires.  The workload is further reduced because I delete the inbox message after the photo image is transferred by the vba code.  I will experiment some more with the "details" of the event.  I think you actually "nailed" the problem and that is the "stores" need to be touched prior to item access.  I am marking this post as an answer.  Again thanks Eugene.

    Respecfully,

    saahg


    Gordon Haas

    Wednesday, April 8, 2015 1:08 PM