Wednesday 19 August 2015

Consuming Outlook NewMailEx event in .NET

In .NET console application add reference to Microsoft.Office.Interop.Outlook. For Office 2013 choose version 15.0.0.0 of this assembly.

On start, this console application detects if Outlook is already running. In this case it uses existing Outlook.Application instance, which it retrieves via GetActiveObject call. Otherwise it creates a new instance.

Next step is creating a handler for the NewMailEx event. Such handler receives a collection of email IDs allegedly delimited by commas. For each ID, corresponding instance of Outlook.MailItem can be retrieved via GetItemFromID call.

Such console application, if uses shared Outlook.Application instance, must run under same security context as the Outlook itself. In other words, run Visual Studio as a regular user, not as administrator.

using System;
using System.Diagnostics;
using System.Linq;
using System.Runtime.InteropServices;
using Microsoft.Office.Interop.Outlook;

namespace OutlookAutomation.ConsoleApplication1
{
    class Program
    {
        private static Application _app;

        static void Main()
        {
            var newApp = false;
            _app = null;

            if (Process.GetProcessesByName("Outlook").Any())
            {
                try
                {
                    //share existing instance
                    _app = Marshal.GetActiveObject(
                        "Outlook.Application") 
                        as Application;
                }
                catch
                {
                }
            }

            if (_app == null)
            {
                //create new instance
                _app = new Application();
                newApp = true;
            }

            _app.NewMailEx += OnNewMail;

            //do not press any key right away :)
            //wait for some emails to come
            Console.Write("\n\nAny key...");
            Console.ReadKey();

            if (_app != null && newApp)
            {
                _app.Quit();
                _app = null;
            }
        }

        static void OnNewMail(string entryIdCollection)
        {
            Console.WriteLine(
                "\nNew mail arrived at {0}", 
                DateTime.Now);

            var emailIDs = entryIdCollection.Split(',');

            foreach (var id in emailIDs)
            {
                ProcessNewMail(_app.GetNamespace("MAPI")
                    .GetItemFromID(id) as MailItem);
            }
        }

        private static void ProcessNewMail(MailItem item)
        {
            if (item == null) return;

            Console.WriteLine("\tReceived: {0}", 
                item.ReceivedTime);

            Console.WriteLine("\tSender: {0}", 
                item.Sender.Address);

            Console.WriteLine("\tSubject: {0}", 
                item.Subject);

            var folder = item.Parent as MAPIFolder;
            if (folder != null)
            {
                Console.WriteLine("\tFolder: {0}", 
                    folder.FullFolderPath);
            }
        }
    }
}


No comments:

Post a Comment