如何在Outlook 2016 c#中获取用户联系人列表

本文关键字:获取 用户 联系人 列表 Outlook 2016 | 更新日期: 2023-09-27 18:34:18

我正在尝试显示Outlook帐户的联系人列表。(展望2016) 以下代码显示全局联系人列表,但不显示您自己的个人联系人列表。如何显示帐户地址列表?这是我到目前为止的代码:

            try
            {    
                 Outlook._Application application = new Outlook.Application();
                 Outlook.AddressList addrList = null;
            foreach (Outlook.AddressList oAL in application.Session.AddressLists)
            {
                Outlook.MAPIFolder folder = oAL.GetContactsFolder();
            }
            Outlook.SelectNamesDialog dlg = application.Session.GetSelectNamesDialog();
            dlg.InitialAddressList = addrList;
            dlg.ShowOnlyInitialAddressList = true;
            dlg.NumberOfRecipientSelectors = Outlook.OlRecipientSelectors.olShowTo;
            dlg.Display();
            if (dlg.Recipients.Count > 0)
            {
                foreach (Outlook.Recipient recip in dlg.Recipients)
                {
                    Outlook.PropertyAccessor pa = recip.PropertyAccessor;
                    string smtpAddress = pa.GetProperty(PR_SMTP_ADDRESS).ToString();
                    AddrTextBox.Text += smtpAddress;
                    AddrTextBox.Text += "; ";
                }
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }

如何在Outlook 2016 c#中获取用户联系人列表

所以问题是你找不到要分配给SelectNamesDialog.InitialAddressList属性的正确AddressList对象?

您可以使用 AddressList.GetContactsFolderAddressList对象转到MAPIFolder对象,但不幸的是没有相应的MAPIFolder.GetAddressList方法(除非您使用 Redemption - 我是它的作者 - 它实现了 RDOFolder2.GetAddressList),所以你能做的最好的事情就是遍历Namespace.AddressLists集合中的所有地址列表,调用 AddressList.GetContactsFolder 。如果返回有效的MAPIFolder对象,请使用 Namespace.CompareEntryIDs 将其条目 ID ( MAPIFolder.EntryID ) 与默认联系人文件夹的条目 ID (Namespace.GetDefaultFolder(olFolderContacts) ) 进行比较。

之后,深入研究和测试。我已经找到了我自己问题的答案。如果要显示特定帐户的联系人列表,只需在第一个foreach语句中添加if语句:

 foreach (Outlook.AddressList oAL in m_AddInModule.OutlookApp.Session.AddressLists)
            {
                Outlook.MAPIFolder folder = oAL.GetContactsFolder();
                 if (folder.AddressBookName == m_AddInModule.ContactsFolder.AddressBookName)
                 {
                     addrList = oAL;
                     break;
                 }
            }         

如果您将其添加到我在最初的帖子中编写的代码中。您将成功在Outlook中查看当前帐户的联系人。我希望这会像我一样帮助你。