如何从 ews 联系人获取电子邮件地址

how to obtain email address from ews contacts

我正在使用此代码从 outlook 获取联系人:

        foreach (var v in svc.FindItems(WellKnownFolderName.Contacts,
                                        new ItemView(20)))
        {
            Contact contact = v as Contact;
            ContactGroup contactGroup = v as ContactGroup;

            if (contact != null)
            {   
                 Console.WriteLine("Contact: {0} <{1}>",
                 contact.DisplayName,
                 contact.EmailAddresses[EmailAddressKey.EmailAddress1]);
            }
        }

我需要获取电子邮件方向 (xxx@xxx.xx),但我在电子邮件地址字段中得到了这个:

"/o=companyname/ou=FIRST ADMINISTRATIVE GROUP/cn=RECIPIENTS/cn=username"

您的 'email address' 采用 Exchange 专有名称的形式。它只是不是 SMTP 地址。

要获取 SMTP 电子邮件地址:

简单代码示例

NameResolutionCollection nd = service.ResolveName(contact.EmailAddresses[EmailAddressKey.EmailAddress1].Address);
       foreach (NameResolution nm in nd)
       {
           if (nm.Mailbox.RoutingType == "SMTP")
           {
               //Console.WriteLine(nm.Mailbox.Address);
               emailAddress1 = nm.Mailbox.Address;
           }
       }

Improved code

EmailAddress email;
if (contact.EmailAddresses.TryGetValue(EmailAddressKey.EmailAddress1, out email))
{
    person.Email = GetResolvedEmailAddress(email.Address, svc);
}

private static Dictionary<String, String> ResolvedEmailAddressCache = new Dictionary<String, String>();
    private static String GetResolvedEmailAddress(string address, ExchangeService svc)
    {
        if (ResolvedEmailAddressCache.ContainsKey(address))
            return ResolvedEmailAddressCache[address];

        NameResolutionCollection nd = svc.ResolveName(address);
        foreach (NameResolution nm in nd)
        {
            if (nm.Mailbox.RoutingType == "SMTP")
            {
                ResolvedEmailAddressCache.Add(address, nm.Mailbox.Address);
                return nm.Mailbox.Address;
            }
        }

        ResolvedEmailAddressCache.Add(address, address);
        return address;
    }

你也可以通过调用Load()方法来获取,这样效果似乎更好。 参见 https://social.msdn.microsoft.com/Forums/sqlserver/en-US/8760c6a5-09c6-438a-a70a-c4063645cc76/exchange-web-service-api-contact-email-address

调用 Load 为我修复了它。