访问多个Outlook帐户的全局地址列表

问题描述:

在搜索一小时后尝试我的运气。访问多个Outlook帐户的全局地址列表

假设您拥有两个活动帐户的Outlook 2010:[email protected][email protected]

你需要拉全局地址列表的[email protected]

  using Microsoft.Office.Interop.Outlook; 

      Application app = new Application(); 
      NameSpace ns = app.GetNamespace("MAPI"); 
      ns.Logon("", "", false, true); 

      AddressList GAL = ns.AddressLists["Global Address List"]; 

      foreach (AddressEntry oEntry in GAL.AddressEntries) 
      { 
       // do something 
      } 

这里的问题是,GAL可以属于任何一个帐户,这不是明显的,至少通过阅读MSDN,如何你想知道你真的想使用哪个账户。

如果我们将经历这样,所有列表:

foreach (AddressList lst in ns.AddressLists) 
{ 
    Console.WriteLine("{0}, {1}", lst.Name, lst.Index); 
} 

我们可以看到,有两个项目名为“全球地址列表”,命名为“联系人”等不同的指标两个条目,但它的仍不清楚哪一个属于哪个帐户。

对于文件夹,它的完成相当不错,因为你可以使用结构类似:

ns.Folders["[email protected]"].Folders["Inbox"]; 

,但我无法找出类似的机制AddressLists。

任何帮助表示赞赏。

谢谢。

+0

请问这个例子帮助:http://msdn.microsoft.com/en-us/library/ff184631.aspx – 2013-03-12 20:00:04

+0

它可以帮助如果我能在纳秒项目获取商店.Accounts,所以我可以使用它来匹配AddressList上的PR_EMSMDB_SECTION_UID。帐户可以有多个商店,是不是? – ilgor 2013-03-12 21:02:13

+0

Dmitry也有关于使用C++/Delphi的答案,他已经删除了它,但是我想知道如何在C#.NET上做到这一点,如果可能的话。 – ilgor 2013-03-12 21:03:24

我使用Account.CurrentUser UID和匹配的AddressList UID来选择正确的列表。 我不知道如果使用Store是一种更好的方法,但是这个工作很好。

理查德和德米特里感谢您的帮助。

另外德米特里我想感谢您维护互联网上可用的所有MAPI标签的唯一来源。

代码:

using Microsoft.Office.Interop.Outlook; 

const string PR_EMSMDB_SECTION_UID = "http://schemas.microsoft.com/mapi/proptag/0x3D150102"; 

Application app = new Application(); 
NameSpace ns = app.GetNamespace("MAPI"); 
ns.Logon("", "", false, true); 

string accountName = "[email protected]"; 
string accountUID = null; 

// Get UID for specified account name 
foreach (Account acc in ns.Accounts) 
{ 
    if (String.Compare(acc.DisplayName, accountName, true) == 0) 
    { 
     PropertyAccessor oPAUser = acc.CurrentUser.PropertyAccessor; 
     accountUID = oPAUser.BinaryToString(oPAUser.GetProperty(PR_EMSMDB_SECTION_UID)); 
     break; 
    } 
} 

// Select GAL with matched UID 
foreach (AddressList GAL in ns.AddressLists) 
{ 
    if (GAL.Name == "Global Address List") 
    { 
     PropertyAccessor oPAAddrList = GAL.PropertyAccessor; 
     if (accountUID == oPAAddrList.BinaryToString(oPAAddrList.GetProperty(PR_EMSMDB_SECTION_UID))) 
     { 
      foreach (AddressEntry oEntry in GAL.AddressEntries) 
      { 
       // do something 
      } 
      break; 
     } 
    } 
} 

来自不同服务器的GAL需要使用配置文件(IProfAdmin)和账户管理API(IOlkAccountManager)与相应的商店和账户相关联。这些接口只能用C++或Delphi访问。 您将需要从两个商店(IMsgSTore)和地址簿对象(IABContainer)中读取PR_EMSMDB_SECTION_UID。如果您需要将其与一个帐户进行匹配,那么在IOlkAccount对象的PROP_MAPI_EMSMDB_SECTION_UID(0x20070102)属性中可以使用相同的值 - 如果您单击IOlkAccountManager按钮并双击一个Exchange帐户,则可以在OutlookSpy中看到它。

如果使用Redemption是一个选项,你可以使用RDOExchangeAccount对象,它公开GAL,AllAddressLists,PrimaryStore,PublicFolders等特性。