Working with Contacts on Exchange Server using WebDav

Getting Contacts from an Exchange Server

The ExchangeClient class’ listContacts() method can be used to get contact information from an Exchange Server. listContacts() method requires the URI of the Contacts folder, which can be easily got with the ExchangeMailboxInfo.ContactsUri property.

To get contacts from an Exchange Server:

  1. Initialize the ExchangeClient class with address and credentials.
  2. Get the Contacts folder’s URI with the ExchangeClient.getMailboxInfo().getContactsUri() property.
  3. Call the listContacts() method. It returns an array of MapiContact.
  4. Do a foreach loop on the MapiContact array to read the contact information.

The following code snippet shows you how to use ExchangeClient class to read all contacts from an Exchange Server.

String mailboxURI = "http://ex2003/exchange/administrator"; // WebDAV
String username = "administrator";
String password = "pwd";
String domain = "domain.local";

// Credentials for connecting to Exchange Server
NetworkCredential credential = new NetworkCredential(username, password, domain);
ExchangeClient client = new ExchangeClient(mailboxURI, credential);

// List all the contacts
MapiContact[] contacts = client.listContacts(client.getMailboxInfo().getContactsUri());
for (MapiContact contact : contacts)
{
    // Display name and email address
    System.out.println("Name: " + contact.getNameInfo().getDisplayName() + ", Email Address: " + contact.getElectronicAddresses().getEmail1());
}