Atmail Mashup Tutorial - Using Java to list Mail, Contacts and Events
August 30, 2011
The Atmail API allows you to access and process data using your favorite language. For this tutorial we are going to use Java to query the Atmail API and process the JSON response to view mail, global contacts and calendar events from a webmail account.
This tutorial assumes that you already have one of the latest Java SDKs and Eclipse for Java Developers installed on your computer.
Download the source-code at: http://atmail.com/downloads/mashups/javaapi.tgz
Step-1: Download and extract the file onto your computer.
Step-2: Import the code into Eclipse.
Step-3: Replace the values in WebmailAPI.java to reflect the webmail account you wish to retrieve the data from.
Step-4: Run the code and the data will appear in the console.
How does it work?
The above mashup is a simple example which uses the Atmail API to download messages, contacts and calendar events in JSON format and displays them in a more readable format. A walk-through of the source-code is below:
//Authenticate & Get session ID
//Set the url based on the basic variables
String urlString = domain + “index.php/mail/auth/processlogin?jsoncallback=true” + “&email=” + username + “&emailDomain=” + emailDomain + “&emailName=” + emailName + “&requestedServer=” + requestedServer + “&password=” + password;
//Get data
String authenticationData = getDataFromUrlString(urlString);
//Decode the results
JSONObject jsonResult = null;
String sessionID = null;
try {
jsonResult = new JSONObject(authenticationData);
sessionID = (String) jsonResult.get(”SessionID”);
System.out.println(”Session ID: ” + sessionID);
} catch (JSONException e) {
System.out.println(”Error: ” + e.getMessage());
}
This part of the Java code uses the provided information to get a new Session ID and prints it out.
//List mail
System.out.println(”\nMail:”);
urlString = domain + “index.php/api/mail/list/?SessionID=” + sessionID;
//Get data
String messageData = getDataFromUrlString(urlString);
//System.out.println(”Message Result: ” + messageData);
…
System.out.println(”Error: ” + e.getMessage());
}
This gets the mail data in JSON format, parses it and displays it.
//Query global address book- creating and displaying an array of up to the first 10 contacts
System.out.println(”\nGlobal Contacts:”);
//Set the url based on the domain name, action (listing contacts that are in the global group), session ID
//and which fields to return
urlString = domain + “index.php/api/contact/list/GroupID/-1/?SessionID=” + sessionID + “&Fields[]=UserFirstName&Fields[]=UserEmail&Fields[]=Favourite”;
//Get data
String contactData = getDataFromUrlString(urlString);
…
else{
System.out.println(” ” + eventInformationFields[j] + “: ” + eventInformation.get(eventInformationFields[j]));
}
This does the same for global contacts and calendar events respectively.
Below are some ideas which could be incorporated for a real-world use of this mashup:
- A user could create a backup of their messages, contacts and calendar events.
- A program could be created that creates invitations using specific fields from the contacts.
- A backup utility could be written in Java, to export a users mail/calendar/contact data to an external provider
- A mashup could be created to pull users upcoming calendar events into a portal written in Java
The mashup API opens the gateways for possible extensions to Atmail - We welcome your feedback and mashup submissions to include on our site.

Atmail RSS Feed