【问题标题】:How to get contacts from Global Address List using c#如何使用 c# 从全局地址列表中获取联系人
【发布时间】:2020-11-17 01:23:32
【问题描述】:

我正在尝试从 Outlook 全局地址列表中获取联系人列表(从我的公司电子邮件中,我认为是从 Exchange),但我有一个例外。我做了这段代码:

Outlook.Application OutlookApplication = new Outlook.Application();
Outlook.AddressLists addrLists = OutlookApplication.Session.AddressLists;
Outlook.AddressList gal = addrLists["Global Address List"];
foreach (Outlook.AddressEntry myentry in gal.AddressEntries)
{
    Outlook.ExchangeDistributionList listex = myentry.GetExchangeDistributionList();
    Outlook.AddressEntries entradas = listex.GetExchangeDistributionListMembers();
    for (int i = 0; i < entradas.Count; i++)
    {
        Outlook.AddressEntry exchDLMember = entradas[i];
        MessageBox.Show(exchDLMember.Name, "Info", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
    }
}

我遇到了这个异常,对象引用未建立为对象的实例,在以下行中:

Outlook.AddressEntries entradas = listex.GetExchangeDistributionListMembers();

提前致谢。

【问题讨论】:

    标签: c# outlook


    【解决方案1】:

    该错误意味着GetExchangeDistributionList() 方法返回null,而您永远不会检查它。这种行为是意料之中的,因为如果您正在处理 GAL 分发列表,它只会返回一个有效对象。

    对于非 DL GAL 条目,您需要使用GetExchangeUser 方法。您还必须始终检查您返回的对象是否有效,而不是 null。

    【讨论】:

    • 谢谢,但我如何获得contact.Email1Address、contact.Department、contact.FullName、contact.NickName。我无法在 Outlook.AddressEntry exchDLMember 中阅读此内容
    • ContactItem 是在位于“联系人”文件夹中的邮件(与 MAPI 的观点与普通邮件项目没有太大区别)之上创建的对象。 GAL 对象来自 Active Directory,不是来自您的联系人文件夹。使用 AddressEntry.PrimarySmtpAddress、Name 等
    • 谢谢,但是当我尝试在 Outlook.AddressEntry.PrimarySmtpAddress 中查找 PrimarySmtpAddress 时出现错误,类型名称 PrimarySmtpAddress 在类型 AddressEntry 中不存在。会是什么错误?
    • 对不起,是 ExchangeUser.PrimarySmtpAddress
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-11-26
    • 2018-11-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多