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 readme.md 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.

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.lalit</groupId>
<artifactId>exchangeWebServiceintegration</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>exchangeWebServiceintegration</name>
<url>http://tech.lalitbhatt.net</url>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
<dependency>
<groupId>com.microsoft.office</groupId>
<artifactId>ews-java-api</artifactId>
<!-- Check the version from pom of ews-java-api -->
<version>1.3-SNAPSHOT</version>
</dependency>
</dependencies>
</project>

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 = "credentials.properties";
Reader reader = new BufferedReader(new InputStreamReader(
EWSIntegrationMain.class.
                   getClassLoader().getResourceAsStream(
fileName)));

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

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

ExchangeService service = new ExchangeService(
ExchangeVersion.Exchange2010_SP2);

ExchangeCredentials credentials = new WebCredentials(
email, password);
service.setCredentials(credentials);

service.setUrl(new URI(ewsUrl));

//Let's send a message
EmailMessage msg= new EmailMessage(service);
msg.setSubject("Message using EWS API"); 
msg.setBody(MessageBody.getMessageBodyFromText
                   ("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
msg.send();
}

}

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