Visual Studio 2008's Outlook Add-in projects allows you to create add-ins that can customize almost any feature of Ms Outlook. Two useful events that can be captured are ReminderFire and NewMailEx. The first occurs before a reminder of a calendar item is executed. And the second occurs when a new email is received in the Inbox.
Following are the list of namespaces imported to reduce some typing.
using Outlook = Microsoft.Office.Interop.Outlook; using System.Windows.Forms; //for MessageBox object
The ThisAddIn_Startup event is created automatically with the project and is called before Outlook is launched. In this event the fields _Explorers, _Inspectors and outlookNamspace are initialized. _Explorers will contain the Outlook's Explorers collection, which are the the windows that display the contents of a folder, like the Inbox. The _Inspectors object will contain the Outlook Inspectors collection. An Inspector is a window that displays the contents of a single item like an email or a calendar appointment. And the outlookNamespace field will reference the Mail namespace.
private Outlook.Explorers _Explorers; // the Outlook Explorers collection private Outlook.Inspectors _Inspectors; // the Outlook Inspectors collection private Outlook.NameSpace outlookNamespace; private void ThisAddIn_Startup(object sender, System.EventArgs e) { _Explorers = this.Application.Explorers; _Inspectors = this.Application.Inspectors; _Explorers.Application.NewMailEx += new Outlook.ApplicationEvents_11_NewMailExEventHandler(Application_NewMailEx); _Explorers.Application.Reminders.ReminderFire += new Outlook.ReminderCollectionEvents_ReminderFireEventHandler(Application_ReminderFire); outlookNamespace = this.Application.GetNamespace("MAPI"); }
The ReminderFire event's only parameter is a Reminder object that can be used to get the details of the reminder, like OriginalReminderDate and NextReminderDate if it's recurring.
private void Application_ReminderFire(Outlook.Reminder reminder) { MessageBox.Show(reminder.Caption, "New Reminder", MessageBoxButtons.OK); }
The NewMailEx event has one parameter too. It's a string containing the ID that can be used to retrieve the email. This could be done by passing the ID to the GetItemFromID method of the Application.Session object and getting a MailItem object. The MailItem object has all the email's details, some of the available properties are Attachments, Body, BodyFormat, CC and BCC.
private void Application_NewMailEx(string EntryID) { Outlook.MailItem newMail = (Outlook.MailItem)_Explorers.Application.Session.GetItemFromID( EntryID, System.Reflection.Missing.Value); if (newMail.Subject != null) { MessageBox.Show("From: " + newMail.SenderEmailAddress + "\nSubject: " + newMail.Subject, "New Email", MessageBoxButtons.OK); } else { MessageBox.Show("You've got mail."); } }