none
Obtaining the entryID of sent emails in outlook plugin using C# RRS feed

  • Question

  • Hi,

    I am trying to capture the unique entryID's of the sent items in outlook 2010.
    I am using the itemAdd event for sent item folder but the event is not getting fired at all.

    Now, after searching about this on internet, I found out that proper initialization of variable is required to ensure that this event fires however. i am not able to find any code sample in C# that shows the proper way of initializing the variables.

    For reference:
    I am attaching the itemAdd event in the "ThisAddIn_Startup()" function.
    Here is a code of what I have done till now:

    private Outlook.Items sentitems;
    private void ThisAddIn_Startup(object sender, System.EventArgs e)
            {

    Outlook.Application app = new Outlook.Application();
                Outlook._NameSpace nameSpace;
                nameSpace = app.GetNamespace("MAPI");
                nameSpace.Logon(null, null, false, false);
                sentitems = nameSpace.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderSentMail).Items;
                sentitems.ItemAdd += new Outlook.ItemsEvents_ItemAddEventHandler(item_ItemAdd);

    }

    void item_ItemAdd(object Item)
            {
                try
                {
                    Outlook.MailItem item = Item as Outlook.MailItem;

                }
                catch
                {

                }
            }

    I am new to outlook plugin development. Kindly help me.

    Thanks in advance.

    Wednesday, February 5, 2014 1:27 PM

Answers

  • Hello abhi922,

    Do you use Exchange server in the profile?

    The code looks good, except the fact that you don't release underlying COM objects instantly. For example, I have noticed the following line of code:

    sentitems = nameSpace.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderSentMail).Items;

    The GetDefaultFolder method of the Namespace class from the Outlook object model returns and instance of the Folder class which should be released after. 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. Please take a look at the Systematically Releasing Objects article in MSDN for more information.

    The ItemAdd event does not run when a large number of items are added to the folder at once. Is this the case? 

    Also you may find the Exchange 2010 and Outlook 2003: ItemAdd event does not fire in online mode article useful.

    P.S. There is no need to use the Logon method if you develop an add-in for Outlook. Here is what MSDN states for the Logon method:

    Use the Logon method only to log on to a specific profile when Outlook is not already running. This is because only one Outlook process can run at a time, and that Outlook process uses only one profile and supports only one MAPI session. When users start Outlook a second time, that instance of Outlook runs within the same Outlook process, does not create a new process, and uses the same profile.

    If Outlook is already running, using this method does not create a new Outlook session or change the current profile to a different one.


    Wednesday, February 5, 2014 1:45 PM
  • And do not create a new instance of the Outlook.Application object in an addin Application object is passed to your addin anyway, and, unlike the instance you get from "new", it is not crippled by the security prompts.


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

    Wednesday, February 5, 2014 2:49 PM
  • As Dmitry noticed, you create a new Application instance in the Startup event handler. Instead, you need to use the Application property provided by the VSTO base classes:

    Outlook.Items items = null;
    private void ThisAddIn_Startup(object sender, System.EventArgs e)
    {
      Outlook._NameSpace nameSpace = Application.GetNamespace("MAPI");
      Outlook.Folder sentItems = nameSpace.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderSentMail) as Outlook.Folder;
      items =  sentItems.Items;
      items.ItemAdd += items_ItemAdd;
    
      System.Runtime.InteropServices.Marshal.ReleaseComObject(sentItems);
      System.Runtime.InteropServices.Marshal.ReleaseComObject(nameSpace);
    }

    The ItemAdd event does not run when a large number (more than 16) of items are added to the folder at once. Is this the case? 

    Wednesday, February 5, 2014 5:47 PM

All replies

  • Hello abhi922,

    Do you use Exchange server in the profile?

    The code looks good, except the fact that you don't release underlying COM objects instantly. For example, I have noticed the following line of code:

    sentitems = nameSpace.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderSentMail).Items;

    The GetDefaultFolder method of the Namespace class from the Outlook object model returns and instance of the Folder class which should be released after. 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. Please take a look at the Systematically Releasing Objects article in MSDN for more information.

    The ItemAdd event does not run when a large number of items are added to the folder at once. Is this the case? 

    Also you may find the Exchange 2010 and Outlook 2003: ItemAdd event does not fire in online mode article useful.

    P.S. There is no need to use the Logon method if you develop an add-in for Outlook. Here is what MSDN states for the Logon method:

    Use the Logon method only to log on to a specific profile when Outlook is not already running. This is because only one Outlook process can run at a time, and that Outlook process uses only one profile and supports only one MAPI session. When users start Outlook a second time, that instance of Outlook runs within the same Outlook process, does not create a new process, and uses the same profile.

    If Outlook is already running, using this method does not create a new Outlook session or change the current profile to a different one.


    Wednesday, February 5, 2014 1:45 PM
  • And do not create a new instance of the Outlook.Application object in an addin Application object is passed to your addin anyway, and, unlike the instance you get from "new", it is not crippled by the security prompts.


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

    Wednesday, February 5, 2014 2:49 PM
  • Hi Eugene,

    Thanks for the prompt reply.

    I didn't understand what you mean by "Exchange Server in the profile". Kindly explain it.
    I had tested the application without using Logon also but still it didn't work and even if I send only one email at a time, still the ItemAdd event doesn't fire.
    Wednesday, February 5, 2014 3:47 PM
  • Are you still creating a new instance of Outlook.Application?

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

    Wednesday, February 5, 2014 4:42 PM
  • Eugene,

    Not is it not Exchange Server. Plugin is simply for Outlook Client, which is pulling emails - POP3 or IMAP

    Wednesday, February 5, 2014 5:36 PM
  • As Dmitry noticed, you create a new Application instance in the Startup event handler. Instead, you need to use the Application property provided by the VSTO base classes:

    Outlook.Items items = null;
    private void ThisAddIn_Startup(object sender, System.EventArgs e)
    {
      Outlook._NameSpace nameSpace = Application.GetNamespace("MAPI");
      Outlook.Folder sentItems = nameSpace.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderSentMail) as Outlook.Folder;
      items =  sentItems.Items;
      items.ItemAdd += items_ItemAdd;
    
      System.Runtime.InteropServices.Marshal.ReleaseComObject(sentItems);
      System.Runtime.InteropServices.Marshal.ReleaseComObject(nameSpace);
    }

    The ItemAdd event does not run when a large number (more than 16) of items are added to the folder at once. Is this the case? 

    Wednesday, February 5, 2014 5:47 PM