1
votes

I am looking to monitor all new emails from Outlook 2013 and get the email's subject. After reading several blogs, I understood that the best/easy way is to use Microsoft.Office.Interop.Outlook with MAPI and ApplicationEvents_11_NewMailExEventHandler event.

I try this base on a blog and I did a bit of modification:

using System;
using System.Windows.Forms;
using Microsoft.Office.Interop.Outlook;


class EmailMonitoring
{
    static void Main(string[] args)
    {
        // Create an Outlook application object. 
        Microsoft.Office.Interop.Outlook.Application app = null;
        Microsoft.Office.Interop.Outlook._NameSpace ns = null;

        try
        {  
            //Email
            app = new Microsoft.Office.Interop.Outlook.Application();
            ns = app.GetNamespace("MAPI");
            ns.Logon(null, null, false, false);

            // Ring up the new message event.
            app.NewMailEx += new ApplicationEvents_11_NewMailExEventHandler(outLookApp_NewMailEx);
            Console.WriteLine("Please wait for new messages...");
            Console.ReadLine();
        }
        catch (System.Exception e)
        {
            Console.WriteLine("Exception in Main "+e);
        }
        finally
        {
            ns = null;
            app = null;

        }
    }

    private static void outLookApp_NewMailEx(string EntryIDCollection)
    {
        Console.WriteLine("You've got a new mail whose EntryIDCollection is \n" + EntryIDCollection);
        Microsoft.Office.Interop.Outlook.Application app = new Microsoft.Office.Interop.Outlook.Application();
        Microsoft.Office.Interop.Outlook.MailItem item = null;
        Microsoft.Office.Interop.Outlook._NameSpace ns = app.GetNamespace("MAPI");

        try
        {

            ns.Logon(null, null, false, false);

            item = ns.GetItemFromID(EntryIDCollection);
            Console.WriteLine("test" + item.Subject);
        }
        catch(System.Exception e)
        {
            Console.WriteLine("Exception in event Handler "+e);
         }
        finally
        {
            ns = null;
            app = null;

        }
    }

}

However, when I have a new email in outlook, the console display the EntryID and the subject, but Outlook become frozen for some specific emails and throw this error message: "Email has stoppped working."

  • Did I forgot to delete some objects?
  • Is there a more elegant way to do that? I do not like the fact that I need to define 2 Outlook.Applications.
  • Do you recommend any blogs/books which describe how to do that?
1
Your code is not cleaning up the explicit unmanaged Outlook COM resources. No doubt that's what's contributing to the hang. It might be the WHOLE reason, but for sure it's at least part of the problem. - Gayot Fow

1 Answers

1
votes

Try this, this won't lock your outlook file in the folder and new emails will be in queue

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Outlook = Microsoft.Office.Interop.Outlook;

namespace Sample
{
    public partial class frmSample : Form
    {
        Outlook.NameSpace outlookNameSpace;
        Outlook.MAPIFolder inbox;
        Outlook.Items items;
        public frmSample()
        {
            InitializeComponent();
        }

        private void frmSample_Load(object sender, EventArgs e)
        {
            Outlook.Application app = new Outlook.Application();
            outlookNameSpace = app.GetNamespace("MAPI");
            inbox = outlookNameSpace.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderInbox);
            items = inbox.Items;
            items.ItemAdd += new Outlook.ItemsEvents_ItemAddEventHandler(items_ItemAdd);
        }
        void items_ItemAdd(object Item)
        {
            Outlook.MailItem mail = (Outlook.MailItem)Item;
            if (Item != null)
            {
                string tempvariable = mail.To;
                tempvariable = mail.Subject;
                tempvariable = mail.Body;
                tempvariable = mail.SenderEmailAddress;
                tempvariable = mail.SenderName.ToString();                        
             }
           }
        }
    }
}