Integrate Microsoft Email Webservice Service (EWS) using JAVA

In this post we will see how to integrate the Microsoft Email Webservice (EWS) with JAVA using the open source EWS-JAVA-API. 
Download the ews-java-api source code here. ews-java-api

I was not able to find the binaries for ews-java-api, thus I build the project using maven.

Below lib are need to run the JAVA program:
1. Binary for ews-java-api.
2. jcifs-1.3.17.jar
3. httpcore-4.3.3.jar
4. httpclient-4.3.6.jar
5. commons-logging-1.2.jar
6. commons-codec-1.6.jar
7. commons-cli-1.2.jar

Please find the simple program to send the email using MS-EWS. The program is explain with the comments inline.

The HTTPS i.e. SSL url is used, there is no need to include the certificate in credential store as the API internally validate the certs.

package com;
import java.net.URI;
import microsoft.exchange.webservices.data.EmailMessage;
import microsoft.exchange.webservices.data.ExchangeCredentials;
import microsoft.exchange.webservices.data.ExchangeService;
import microsoft.exchange.webservices.data.ExchangeVersion;
import microsoft.exchange.webservices.data.MessageBody;
import microsoft.exchange.webservices.data.WebCredentials;
public class Test {
public static void main(String[] args) throws Exception {
//Specify the version of the Microsoft Exchange Server
ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2007_SP1);
//Create the credentials by passing email and password
ExchangeCredentials credentials = new WebCredentials(
"test@test.com", "password");
service.setCredentials(credentials);
//Give the asmx URL instead of the WSDL
service.setUrl(new URI("https://hostname/ews/exchange.asmx"));
EmailMessage msg = new EmailMessage(service);
msg.setSubject("Hello Email");
msg.setBody(MessageBody.getMessageBodyFromText("Hello"));
msg.getToRecipients().add("Test@test.com");
msg.send();
}
}

Popular posts from this blog

JAVA embedding in Oracle SOA 12c

Passing/Receiving HTTP header in SOA BPEL

Integrating Weblogic with IBM MQ over JMS