Wednesday, December 9, 2015

Accessing Unread Mails from Outlook through C# Code

This example shows how to get unread mails from outlook through C# code.

Getting the collection of Unread mails using Restrict() method.

The following demonstrates how to retrieve data from items within an Outlook folder (called "IN" under the Inbox folder) using .NET.

First add a reference to the Outlook COM object your project.
  1. In VS.NET right click on References and choose Add Reference.
  2. Select the COM tab
  3. Choose "Microsoft Outlook 15.0 Object Library" (this is for MS Office 2013 ) and click Select.
  4. Click OK.
            using Microsoft.Office.Interop.Outlook;

            Microsoft.Office.Interop.Outlook.Application app = null;
            Microsoft.Office.Interop.Outlook._NameSpace namespce = null;
            Microsoft.Office.Interop.Outlook.MailItem item = null;
            Microsoft.Office.Interop.Outlook.MAPIFolder inboxFolder = null;
            Microsoft.Office.Interop.Outlook.MAPIFolder subFolder = null;
            Microsoft.Office.Interop.Outlook.Items inboxItems = null;
            Microsoft.Office.Interop.Outlook.Items restrictedItems = null;


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

                inboxFolder = namespce.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderInbox);
                subFolder = inboxFolder.Folders["IN"]; //folder.Folders[1]; also works
                Console.WriteLine("Folder Name: {0}, EntryId: {1}", subFolder.Name, subFolder.EntryID);
                Console.WriteLine("Num Items: {0}", subFolder.Items.Count.ToString());

                foreach (Folder folder in inboxFolder.Folders)
                {

                    if (folder.Name == "IN")
                    {
                        Console.WriteLine(folder.Name);
                        inboxItems = folder.Items;
                        restrictedItems = inboxItems.Restrict("[Unread]=true");
                        for (int i = 1; i <= restrictedItems.Count; i++)
                        {
                            item = (Microsoft.Office.Interop.Outlook.MailItem)folder.Items[i];
                            Console.WriteLine("Subject: {0}", item.Subject);
                        }
                    }
                }
            }
            catch (System.Runtime.InteropServices.COMException ex)
            {
                Console.WriteLine(ex.ToString());
            }
            finally
            {
                namespce = null;
                app = null;
                inboxFolder = null;
            }

We can also use Find() and FindNext() of outlook items instead of Restrict() for reading unread mails.
The Find() and FindNext() methods of the Outlook Items object are faster than the Restrict ()method, but only when working with a small number of items. The Find() method returns a single item based on the filter, but if we want to retrieve the next mail item that matches the filter we would have to call the FindNext() method.

We can loop through unread mails returned by the Folder.GetTable() method.The GetTable() method can be found on the outlook folder object and returns a Table object.Each item returned inside the Table object only contains a certain number of fields ,we can load the items returned using the GetItemFromID() method.


1 comment: