Monday, September 8, 2014

Exchange server connectivity in Java using ESW API

EWS API help is building connectivity to Java. EWS Java API has been open sourced and now exist in GitHub at this location. We will look into a simple program of how to use the API and send a message. For extensive documentation for the API, see the file at GitHub location. The reamed file is displayed on the browser itself if you browse to the GitHub Location. Also please look into the license terms also here and make sure it's valid for your use case scenario. 

Install the Java EWS API jar locally

First you will need to pull the source code from GitHub locally and build it using mvn install. You
can either pull it to a local git repository or download it as a zip. Once you unzip it, do 'mvn install' to build the ews-java-api artifact. (I could not figure out a public repository for that). 

Project to use the ews-java-api

Let's make a simple Maven based Java project to see how we can use the api. The readme file mentioned above has details of many use cases. Also be comfortable with the concept of Items and Folders.


<project xmlns="" xmlns:xsi=""




<!-- Check the version from pom of ews-java-api -->

Main Code

class EWSIntegrationMain {

public static void main(String[] args) throws Exception {

// Read the properties file in a reader
//Make sure you have a properties file at the class path
//having three properties
//email: Email to be used to connect to Exchange server
//password: Password of the email
//url: URL to your exchange server. Read about Autodiscovery 
//     also in Readme file of ews-java-api GitHub
String fileName = "";
Reader reader = new BufferedReader(new InputStreamReader(

// Instantiate a properties object and load the reader
Properties properties = new Properties();

String email = properties.getProperty("email");
String password = properties.getProperty("password");
String ewsUrl = properties.getProperty("url");

ExchangeService service = new ExchangeService(

ExchangeCredentials credentials = new WebCredentials(
email, password);

service.setUrl(new URI(ewsUrl));

//Let's send a message
EmailMessage msg= new EmailMessage(service);
msg.setSubject("Message using EWS API"); 
                   ("Hope you are going to recieve this message."
+ "If you get it than take the Ice Bucket Challenge"));

//You can add multiple recipients
msg.getToRecipients().add("<Recipient mail>");

//Send the message


Caution: The API is buggy at the moment for certain use cases as can be seen in the link here and open issues.

No comments:

Post a Comment