Microsoft KB Archive/895940

From BetaArchive Wiki
Knowledge Base


How to use the NewMail event or the NewMailEx event to monitor Outlook and to notify you in Visual C# .NET that new e-mail messages have arrived

Article ID: 895940

Article Last Modified on 1/16/2007



APPLIES TO

  • Microsoft Visual C# .NET 2003 Standard Edition
  • Microsoft Outlook 2000 Standard Edition
  • Microsoft Outlook 2002 Standard Edition
  • Microsoft Office Outlook 2003



SUMMARY

Introduction

This article describes how to use the NewMail event to monitor Microsoft Outlook 2000 or Microsoft Outlook 2002, and how to use the NewMailEx event to monitor Microsoft Office Outlook 2003. The NewMail event and the NewMailEx event will notify you in Microsoft Visual C# .NET regarding the arrival of new e-mail messages in the Inbox in Outlook.

MORE INFORMATION

You can use the NewMail event to monitor Outlook 2000 or Outlook 2002, or you can use the NewMailEx event to monitor Outlook 2003.

When a new e-mail message arrives in the Inbox in Outlook 2000 or in Outlook 2002, the e-mail message will trigger the NewMail event to notify you in Visual C# .NET that a new e-mail message has arrived.

When a new e-mail message arrives in the Inbox in Outlook 2003, the e-mail message will trigger the NewMailEx event to notify you in Visual C# .NET that a new e-mail message has arrived. The notification that you receive will also include the EntryID property of the new e-mail message that has arrived.

For notification in Visual C# .NET that a new e-mail message has arrived in the Inbox in Outlook, follow these steps:

  1. Start Microsoft Visual Studio .NET.
  2. On the File menu, click New, and then click Project.
  3. Under Project Types, click Visual C# Projects.
  4. Under Templates, click Console Application, type OPineApp in the Name box, and then click OK. By default, the Class1.cs file is generated.
  5. Change the Class1.cs file name to OPineApp.cs.
  6. Add a reference to the Object library that corresponds to the version of Microsoft Outlook that you are working in. To do this, follow these steps:
    1. On the Project menu, click Add Reference.
    2. Click the COM tab.

      If you are using Outlook 2000, locate Microsoft Outlook 9.0 Object library, and then click Select.

      If you are using Outlook 2002, locate Microsoft Outlook 10.0 Object library, and then click Select.

      If you are using Outlook 2003, locate Microsoft Outlook 11.0 Object library, and then click Select.
    3. In the Add Reference dialog box, click OK.
    4. If you are prompted to generate a wrapper, click Yes.
  7. Add a reference to the System.Windows.Forms.dll file. To do this, follow these steps:
    1. On the Project menu, click Add Reference.
    2. Click the .NET tab, locate System.Windows.Forms.dll, and then click Select.
    3. In the Add Reference dialog box, click OK.
  8. If you are using Outlook 2000 or Outlook 2002, replace the code in the code window with the following code:

    using System;
    using System.Windows.Forms;
    using Outlook;
    using OutLookApp = Outlook.Application;
    
    namespace OPine
    {
        class OPineApp
        {
            [STAThread]
            static void Main(string[] args)
            {
                // Create an Outlook application object. 
                ApplicationClass outLookApp = new ApplicationClass();
    
                // Ring up the new message event.
                    outLookApp.NewMail+= new ApplicationEvents_10_NewMailEventHandler(outLookApp_NewMail);
                    Console.WriteLine("Please wait for new messages...");
                    Console.ReadLine();
            }
            #region NewMail event handler. 
            private static void outLookApp_NewMail()
            {
                 MessageBox.Show("You've got a new mail!","Note", 
                    MessageBoxButtons.OK)
            }
            #endregion
        }
    
        
    }

    If you are using Outlook 2003, replace the code in the code window with the following code:

    using System;
    using System.Windows.Forms;
    using Outlook;
    
    // To use this namespace you must set a
    // reference to the Microsoft Outlook 11.0
    // COM server. 
    
    using OutLookApp = Outlook.Application;
    
    namespace OPine
    {
        class OPineApp
        {
            [STAThread]
            static void Main(string[] args)
            {
                // Create an Outlook application object. 
                ApplicationClass outLookApp = new ApplicationClass();
    
                // Ring up the new message event.
                outLookApp.NewMailEx+= new ApplicationEvents_11_NewMailExEventHandler(outLookApp_NewMailEx);
                Console.WriteLine("Please wait for new messages...");
                Console.ReadLine();
            }
            #region NewMail event handler. 
            private static void outLookApp_NewMailEx(string EntryIDCollection)
            {
                MessageBox.Show("You've got a new mail whose EntryIDCollection is \n" + EntryIDCollection,
                        "NOTE", MessageBoxButtons.OK);
            }
            #endregion
        }
    
        
    }
  9. Press the F5 key to build and to run the program.
  10. From the version of Outlook that you are using, send an e-mail message to confirm that you will be notified in Visual C# .NET that a new e-mail message has arrived in the Inbox in Outlook.

Note If you are using Microsoft Outlook 2000 Service Pack 2 (SP2) or later versions of Microsoft Outlook, you must change the security setting in the Outlook Administrator Pack (Admpack.exe). You may also want to modify the security settings in Registry Editor.

If you want to process the e-mail messages that arrive in the Inbox in Outlook, consider using the ItemAdd event. The ItemAdd event passes a reference to each e-mail message that arrives in the Inbox in Outlook.

Notes

  • The ItemAdd event does not run when many e-mail messages arrive at the same time in the Inbox in Outlook.
  • The ItemAdd event is not available in Microsoft Visual Basic Scripting Edition (VBScript).

For more information about the ItemAdd event, visit the following Microsoft Web site:

You can also find more information about the ItemAdd event by clicking the following article numbers to view the articles in the Microsoft Knowledge Base:

249156 OL2000: ItemAdd event doesn't fire in some scenarios


290653 OL2002: ItemAdd event doesn't run in some scenarios


REFERENCES

For more information, click the following article numbers to view the articles in the Microsoft Knowledge Base:

290498 You receive warning messages when you use a third-party add-in or custom solution in Outlook 2002


290499 Administrator information about E-mail security features


Keywords: kbcode kbhowto KB895940