Add or Delete Contacts from Microsoft Exchange Server in Java

Microsoft Exchange Server is an email and calendaring server that provides different collaboration services like email, calendars, contacts management, etc. In one of my posts, I have shown you how to access and read emails from Microsoft Exchange Server programmatically in Java. However, we often need the contact list that we have on the MS Exchange Server. So in this article, you will learn how to add, delete, or update contacts on Microsoft Exchange Server in Java.

Java API to Access Contacts on MS Exchange Server

Aspose.Email for Java is a popular API to implement email client applications in Java. Moreover, it allows you to work with MS Exchange Server and manipulate the contacts, emails, and conversation items. We will use this API to add, update, and delete contacts on Exchange Server. You can either download the API’s JAR or install it using the following Maven configurations.

Repository:

<repository>
    <id>AsposeJavaAPI</id>
    <name>Aspose Java API</name>
    <url>http://repository.aspose.com/repo/</url>
</repository>

Dependency:

<dependency>
    <groupId>com.aspose</groupId>
    <artifactId>aspose-email</artifactId>
    <version>22.2</version>
    <classifier>jdk16</classifier>
</dependency>

Add Contacts to MS Exchange Server in Java

The following are the steps to add contacts to Microsoft Exchange Server in Java.

  • First, create and initialize NetworkCredential object with username, password, and domain.
  • Then, initialize IEWSClient with mailbox URI and NetworkCredential object.
  • Create an object of Contact class and set its properties such as name, job, gender, phone, associated persons, etc.
  • Finally, call EWSClient.createContact(Contact) method to add contact.

The following code sample shows how to add a contact to Microsoft Exchange Server in Java.

// Set mailbox URI, username, password, domain information
String mailboxUri = "https://ex2010/ews/exchange.asmx";
String username = "test.exchange";
String password = "pwd";
String domain = "ex2010.local";
NetworkCredential credentials = new NetworkCredential(username, password, domain);
IEWSClient client = EWSClient.getEWSClient(mailboxUri, credentials);
// Create New Contact
Contact contact = new Contact();
// Set general info
contact.setGender(Gender.Male);
contact.setDisplayName("Frank Lin");
contact.setCompanyName("ABC Co.");
contact.setJobTitle("Executive Manager");
PhoneNumber tmp0 = new PhoneNumber();
tmp0.setNumber("123456789");
tmp0.setCategory(PhoneNumberCategory.getHome());
// Add Phone numbers
contact.getPhoneNumbers().add(tmp0);
AssociatedPerson tmp1 = new AssociatedPerson();
tmp1.setName("Catherine");
tmp1.setCategory(AssociatedPersonCategory.getSpouse());
// Contact's associated persons
contact.getAssociatedPersons().add(tmp1);
AssociatedPerson tmp2 = new AssociatedPerson();
tmp2.setName("Bob");
tmp2.setCategory(AssociatedPersonCategory.getChild());
contact.getAssociatedPersons().add(tmp2);
AssociatedPerson tmp3 = new AssociatedPerson();
tmp3.setName("Merry");
tmp3.setCategory(AssociatedPersonCategory.getSister());
contact.getAssociatedPersons().add(tmp3);
Url tmp4 = new Url();
tmp4.setHref("www.blog.com");
tmp4.setCategory(UrlCategory.getBlog());
// URLs
contact.getUrls().add(tmp4);
Url tmp5 = new Url();
tmp5.setHref("www.homepage.com");
tmp5.setCategory(UrlCategory.getHomePage());
contact.getUrls().add(tmp5);
EmailAddress tmp6 = new EmailAddress();
tmp6.setAddress("Frank.Lin@Abc.com");
tmp6.setDisplayName("Frank Lin");
tmp6.setCategory(EmailAddressCategory.getEmail1());
// Set contact's email address
contact.getEmailAddresses().add(tmp6);
try {
// Create contact
client.createContact(contact);
} catch (java.lang.RuntimeException ex) {
System.out.println(ex.getMessage());
}

Delete Contacts from MS Exchange Server in Java

You can also delete a contact from the MS Exchange Server. To filter the contacts, you can use the name, email, or any other suitable property. The following are the steps to delete a contact from Microsoft Exchange Server in Java.

The following code sample shows how to delete contacts from Microsoft Exchange Server in Java.

// Initialize EWS client
IEWSClient client = EWSClient.getEWSClient("https://outlook.office365.com/ews/exchange.asmx", "testUser", "pwd", "domain");
// Contact name
String strContactToDelete = "John Teddy";
// Get contacts
Contact[] contacts = client.getContacts(client.getMailboxInfo().getContactsUri());
// Loop through contacts
for (Contact contact : contacts) {
if (contact.getDisplayName().equals(strContactToDelete))
// Delete contact
client.deleteItem(contact.getId().getEWSId(), DeletionOptions.getDeletePermanently());
}
client.dispose();

Update a Contact on Exchange Server in Java

Aspose.Email for Java also allows you to update a contact on MS Exchange Server. The following are the steps to perform this operation.

The following code sample shows how to update a contact on MS Exchange Server in Java.

// Set mailbox URI, username, password, domain information
String mailboxUri = "https://ex2010/ews/exchange.asmx";
String username = "test.exchange";
String password = "pwd";
String domain = "ex2010.local";
NetworkCredential credentials = new NetworkCredential(username, password, domain);
// Initialize EWS client
IEWSClient client = EWSClient.getEWSClient(mailboxUri, credentials);
// List all the contacts and Loop through all contacts
Contact[] contacts = client.getContacts(client.getMailboxInfo().getContactsUri());
Contact contact = contacts[0];
// Update contact details
System.out.println("Name: " + contact.getDisplayName());
contact.setDisplayName("David Ch");
client.updateContact(contact);

Get a Free API License

You can get a free temporary license to use Aspose.Email for Java without evaluation limitations.

Conclusion

In this article, you have learned how to work with contacts on Microsoft Exchange Server in Java. We have demonstrated how to add, remove, and update contacts from the MS Exchange Server in Java. Besides, you can explore the documentation to read more about Aspose.Email for Java. Also, you can post your queries on our forum.

See Also