Tuesday, July 1, 2008

Read Outlook Contact Details Using C#

First Make reference to Outlook Object Library in your project, which you can do from

Solution Explorer -> Reference : Right Click -> Add Reference -> COM Tab -> Scroll to Microsoft Outlook Object Library x.x (x.x depends on the version of Office installed)


Here is the Code :

using OutLook = Microsoft.Office.Interop.Outlook;

OutLook.Items contactItems = null;
try
{
OutLook.Application outapp = new Microsoft.Office.Interop.Outlook.ApplicationClass();
OutLook.NameSpace outnamespace = outapp.GetNamespace("MAPI");
OutLook.MAPIFolder contactsFolder = outnamespace.GetDefaultFolder (OutLook.OlDefaultFolders.olFolderContacts);
contactItems = contactsFolder.Items;
owner = outnamespace.CurrentUser.AddressEntry.Name;
}
catch
{
MessageBox.Show("Either Outlook is not available or Outlook has reject your request for connection.", "Outlook Search", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
return;
}

contactItems contains the collection of the Contact which you can iterate though it using Foreach as
foreach (OutLook.ContactItem items in contactItems)
{
//.......
//Some Code
//.......
}