How to access the Outlook Contacts using C#.

No.of Views2396
Bookmarked2 times
Downloads 
Votes0
By  RRaveen   On  15 Feb 2010 22:02:47
Tag : Office Development , How to
How to access the Outlook Contacts using C#.
emailbookmarkadd commentsprint

Images in this article missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at info@codegain.com

 

Introduction

In 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

Implementation

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

Image Loading

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.

Image Loading

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.

Conclusion

In this article I have given way to access the contacts in outlook email.

Sample Project Source

Download source files -395 kb

 
Sign Up to vote for this article
 
About Author
 
RRaveen
Occupation-Software Engineer
Company-TGS
Member Type-Gold
Location-Singapore
Joined date-03 Jun 2009
Home Page-codegain.com
Blog Page-www.codegain.com
- B.Sc. degree in Computer Science. - 4+ years experience in Visual C#.net and VB.net - Obsessed in OOP style design and programming. - Designing and developing Network security tools. - Designing and developing a client/server application for sharing files among users in a way other than FTP protocol. - Designing and implementing GSM gateway applications and bulk messaging. - Windows Mobile and Symbian Programming - Having knowledge with ERP solutions
 
 
Other popularSectionarticles
Comments
By:RajDate Of Posted:3/27/2011 1:03:13 AM
Good
This helped me
Leave a Reply
Title:
Display Name:
Email:
(not display in page for the security purphase)
Website:
Message:
Please refresh your screen using Ctrl+F5
If you can't read this number refresh your screen
Please input the anti-spam code that you can read in the image.
^ Scroll to Top