简体   繁体   中英

Reading Outlook contact in C# using any Microsoft Outlook version

I have tried to read Microsoft Outlook contact using Microsoft Outlook 15.0 object library DLL, it works locally; when it comes to client, we do not know what version of Outlook is the client using. How to read if each client having different versions of Outlook?

I want to read the contact with any version of Microsoft Outlook Version using C#.

If you have any open source code, it helps a lot.

Please look at my code and help me where am doing wrong.

using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Runtime.InteropServices;
using MsOutlook = Microsoft.Office.Interop.Outlook;

namespace Test
{
    public class OutlookMailManager : IDisposable
    {

        public OutlookMailManager() { }


        /// <summary>
        /// Get MailContacts From Google (Gmail) using the provided username and password.
        /// </summary>
        /// <param name="maxEnries">Total number of entries to return</param>
        /// <returns>The addressbook entries</returns>
        public string GetOutlookMailContacts(int maxEnries)
        {
            MsOutlook.ApplicationClass OutlookApplication = new MsOutlook.ApplicationClass();
            MsOutlook.NameSpace outlookNameSpace = OutlookApplication.GetNamespace("MAPI");
            MsOutlook.MAPIFolder contactsCollection = outlookNameSpace.GetDefaultFolder(MsOutlook.OlDefaultFolders.olFolderContacts);
            Microsoft.Office.Interop.Outlook.Items folderItems = contactsCollection.Items;

            string rtnStr = "";
            if (folderItems.Count > 0)
            {
                for (int i = 1; folderItems.Count >= i; i++)
                {
                    object contactObj = folderItems[i];
                    if (contactObj is MsOutlook.ContactItem)
                    {
                        MsOutlook.ContactItem contact = (MsOutlook.ContactItem)contactObj;
                        rtnStr += contact.FullName + " (" + contact.BusinessTelephoneNumber + ")\n";
                    }
                    Marshal.ReleaseComObject(contactObj);
                    if (i == maxEnries) break;
                }
            }
            Marshal.ReleaseComObject(folderItems);
            Marshal.ReleaseComObject(contactsCollection);
            Marshal.ReleaseComObject(outlookNameSpace);

            return rtnStr;
        }
    }


}

You just need to use the PIAs corresponding the lowest Outlook version you need to support. Thus, you will be sure that only properties and methods existing in all Outlook versions are used. See C# app automates Outlook (CSAutomateOutlook) for the sample project.

Currently its working fine with my Outlook 2003 Version CheckOut this code but i havent tested with different outlook version.but

Add Microsoft.Office.Interop.Outlook dll in reference

    Microsoft.Office.Interop.Outlook.Items OutlookItems;
    Microsoft.Office.Interop.Outlook.Application outlookObj;
    MAPIFolder Folder_Contacts;
    private void Form1_Load(object sender, EventArgs e)
    {
        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];
      MessageBox.Show("FirstName:"contact.FirstName +" "+"LastName:"+contact.LastName +" "+"Emailid:"+contact.Email1Address);  
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM