Sunday 21 July 2013

OutLook Addins C# - To Move the OutLook InBox Mails to UnRead Folder and Read Mails from UnRead Folder to Read Folder



            This Add ins is created for some real time purpose , Whenever a Mail is received in Outlook inbox a small notify is launched in Task bar for each mail. Sometimes we have a chance to miss a mail to read and leave it. How to avoid to this ?

Create  a Two folders inside the Inbox Like Read and UnRead , Whenever a Mail is received in inbox , Mail is moved to UnRead Folder.If we read the mail in the UnRead Folder Mails are moved to Read Folder.

Now we can see How we can do this in C# language by integrating with outlook.To do this we have to do Addins for Outlook.

       On Application start up, we check for the folders present in the inside the inbox , If Read and Unread folder are not created there, Then we are creating at start up.Then bind a method for the two events NewMail for Inbox to find the new mails received in inbox and ItemChange for UnRead Folder to find the Read mails


   private void ThisAddIn_Startup(object sender, System.EventArgs e)
        {
            Outlook.MAPIFolder inboxfolder = this.Application.Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox);            
            var inFolder = this.Application.Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox);
            Outlook.MAPIFolder ofolread = null;
            Outlook.MAPIFolder ofolunread = null;
            bool unreadexists = false;
            bool readexists = false;
           
            for (int i = 1; i <= inFolder.Folders.Count; i++)
            {
                if (inFolder.Folders[i].Name == "UnRead")
                {
                    unreadexists = true;
                }
                if (inFolder.Folders[i].Name == "Read")
                {
                    readexists = true;
                }
            }
            if (!unreadexists)
            {
                ofolunread = inFolder.Folders.Add("UnRead", Outlook.OlDefaultFolders.olFolderInbox);                         
            }
            if (!readexists)
            {
                ofolread = inFolder.Folders.Add("Read", Outlook.OlDefaultFolders.olFolderInbox);
            }
            if (ofolunread == null)
            {
                ofolunread = inFolder.Folders["UnRead"];
            }
            ofolunread.Items.ItemChange += new Outlook.ItemsEvents_ItemChangeEventHandler(Items_ItemChange);
            this.Application.NewMail += new Outlook.ApplicationEvents_11_NewMailEventHandler(Application_NewMail);                    
           
        }



Below image shows how the Outlook inbox looks it have the 392 unread mails in inbox, With no sub folders.




In the ItemChange event we are finding the items that are read in UnRead Folder and move to Read Folder.

  void Items_ItemChange(object Item)
        {
            Outlook.MAPIFolder inboxfolder = this.Application.Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox);
            Outlook.MAPIFolder unread = inboxfolder.Folders["UnRead"];
            Outlook.MAPIFolder read = inboxfolder.Folders["Read"];
            Outlook.MailItem email = null;
           foreach (object mail in unread.Items.Restrict("[UnRead] = false"))
           {
               try
               {
                   email = mail as Outlook.MailItem;
                   if (email != null)
                   {
                       email.Move(read);
                   }
               }
               catch (Exception ex)
               {
                   MessageBox.Show(ex.Message);
               }
           }
        }
  
In the NewMail event we are finding the Mails received in InBox and move to UnRead folder

   void Application_NewMail()
        {
            Outlook.MAPIFolder inboxfolder = this.Application.Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox);
            Outlook.Items items =(Outlook.Items)inboxfolder.Items;
            items.Restrict("[UnRead] = true");
            Outlook.MAPIFolder unread = inboxfolder.Folders["UnRead"];
            Outlook.MailItem email = null;
            foreach (object mail in items)
            {
                try
                {
                    email = mail as Outlook.MailItem;
                    if (email != null)
                    {
                        email.Move(unread);
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
        }



Above image shows That InBox folder have two sub - folders, One is Read and UnRead. You can see now the mails are moved from inbox folder to unread folder,Then the mails which are read in UnRead folder are moved to Read Folder.


 Now We see the Whole Program 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using Outlook = Microsoft.Office.Interop.Outlook;
using Office = Microsoft.Office.Core;
using System.Windows.Forms;

namespace UnReadMails
{
    public partial class ThisAddIn
    {
        private void ThisAddIn_Startup(object sender, System.EventArgs e)
        {
            Outlook.MAPIFolder inboxfolder = this.Application.Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox);            
            var inFolder = this.Application.Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox);
            Outlook.MAPIFolder ofolread = null;
            Outlook.MAPIFolder ofolunread = null;
            bool unreadexists = false;
            bool readexists = false;
           
            for (int i = 1; i <= inFolder.Folders.Count; i++)
            {
                if (inFolder.Folders[i].Name == "UnRead")
                {
                    unreadexists = true;
                }
                if (inFolder.Folders[i].Name == "Read")
                {
                    readexists = true;
                }
            }
            if (!unreadexists)
            {
                ofolunread = inFolder.Folders.Add("UnRead", Outlook.OlDefaultFolders.olFolderInbox);                         
            }
            if (!readexists)
            {
                ofolread = inFolder.Folders.Add("Read", Outlook.OlDefaultFolders.olFolderInbox);
            }
            if (ofolunread == null)
            {
                ofolunread = inFolder.Folders["UnRead"];
            }
            ofolunread.Items.ItemChange += new Outlook.ItemsEvents_ItemChangeEventHandler(Items_ItemChange);
            this.Application.NewMail += new Outlook.ApplicationEvents_11_NewMailEventHandler(Application_NewMail);                    
           
        }

        void Items_ItemChange(object Item)
        {
            Outlook.MAPIFolder inboxfolder = this.Application.Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox);
            Outlook.MAPIFolder unread = inboxfolder.Folders["UnRead"];
            Outlook.MAPIFolder read = inboxfolder.Folders["Read"];
            Outlook.MailItem email = null;
           foreach (object mail in unread.Items.Restrict("[UnRead] = false"))
           {
               try
               {
                   email = mail as Outlook.MailItem;
                   if (email != null)
                   {
                       email.Move(read);
                   }
               }
               catch (Exception ex)
               {
                   MessageBox.Show(ex.Message);
               }
           }
        }

        void Application_NewMail()
        {
            Outlook.MAPIFolder inboxfolder = this.Application.Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox);
            Outlook.Items items =(Outlook.Items)inboxfolder.Items;
            items.Restrict("[UnRead] = true");
            Outlook.MAPIFolder unread = inboxfolder.Folders["UnRead"];
            Outlook.MailItem email = null;
            foreach (object mail in items)
            {
                try
                {
                    email = mail as Outlook.MailItem;
                    if (email != null)
                    {
                        email.Move(unread);
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
        }


        private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
        {
        }

        #region VSTO generated code

        ///
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        ///

        private void InternalStartup()
        {
            this.Startup += new System.EventHandler(ThisAddIn_Startup);
            this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);
        }
       
        #endregion
    }
}

 From this Article we can learn how to create a basic Add-Ins for OutLook Application.