编程语言
首页 > 编程语言> > C#-如何从ews联系人获取电子邮件地址

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

作者:互联网

即时通讯使用此代码从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”

解决方法:

您的“电子邮件地址”采用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;
    }

标签:exchangewebservices,contacts,c
来源: https://codeday.me/bug/20191028/1953680.html