none
Retrieving Contact Email Addresses with EWS RRS feed

  • Question

  • Hi,

    I have some code which retrieves contacts from a users Outlook.

    This retrieves a Contact object correctly associated with the outlook contacts Display Name however the `EmailAddressDictionary` attached to this object does not contain any of the e-mail addresses from the contact.

    I have tried updating the Contact in outlook, if I change the display name I see the change reflected when I run my code again but the e-mail dictionary always contains 0 entries despite the fact I have added them in outlook.

    Here is the code:

            private static void DisplayContacts(ExchangeService service)
            {
                // Get the number of items in the Contacts folder. To keep the response smaller, request only the TotalCount property.
                ContactsFolder contactsfolder = ContactsFolder.Bind(service,
                                                                    WellKnownFolderName.Contacts,
                                                                    new PropertySet(BasePropertySet.IdOnly, FolderSchema.TotalCount));

                // Set the number of items to the smaller of the number of items in the Contacts folder or 1000.
                int numItems = contactsfolder.TotalCount < 1000 ? contactsfolder.TotalCount : 1000;

                // Instantiate the item view with the number of items to retrieve from the Contacts folder.
                ItemView view = new ItemView(numItems);

                // To keep the request smaller, send only the display name.
                view.PropertySet = new PropertySet(BasePropertySet.IdOnly, ContactSchema.DisplayName);

                // Retrieve the items in the Contacts folder that have the properties you've selected.
                FindItemsResults<Item> contactItems = service.FindItems(WellKnownFolderName.Contacts, view);

                // Display the display name of all the contacts. (Note that there can be a large number of contacts in the Contacts folder.)
                foreach (Item item in contactItems)
                {
                    if (item is Contact)
                    {
                        Contact contact = item as Contact;
                        EmailAddress email;
                        Console.WriteLine(contact.DisplayName);
                        bool success = contact.EmailAddresses.TryGetValue(EmailAddressKey.EmailAddress1, out email);
                        if (!success)
                        {  
                             success = contact.EmailAddresses.TryGetValue(EmailAddressKey.EmailAddress2, out email); 
                             if (!success) 
     { 
         success = contact.EmailAddresses.TryGetValue(EmailAddressKey.EmailAddress1, out email);
     }
                        }
                    }
                }
            }



    • Edited by Alexabix Thursday, April 18, 2013 11:01 AM
    Thursday, April 18, 2013 11:01 AM

Answers

  • Hi,

    Not sure, but don't you need to add the EmailAddresses to the requested PropertySet?

    Moshe

    • Marked as answer by Alexabix Friday, April 19, 2013 8:21 AM
    Thursday, April 18, 2013 12:27 PM

All replies

  • Hi,

    Not sure, but don't you need to add the EmailAddresses to the requested PropertySet?

    Moshe

    • Marked as answer by Alexabix Friday, April 19, 2013 8:21 AM
    Thursday, April 18, 2013 12:27 PM
  • Ah, yeah that seems to be the issue. EmailAddresses throw an exception if you put them in the request but it seems to work for the individual emailaddress slots.

    It doesn't seem to return an address but it is populated at least:

    /o=Developement /ou=Exchange Administrative Group (FYDIBOHF23SPDLT) /cn=Recipients /cn=John Doe

    Friday, April 19, 2013 8:26 AM
  • That is the email address, in the form of an Exchange distinguished name. It's just not an SMTP address. The user alias is John Doe.

    --
    Ken Slovak
    [MVP-Outlook]
    http://www.slovaktech.com
    Author: Professional Programming Outlook 2007
    "Alexabix" <=?utf-8?B?QWxleGFiaXg=?=> wrote in message news:228af17a-49de-4db6-b332-ef17e24300c0...

    Ah, yeah that seems to be the issue. EmailAddresses throw an exception if you put them in the request but it seems to work for the individual emailaddress slots.

    It doesn't seem to return an address but it is populated at least:

    /o=Developement /ou=Exchange Administrative Group (FYDIBOHF23SPDLT) /cn=Recipients /cn=John Doe


    Ken Slovak MVP - Outlook
    Friday, April 19, 2013 4:05 PM
    Moderator
  • To get real SMTP email address. You can use this way. I just find out the solution.

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

    Hopely it's helpful.

    Brian Tsai

    Wednesday, August 7, 2013 10:24 AM