Part I: Java -> MS Exchange Server

To assist my laziness I tried to write a utility that helps me check the available meeting rooms for booking faster than having to go to Outlook’s Calendar’s Scheduling Assistant.

Luckily, I found that my mail server has enabled Web Services, which should make my job much easier. The WSDL can be found at “https://my.exchange.com/EWS/exchange.asmx”. I tried to use Axis to generate the stubs, but hit an error.

{http://schemas.microsoft.com/exchange/services/2006/types}
ReminderMinutesBeforeStartType>null already exists

I searched and couldn’t find anything. Not wanting to dig into the implementation, I tried to switched to JAXWS. Not going well either.

At least one WSDL with at least one service definition needs to 
be provided.

This time I found the answer here. In fact I hit all the same problems as described even after solving this. I’ll repeat the solution here because I realize linking is good, but the sites might get torn down after some time.

Download the Services.wsdl, messages.xsd and types.xsd to your local disk. Edit Services.wsdl to add the service definition manually.

...
   <wsdl:service name="ExchangeServices">
     <wsdl:port name="ExchangeServicePort" binding="tns:ExchangeServiceBinding">
       <soap:address location="https://my.exchange.com/EWS/Exchange.asmx"/>
     </wsdl:port>
   </wsdl:service>
</wsdl:definitions>

With this added I generated the stubs. I used the “-Xnocompile” option to keep the .java files, so I copied them into Eclipse directly. This will be needed as we need to hack the source later.

I tried to call a simple API first – to resolve a name, or “Check Names”. How do I know what to call? Read the documentation.


ExchangeServices factory = new ExchangeServices();
ExchangeServicePortType service = factory.getExchangeServicePort();

// prepare request		
ResolveNamesType request = new ResolveNamesType();
request.setUnresolvedEntry("francis");

// prepare response
Holder<ResolveNamesResponseType> responseHolder = 
    new Holder<ResolveNamesResponseType>();

service.resolveNames(request, responseHolder);

ResolveNamesResponseMessageType response = 
    (ResolveNamesResponseMessageType) responseHolder.value.getResponseMessages()
    .getCreateItemResponseMessageOrDeleteItemResponseMessageOrGetItemResponseMessage()
    .get(0).getValue();
for (ResolutionType resolution: response.getResolutionSet().getResolution()) {
  System.out.printf("%s: %s", resolution.getMailbox().getName(), 
    resolution.getMailbox().getEmailAddress());
}

I hit the isNil problem also as described in the guide, so just comment out the fields that you are not using in ExchangeServicePortType such that you are left with the request and response.

This example is meant to demonstrate how to access Exchange with Java Web Services. If you use it you will likely need to handle errors, exceptions, nulls and the like.

Part II will show how to query the resource availability, and Part III shows how to display this results graphically using JFreeChart.

3 Comments »

  1. Nalin said,

    July 23, 2011 at 6:24 pm

    Hi,
    Very good information presented. It is very useful who ever wants to invoke MSExchange services.

    from last 3 days I was finding to get the WSDL for the MSExchange webservices.
    I read the link
    http://msdn.microsoft.com/en-us/library/aa580675(v=EXCHG.140).aspx
    and found some webservices availability.

    In this blog some lines of text is not wraped and we could see the complete line.
    if you can make it visible then it would be great.

    Thanks and Regards,
    Nalin.

  2. Nalin said,

    July 23, 2011 at 6:32 pm

    I need to add a functionality of creating meeting requests through java on a portlet.
    So I need your help. Please can you provide your contact details.
    My email:-nalinkumar.asawa@wipro.com

    Thanks you.
    -Nalin.

  3. geek said,

    February 23, 2012 at 2:30 pm

    I finally had that bit of time to make my code scrollable for anyone’s viewing pleasure (at least in FF10, IE9 and CR17).

    As a future tip for other sites that have this problem but the author is too lazy to fix it, you can either:
    1. Do a select on the affected lines and do a copy/paste into Notepad.
    2. View Source

RSS feed for comments on this post · TrackBack URL

Leave a Comment