Home > C# > C# Reading Outlook Contacts

C# Reading Outlook Contacts

In this post I am going to post a sample code to access outlook contact.

  • Add reference to “Microsoft.Office.Interop.Outlook”
  • Add using statement
using Microsoft.Office.Interop.Outlook;
  • Use following method
        public DataSet GetContacts()
        {
            DataSet ds = new DataSet();
            ds.Tables.Add("Contacts");
            ds.Tables[0].Columns.Add("Email");
            ds.Tables[0].Columns.Add("FirstName");
            ds.Tables[0].Columns.Add("LastName");

            Microsoft.Office.Interop.Outlook.Items OutlookItems;
            Microsoft.Office.Interop.Outlook.Application outlookObj;
            MAPIFolder Folder_Contacts;

            outlookObj = new Microsoft.Office.Interop.Outlook.Application();
            Folder_Contacts = 
(MAPIFolder)outlookObj.Session.GetDefaultFolder(
OlDefaultFolders.olFolderContacts);
            OutlookItems = Folder_Contacts.Items;

            for (int i = 0; i < OutlookItems.Count; i++)
            {
                Microsoft.Office.Interop.Outlook.ContactItem contact = 
(Microsoft.Office.Interop.Outlook.ContactItem)OutlookItems[i + 1];
                DataRow dr = ds.Tables[0].NewRow();
                dr[0] = contact.Email1Address;
                dr[1] = contact.FirstName;
                dr[2] = contact.LastName;

                ds.Tables[0].Rows.Add(dr);
            }

            return ds;
        }
Advertisements
Categories: C# Tags:
  1. Sujit
    March 19, 2013 at 2:15 PM

    Helping 1!!

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: