IntroductionIn this article, I’m going to give detail about access outlook contacts using C#. Most of the users asking question how to access outlook contacts in .net. Before play with code we need understand the structure of the outlook folders. Download source files -395 kb Outlook Folders The outlook has common folder object call "MAPI"( it can be say namespace also). So when you are going to access contacts or email, any other content. we need call first MAPI. then only we can access the contact folder.More detail about outlook folder structure here. ImplementationStep 1- Start Microsoft Visual Studio 2008. Step 2- On the File menu, point to New, and then click Project. Step 3- Click Visual C# Projects under Project Types, and then click Console Application under Templates , give name as "ReadContactInOL". look at the following figure. Step 4- Add a reference to the Microsoft Outlook 12.0 Object Library. To do this, On the Project menu, click Add Reference.Click the .NET tab. and then Click Microsoft Outlook 12.0 Object Library, and then click Select, look at the following figure. Code Now we need write code for access outlook namespace top of the class file. using Outlook = Microsoft.Office.Interop.Outlook; Now write code in your class file . using System;
using System.Reflection;
using Outlook = Microsoft.Office.Interop.Outlook;
namespace ReadContactInOL
{class Program
{static void Main(string[] args)
{
Outlook.Application oApp = new Outlook.Application();
Outlook.NameSpace oNS = oApp.GetNamespace("mapi");
oNS.Logon("Outlook", Missing.Value, false, true);
Outlook.MAPIFolder contacts = oNS.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderContacts);
Outlook.Items oItems = contacts.Items;try{for (int i = 0; i < oItems.Count; i++)
{
Outlook.ContactItem contact = (Outlook.ContactItem)oItems[i];
Console.WriteLine(contact.Email1Address);
}
Console.ReadKey();
}catch (Exception ex)
{throw ex;
}finally{
oItems = null;
oNS.Logoff();
oNS = null;
oApp = null;
}
}
}
}How code Work Before access the folder we need create root namespace with MAPI which from the Outlook Application. Then we need set logon information to root namespace. When we take look at the folder structure in outlook, it looks like, using System;
namespace Microsoft.Office.Interop.Outlook
{// Summary:// Specifies the folder type for the current Outlook profile.public enum OlDefaultFolders
{// Summary:// The Deleted Items folder.olFolderDeletedItems = 3,//// Summary:// The Outbox folder.olFolderOutbox = 4,//// Summary:// The Sent Mail folder.olFolderSentMail = 5,//// Summary:// The Inbox folder.olFolderInbox = 6,//// Summary:// The Calendar folder.olFolderCalendar = 9,//// Summary:// The Contacts folder.olFolderContacts = 10,//// Summary:// The Journal folder.olFolderJournal = 11,//// Summary:// The Notes folder.olFolderNotes = 12,//// Summary:// The Tasks folder.olFolderTasks = 13,//// Summary:// The Drafts folder.olFolderDrafts = 16,//// Summary:// The All Public Folders folder in the Exchange Public Folders store. Only// available for an Exchange account.olPublicFoldersAllPublicFolders = 18,//// Summary:// The Conflicts folder (subfolder of Sync Issues folder). Only available for// an Exchange account.olFolderConflicts = 19,//// Summary:// The Sync Issues folder. Only available for an Exchange account.olFolderSyncIssues = 20,//// Summary:// The Local Failures folder (subfolder of Sync Issues folder). Only available// for an Exchange account.olFolderLocalFailures = 21,//// Summary:// The Server Failures folder (subfolder of Sync Issues folder). Only available// for an Exchange account.olFolderServerFailures = 22,//// Summary:// The Junk E-Mail folder.olFolderJunk = 23,//// Summary:// The RSS Feeds folder.olFolderRssFeeds = 25,//// Summary:// The To Do folder.olFolderToDo = 28,//// Summary:// The top-level folder in the Managed Folders group. For more information on// Managed Folders, see Help in Outlook 2007. Only available for an Exchange// account.olFolderManagedEmail = 29,
}
}
Since the list of the folder, I had access only contact MAPI folder. Within the folder all the contacts are present like item, since from I’m going to access every contact email address using foreach loop. In this way you can access other folders also. That’s all. ConclusionIn this article I have given way to access the contacts in outlook email. Sample Project SourceDownload source files -395 kb |