ItemAddEventHandler only runs once

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi,

Im trying to catch all incoming new mail using the code below. The problem
is that it is only run once, so when i get 3 new messages, only the first
mail will be used in the eventhandler.

--


private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
Outlook.MAPIFolder inbox =
Application.Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox);
inbox.Items.ItemAdd += new
Outlook.ItemsEvents_ItemAddEventHandler(inboxFolderItemAdded);
}

public void inboxFolderItemAdded(object item)
{
if (item is Outlook.MailItem)
{
Outlook.MailItem mail = (Outlook.MailItem)item;

MessageBox.Show(mail.Subject.ToString());
}
}
 
Declare the inbox object at class level and not in ThisAddIn_Startup and I'd
recommend also declaring an object for inbox.Items and attaching the event
handler to that instead of to inbox.Items as you are doing.

The way that object is declared it's going out of scope and is being garbage
collected.
 
Back
Top