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

时间:2015-03-03 18:15:33

标签: c# contacts exchangewebservices

我正在使用此代码从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”

2 个答案:

答案 0 :(得分:4)

您的电子邮件地址&#39;是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;
    }

答案 1 :(得分:0)

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

Calling Load为我修好了。