Tag Archives: Java

How to get mails from OFFICE365 Server in Java?

We are using REST API of OFFICE 365 to get mails/messages from mail server. This REST API includes following operations that we can perform on OFFICE 365 Mailbox:

  1. Get Messages:

It includes two types:

  • Get a message collection
  • Get a single message
  1. Create and send messages:
  • Send a new message
  • Create a draft message
  • Send a draft message
  1. Reply to messages:
  • Reply to a single message
  • Reply to all
  1. Forward message:
  • Forward a message directly
  • Create a draft of forward message
  1. Delete Messages
  2. Get attachments
  • Get collection of attachment
  • Get a single attachment

These above mentioned REST API are mentioned here.

Now if we take an example of ‘Get Messages from Mailbox’, we will need a username and password of the mailbox. Through these security credentials we need to connect to the server and fetch the mails. It’s seems a lot of work of connections, basic authentications and then getting mails. Any ways we need to follow these steps, but to save our time, we are going to use a Unirest.

One can get all the information related to Unirest here.

It is nothing but a set of lightweight HTTP libraries available in multiple languages. I’ve used it just to simplify HTTP requests as it can be used to make all GET, POST, PUT, PATCH, DELETE, HEAD, OPTIONS requests. Also it does non-blocking synchronous and asynchronous requests.  It supports form parameters, file uploads and custom body entities. Easily add route parameters without ugly string concatenations. Supports Basic Authentication natively. Customizable default headers for every request (DRY). Customizable HttpClient and HttpAsyncClient implementation. Automatic JSON parsing into a native object for JSON responses.

So as to get started let’s consider the first operation, that is to ‘Get messages’.

Get Messages

You can get a message collection or an individual message from a mailbox folder.

Get collection of messages

Required scope: Mail.Write or Mail.Read

To get messages from ‘inbox’ folder,

Req: GET https://outlook.office365.com/api/v1.0/me/messages

To get messages from different mailbox folder,

Req: GET https://outlook.office365.com/api/v1.0/me/folders/{folder_id}/messages 

{Folder_id} is a String type parameter which can be Inbox, Drafts, SentItems or DeletedItems.

Sample Req:

 GET https://outlook.office365.com/api/v1.0/me/folders/sentitems/messages

Now we are going to implement this in a Java program with the help of Unirest.

public class ConnectToOffice365REST{


* This methods accepts a username and password of the Office 365   * mailbox and send API request to Office 365 Server. Which returns * json object with list of unread mails.

* @param user

* @param password

* @return JSON object in string format.


 public String getMessagesFromOffice(String user, String password){

                     JsonNode jsonObject = null;

                     HttpResponse<JsonNode> response;

                     String returnString = “”;

                     try {

                           // It send request to get list of unread mails from                                             // inbox folder of mailbox and does basic                                                               // authentication with provided username and password

                           response =                                                    Unirest.get(“https://outlook.office365.com/api/v1.0/me/messages?$filter=” +         java.net.URLEncoder.encode(“IsRead eq false”, “UTF-    8″)).basicAuth(user,password).asJson();

                           System.out.println(“response : ” +response.getBody());

                           jsonObject = response.getBody();

                           if(response.getBody() == null){

                                  returnString = “NULL”;



                                  returnString = jsonObject.toString();


                     } catch (UnsupportedEncodingException e) {

                           // TODO Auto-generated catch block


                     } catch (UnirestException e) {

                           // TODO Auto-generated catch block



                     return returnString;




ProsperaSoft offers Java development solutions. You can email at info@prosperasoft.com to get in touch with ProsperaSoft Grails experts and consultants.