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.
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.
- 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. - In the Add Reference dialog box, click
OK.
- If you are prompted to generate a wrapper, click
Yes.
- Add a reference to the System.Windows.Forms.dll file. To do
this, follow these steps:
- On the Project menu, click Add
Reference.
- Click the .NET tab, locate
System.Windows.Forms.dll, and then click
Select.
- In the Add Reference dialog box, click
OK.
- 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
}
} - Press the F5 key to build and to run the
program.
- 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Â
(http://kbalertz.com/Feedback.aspx?kbNumber=249156/
)
OL2000: ItemAdd event doesn't fire in some scenarios
290653Â
(http://kbalertz.com/Feedback.aspx?kbNumber=290653/
)
OL2002: ItemAdd event doesn't run in some scenarios
For more information, click the
following article numbers to view the articles in the Microsoft Knowledge Base:
290498Â
(http://kbalertz.com/Feedback.aspx?kbNumber=290498/
)
You receive warning messages when you use a third-party add-in or custom solution in Outlook 2002
290499Â
(http://kbalertz.com/Feedback.aspx?kbNumber=290499/
)
Administrator information about E-mail security features