简体   繁体   English

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

[英]How can you get user contact list in Outlook 2016 c#?

I am trying to display the contact list of an outlook account. 我正在尝试显示一个Outlook帐户的联系人列表。 (Outlook 2016) The following code displays the global contact list but not your own personal contact list. (Outlook 2016)以下代码显示全局联系人列表,但不显示您自己的个人联系人列表。 How can i show the account address list? 如何显示帐户地址列表? This is code i have so far: 这是我到目前为止的代码:

            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);
        }

So the problem is that you cannot find the right AddressList object to assign to the SelectNamesDialog.InitialAddressList property? 因此,问题在于您找不到正确的AddressList对象来分配给SelectNamesDialog.InitialAddressList属性?

You can go from the AddressList object to the MAPIFolder object using AddressList.GetContactsFolder , but unfortunately there is no corresponding MAPIFolder.GetAddressLIst (unless you are using Redemption which implements RDOFolder2 .GetAddressList), so the best you can do is loop through all address lists in the Namespace.AddressLists collection, call AddressList.GetContactsFolder . 您可以使用AddressList.GetContactsFolder从AddressList对象转到MAPIFolder对象,但是不幸的是,没有对应的MAPIFolder.GetAddressLIst(除非您使用实现RDOFolder2 .GetAddressList的Redemption ),所以最好的办法是遍历所有地址列表在Namespace.AddressLists集合中,调用AddressList.GetContactsFolder If you get back a valid MAPIFolder object, compare its entry id ( MAPIFolder.EntryID ) with the entry id of the default Contacts folder ( Namespace.GetDefaultFolder(olFolderContacts) ) using Namespace.CompareEntryIDs . 如果返回有效的MAPIFolder对象,则使用Namespace.CompareEntryIDs将其条目ID( MAPIFolder.EntryID )与默认Contacts文件夹( Namespace.GetDefaultFolder(olFolderContacts) )的条目ID进行比较。

After, digging around researching and testing. 之后,深入研究和测试。 I have found the answer to my own question. 我找到了自己问题的答案。 If you want to display the contact list of the specific account all you have to do is add an if statement in the first foreach statement: 如果要显示特定帐户的联系人列表,只需在第一个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;
                 }
            }         

If you add that into the code I have written in my initial post. 如果您将其添加到我在第一篇文章中编写的代码中。 You will succeed with seeing the contacts of the current account in outlook. 您将可以在Outlook中看到当前帐户的联系人。 I hope this will help you as it did me. 希望对我有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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