【问题标题】:Accessing Global Address Lists for multiple Outlook accounts访问多个 Outlook 帐户的全局地址列表
【发布时间】:2013-03-14 00:17:26
【问题描述】:

经过一个小时的搜索,在这里试试运气。

假设您的 Outlook 2010 有两个活动帐户:john.doe@company.com、admin.test@company.com。

您需要为 admin.test@company.com 拉取全局地址列表:

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

我们可以看到有两个名为“Global Address List”的条目,两个名为“Contacts”的条目等索引不同,但仍然不清楚哪个条目属于哪个帐户。

对于文件夹,它做得非常好,因为您可以使用这样的结构:

ns.Folders["admin.test@company.com"].Folders["Inbox"];

但我不知道 AddressLists 的类似机制。

任何帮助表示赞赏。

谢谢。

【问题讨论】:

  • 如果我可以从 ns.Accounts 中的条目中获取商店,这将有所帮助,因此我可以使用它与 AddressList 上的 PR_EMSMDB_SECTION_UID 匹配。帐户可以有多个商店,不是吗?
  • Dmitry 也回答了关于使用 C++/Delphi 的问题,他已经删除了它,但如果可能的话,我想了解如何在 C# .NET 上进行操作。
  • 好的,我没有删除我的答案 - 我不希望它看起来像 Redemption 的插件,因为 Richard 发布了一个使用 OOM 做同样事情的示例(我不知道)。跨度>

标签: c# outlook office-interop mapi


【解决方案1】:

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

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

还有 Dmitry,我要感谢您维护 Internet 上所有可用 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 = "admin.test@company.com";
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;
        }
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多