Showing posts with label VS Add-Ins. Show all posts
Showing posts with label VS Add-Ins. Show all posts

Sunday, July 21, 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(objectsender, System.EventArgs e)
        {
            Outlook.MAPIFolderinboxfolder = this.Application.Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox);            
            varinFolder = this.Application.Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox);
            Outlook.MAPIFolderofolread = null;
            Outlook.MAPIFolderofolunread = null;
            boolunreadexists = false;
            boolreadexists = 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.

  voidItems_ItemChange(object Item)
        {
            Outlook.MAPIFolderinboxfolder = this.Application.Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox);
            Outlook.MAPIFolderunread = inboxfolder.Folders["UnRead"];
            Outlook.MAPIFolderread = inboxfolder.Folders["Read"];
            Outlook.MailItememail = null;
           foreach(object mail inunread.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

   voidApplication_NewMail()
        {
            Outlook.MAPIFolderinboxfolder = this.Application.Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox);
            Outlook.Itemsitems =(Outlook.Items)inboxfolder.Items;
            items.Restrict("[UnRead] = true");
            Outlook.MAPIFolderunread = inboxfolder.Folders["UnRead"];
            Outlook.MailItememail = null;
            foreach(object mail initems)
            {
                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
    {
        privatevoid ThisAddIn_Startup(objectsender, System.EventArgs e)
        {
            Outlook.MAPIFolderinboxfolder = this.Application.Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox);            
            varinFolder = this.Application.Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox);
            Outlook.MAPIFolderofolread = null;
            Outlook.MAPIFolderofolunread = null;
            boolunreadexists = false;
            boolreadexists = 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);                    
           
        }

        voidItems_ItemChange(object Item)
        {
            Outlook.MAPIFolderinboxfolder = this.Application.Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox);
            Outlook.MAPIFolderunread = inboxfolder.Folders["UnRead"];
            Outlook.MAPIFolderread = inboxfolder.Folders["Read"];
            Outlook.MailItememail = null;
           foreach(object mail inunread.Items.Restrict("[UnRead] = false"))
           {
               try
               {
                   email = mail as Outlook.MailItem;
                   if(email != null)
                   {
                       email.Move(read);
                   }
               }
               catch(Exception ex)
               {
                   MessageBox.Show(ex.Message);
               }
           }
        }

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


        private voidThisAddIn_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.
        ///

        privatevoid 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.