Search code examples
microsoft-graph-api

Microsoft Graph API messages null


I am trying to access messages using Microsoft graph API using the SDK but the messages are null in MailFolderCollectionPage. I can list folders in the mailbox and get count of items. If I use REST API I am able to see messges as json response but with SDK I can't

Here is the code snippet on how I am trying to read messages. Getting nullpointer at f.messages.

    MailFolderCollectionRequestBuilder folders = graphClient.users("email@domain.com").mailFolders();
System.out.println("Printing Folders..");
        for (MailFolder f : mailFolders) {
            System.out.println("## Display Name:" + f.displayName + ", " + f.id);
            System.out.println("  ## Message Count:" + f.messages.getCount() );
            System.out.println("  ## Unread item count " + f.unreadItemCount);
            System.out.println("  ## Total item count " + f.totalItemCount);
        }

If I access using rest API I can see messages in response

okhttp3.Request listMessages = new okhttp3.Request.Builder()                .url("https://graph.microsoft.com/v1.0//users/email@domain.com/mailFolders/mVlLWE3N2UtNDAwNy05NmRkLTRhZDk4YTFjYWEAAAAEMAAA=/messages")
            .method("GET", null)

            .addHeader("Content-Type", "application/json")
            .addHeader("Authorization", "Bearer " + access_token))

            .build();

Solution

  • Then only request your making to the Graph is to ask for the mail Folders eg in

    MailFolderCollectionRequestBuilder folders = graphClient.users("email@domain.com").mailFolders()

    If you want to get the messages then you will need to include a request for the Messages collection of each folder you want to access and pass in the FolderId from your folder request something like eg

    graphClient.users("email@domain.com").mailFolders(f.id).messages()