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

问题描述:

我正在尝试显示Outlook帐户的联系人列表。 (Outlook 2016)以下代码显示全局联系人列表,但不显示您自己的个人联系人列表。我如何显示帐户地址列表?这是代码,我至今:如何在Outlook 2016 c#中获取用户联系人列表?

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

您可以尝试使用此:Outlook.MAPIFolder myContactsFolder = application.Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderContacts); – Mangist

+0

尝试看看这个链接底部发布的答案.http://*.com/questions/16884063/get-outlook-contacts-into-c-sharp-form-b​​ased-application还看看是否有一个在'MS LOOK OUT 2016'中需要特定的API' – MethodMan

+0

@MethodMan我会挑衅地告诉你一切,看看结果如何。谢谢 – Jim

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

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中当前帐户的联系人。我希望这会像你一样帮助你。

所以问题是,你不能找到合适的对象AddressList中分配给SelectNamesDialog.InitialAddressList财产?

可以使用AddressList.GetContactsFolder从AddressList中对象的MAPIFolder对象去,但遗憾的是没有相应的MAPIFolder.GetAddressLIst(除非您使用Redemption它实现RDOFolder2 .GetAddressList),所以你能做的最好的是遍历所有Namespace.AddressLists集合中的地址列表,请致电AddressList.GetContactsFolder。如果返回有效的MAPIFolder对象,请使用Namespace.CompareEntryIDs将其条目标识(MAPIFolder.EntryID)与默认联系人文件夹(Namespace.GetDefaultFolder(olFolderContacts))的条目标识进行比较。