none
Get an event on new sent email in ol Folder Sent Mail RRS feed

  • Question

  • Hello,

    Actually what I want to do is

    foreach (Account account in Globals.ThisAddIn.Application.Session.Accounts) {
                  (Folder)account
                      .DeliveryStore
                      .GetDefaultFolder(OlDefaultFolders.olFolderSentMail)
                      .Items
                      .ItemAdd += SomeMethod;

    But ItemAdd is never rise... What can I do?


    • Edited by Aurelien312 Monday, February 8, 2016 5:23 PM
    Monday, February 8, 2016 5:23 PM

Answers

  • The items collection that fires the events must be kept alive (it is Garbage Collected in your case) - create List<Items> collection, retrieve the Items property, add it to the list, set the event handler.

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

    Monday, February 8, 2016 5:27 PM
  • You need to declare the source objects (an instance of the Items class) at the global scope preventing them from swiping by the garbage collector. Thus, you will be sure the events are fired all the time.

    Another aspect is that the event is not fired when a large number of items are added to the folder at once (more than 16). This is a known issue in OOM.


    P.S. Additionally, I'd recommend breaking the chain of property and method calls and declaring them on separate lines of code. Thus, you will be able to release underlying COM objects instantly. Use System.Runtime.InteropServices.Marshal.ReleaseComObject to release an Outlook object when you have finished using it.  Then set a variable to Nothing in Visual Basic (null in C#) to release the reference to the object. Read more about that in the Systematically Releasing Objects article.
    Monday, February 8, 2016 5:29 PM

All replies

  • The items collection that fires the events must be kept alive (it is Garbage Collected in your case) - create List<Items> collection, retrieve the Items property, add it to the list, set the event handler.

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

    Monday, February 8, 2016 5:27 PM
  • You need to declare the source objects (an instance of the Items class) at the global scope preventing them from swiping by the garbage collector. Thus, you will be sure the events are fired all the time.

    Another aspect is that the event is not fired when a large number of items are added to the folder at once (more than 16). This is a known issue in OOM.


    P.S. Additionally, I'd recommend breaking the chain of property and method calls and declaring them on separate lines of code. Thus, you will be able to release underlying COM objects instantly. Use System.Runtime.InteropServices.Marshal.ReleaseComObject to release an Outlook object when you have finished using it.  Then set a variable to Nothing in Visual Basic (null in C#) to release the reference to the object. Read more about that in the Systematically Releasing Objects article.
    Monday, February 8, 2016 5:29 PM
  • Thanks it work. I didn't thinks I need to keep a reference to the Items object.
    Monday, February 8, 2016 5:43 PM
  • Thanks

    I use Marshal.ReleaseComObject but I remove it for the sample.

    I will just supposed that 16 mail added bug is not a problem. I suppose there no other way to catch this even to repare that issue?

    Monday, February 8, 2016 5:47 PM
  • Do you mean the problem of the Items events not firing when a large number of items is added?

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

    Monday, February 8, 2016 7:44 PM
  • Reply, No sorry you already answer my question in fact the problem was to keep folder object in a list... Thanks for your help.
    Monday, February 29, 2016 8:14 AM