ApplicationEvents_11_NewMailExEventHandler fires multiple times for a single email

  • Thread starter Thread starter Paddy
  • Start date Start date
P

Paddy

I have a simple test app that I have written to gain some knowledge
about the API for accessing Outlook using Visual Studio .NET

Relevant snippets are
........
while(true)
{
Application outlook = new Application();
outlook.NewMailEx += new
ApplicationEvents_11_NewMailExEventHandler(outlook_NewMailEx);


}


........
private static void outlook_NewMailEx(string EntryIDCollection)
{
Console.WriteLine("New mail arrived .... !");


}


I realise that there may be a problem with the "while(true)" part
(otherwise it simply exits when the process starts) but I was expecting

that when I send the test email, I should see only one line at the
console saying "New mail arrived ....!". Instead I see thousands !!

Any help is appreciated,
- K
 
What are you doing is subscribing to the event infinite number of times.
What you probably want to do is:

(i assume you have a console application)

Application outlook = new Application();
outlook.NewMailEx += new
ApplicationEvents_11_NewMailExEventHandler(outlook_NewMailEx);
Console.ReadLine(); //this stops the current thread until you press enter in
the console

if you have winform application, just subscribe to the event in the
Form.Load event - and do it once J
 
I solved it by creating a new thread and calling the delegate on it
..... But dang it, your solution is much more simpler !
Thanks,
- K
 
Back
Top