Thursday, June 7, 2012

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Office.Interop.Outlook;

using System.Threading;

namespace OutlookReader
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                Microsoft.Office.Interop.Outlook.Application myApp = new Microsoft.Office.Interop.Outlook.Application();
                Microsoft.Office.Interop.Outlook.NameSpace mapiNameSpace = myApp.GetNamespace("MAPI");
                Microsoft.Office.Interop.Outlook.MAPIFolder myInbox = mapiNameSpace.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderInbox);
                Microsoft.Office.Interop.Outlook.Items items = myInbox.Items;

                int i = 0;
                foreach (Object mail in items)
                {
                    i++;
                    if ((mail as Microsoft.Office.Interop.Outlook.MailItem) != null && (mail as Microsoft.Office.Interop.Outlook.MailItem).UnRead == true)
                    {
                        Console.WriteLine(
                            "Subject " + i.ToString() + ": " + ((Microsoft.Office.Interop.Outlook.MailItem)mail).Subject + "\n" +
                            "Body: " + ((Microsoft.Office.Interop.Outlook.MailItem)mail).Body + "\n"
                            );

                            Thread.Sleep(5000);
                    }
                }

                Console.WriteLine("End of messages");
                Thread.Sleep(10000);
            }
            catch (System.Exception ex)
            {
                ex.Data.Clear();
            }
        }
    }
}