Microsoft KB Archive/895940

= 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

-



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:  Start Microsoft Visual Studio .NET. On the File menu, click New, and then click Project. Under Project Types, click Visual C# Projects. Under Templates, click Console Application, type OPineApp in the Name box, and then click OK. By default, the Class1.cs file is generated. Change the Class1.cs file name to OPineApp.cs. 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:  On the Project menu, click Add Reference.</li> 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.</li> In the Add Reference dialog box, click OK.</li> If you are prompted to generate a wrapper, click Yes.</li></ol> </li> Add a reference to the System.Windows.Forms.dll file. To do this, follow these steps: <ol style="list-style-type: lower-alpha;"> On the Project menu, click Add Reference.</li> Click the .NET tab, locate System.Windows.Forms.dll, and then click Select.</li> In the Add Reference dialog box, click OK.</li></ol> </li>  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(&quot;Please wait for new messages...&quot;); Console.ReadLine; }       #region NewMail event handler. private static void outLookApp_NewMail {            MessageBox.Show(&quot;You've got a new mail!&quot;,&quot;Note&quot;,                 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(&quot;Please wait for new messages...&quot;); Console.ReadLine; }       #region NewMail event handler. private static void outLookApp_NewMailEx(string EntryIDCollection) {           MessageBox.Show(&quot;You've got a new mail whose EntryIDCollection is \n&quot; + EntryIDCollection,                    &quot;NOTE&quot;, MessageBoxButtons.OK); }       #endregion }

} </li> Press the F5 key to build and to run the program.</li> 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.</li></ol>

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.

<div class="moreinformation_section">

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:

http://msdn2.microsoft.com/en-us/library/aa157829(office.10).aspx

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

<div class="references_section">