ItemAddEventHandler only runs once

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());
}
}
 
K

Ken Slovak - [MVP - Outlook]

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top