How to get Outlook contact groups using MAPI?

旧街凉风 提交于 2019-12-10 17:02:09

问题


In Outlook 2010, you can create contacts and add them to groups. Is there any way to get the list of such groups and the contacts in them? Here's how I access the contacts:

var outlook = new Outlook.Application().GetNamespace("MAPI");
var folder = outlook.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderContacts);
foreach (var curr in folder.Items.OfType<Outlook.ContactItem>())
{
    ...
}

I do not mean default contact folders, such as "Contacts" and "Suggested contacts".


回答1:


The contact groups are represented by DistListItem Interface. DistListItem interface has MemberCount property and GetMember() method to iterate through the group members.

var outlook = new Application().GetNamespace("MAPI");
var folder = outlook.GetDefaultFolder(OlDefaultFolders.olFolderContacts);
foreach (var curr in folder.Items.OfType<DistListItem>())
{
    Console.WriteLine(curr.DLName);

    for (int memberIdx = 1; memberIdx <= curr.MemberCount; memberIdx++)
    {
        var member = curr.GetMember(memberIdx);
        Console.WriteLine(member.Name);
    }
}



回答2:


You can try this one

            DataTable dt = new DataTable();
            dt.Columns.Add("FirstName");
            dt.Columns.Add("MiddleName");
            dt.Columns.Add("LastName");
            dt.Columns.Add("Email");

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

            foreach (var item in OutlookItems)
            {
                var contact = item as ContactItem;
                if (contact != null)
                {
                    DataRow dr = dt.NewRow();
                    dr["FirstName"] = contact.FirstName;
                    dr["MiddleName"] = contact.MiddleName;
                    dr["LastName"] = contact.LastName;
                    dr["Email"] = contact.Email1Address;
                    dt.Rows.Add(dr);
                }
            }


来源:https://stackoverflow.com/questions/12423425/how-to-get-outlook-contact-groups-using-mapi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!