Read Email From Mailbox Folders Using Microsoft Graph API

In this article, I have explained how to retrieve emails from mailbox folders from your Office 365 account using Microsoft Graph API endpoint.

In my previous articles, I have written about retrieving emails from folders and fetching access tokens and users/groups, etc. I have added the links below for your reference. Please go through these before following the steps given in this write-up.
Retrieve Emails From Mail Folders
  • EndPoint - https://graph.microsoft.com/v1.0/users/{{userid}}/mailFolders/{{Foldername}}/Messages
  • Usage - It retrieves all your emails from a selected folder from your mailbox
Tested in the Graph Explorer, it returns the JSON data like below. In this article, we are going to retrieve emails from the Inbox folder.
 
https://graph.microsoft.com/v1.0/users/{{userid}}/mailFolders/Inbox/messages
 
Read Email From Mailbox Folders Using Microsoft Graph API 
 Read Email From Mailbox Folders Using Microsoft Graph API
 
It returns all the properties of the mail like From, To, CC, Subject, Body, Conversation Id, Weblink, and a lot more.

So let's see how to retrieve this programmatically using JavaScript.

Please follow my previous article, How to fetch access token, to authenticate your web application to fetch the access token and authenticate.

Step 1 - Fetch Access Token

AuthUrl: https://login.microsoftonline.com/{{tenant}}/oauth2/v2.0/token

Type: POST
  1. var token; // Initialize Globally    
  2. function requestToken() {    
  3.     $.ajax({    
  4.         "async"true,    
  5.         "crossDomain"true,    
  6.         "url""https://howling-crypt-47129.herokuapp.com/https://login.microsoftonline.com/sharepointtechie.onmicrosoft.com/oauth2/v2.0/token", // Pass your tenant name instead of sharepointtechie    
  7.         "method""POST",    
  8.         "headers": {    
  9.             "content-type""application/x-www-form-urlencoded"    
  10.         },    
  11.         "data": {    
  12.             "grant_type""client_credentials",    
  13.             "client_id ""8baf0301-27df-44b1-b4fe-7911b9a918de"//Provide your app id    
  14.             "client_secret""tZ76oVPN039WlWPoAp+1aICq66vs7oUtE4lhDQYwxGY="//Provide your secret    
  15.             "scope ""https://graph.microsoft.com/.default"    
  16.         },    
  17.         success: function(response) {    
  18.             log(response);    
  19.             token = response.access_token; //Store the token into global variable    
  20.         },    
  21.         error: function(error) {    
  22.             log(JSON.stringify(error));    
  23.         }    
  24.     })    
  25. }    
 The successful response is below,

Read Email From Mailbox Folders Using Microsoft Graph API

 

So now create a function "RetrieveMessagesFromFolder", then pass the access token into the header.

EndPoint

https://graph.microsoft.com/v1.0/users/{{userid}}/mailFolders/{{Folderid}}/Messages

Method

GET

Code 

  1. function RetrieveMessagesFromFolder() {    
  2.     $.ajax({    
  3.         method: 'GET',    
  4.         url: "https://graph.microsoft.com/v1.0/users/vinodhvignesh@sharepointtechie.onmicrosoft.com/mailFolders/Inbox/Messages",    
  5.         headers: {    
  6.             'Authorization''Bearer ' + token,    
  7.             'Content-Type''application/json'    
  8.         },    
  9.     }).success(function(response) {    
  10.            
  11.         var data = response.value;  
  12.          console.log(data);    
  13.         //  data.map(function(folderinfo){  
  14.         //      $('#display').append('<li>'+ folderinfo.displayName+ '</li>');  
  15.         //  })  
  16.         
  17.           }).error(function(error) {});    
  18. }    
 HTTP "200", success  -- it returns the JSON data Below,
 
 Read Email From Mailbox Folders Using Microsoft Graph API

Then apply .map to iterate over the response and display it on the page

Full Code

  1. < script src = "https://ajax.aspnetcdn.com/ajax/jQuery/jquery-3.3.1.min.js" > < /script>   < script type = "text/javascript" >  
  2.     var token;  
  3. $(document).ready(function() {  
  4.     requestToken();  
  5. });  
  6.   
  7. function requestToken() {  
  8.     $.ajax({  
  9.         "async"true,  
  10.         "crossDomain"true,  
  11.         "url""https://howling-crypt-47129.herokuapp.com/https://login.microsoftonline.com/sharepointtechie.onmicrosoft.com/oauth2/v2.0/token", // Pass your tenant name instead of sharepointtechie      
  12.         "method""POST",  
  13.         "headers": {  
  14.             "content-type""application/x-www-form-urlencoded"  
  15.         },  
  16.         "data": {  
  17.             "grant_type""client_credentials",  
  18.             "client_id ""8baf0301-27df-44b1-b4fe-7911b9a918de"//Provide your app id      
  19.             "client_secret""tZ76oVPN039WlWPoAp+1aICq66vs7oUtE4lhDQYwxGY="//Provide your secret      
  20.             "scope ""https://graph.microsoft.com/.default"  
  21.         },  
  22.         success: function(response) {  
  23.             console.log(response);  
  24.             token = response.access_token;  
  25.             //     console.log(token);    
  26.             RetrieveMessagesFromFolder();  
  27.         },  
  28.         error: function(error) {  
  29.             console.log(JSON.stringify(error));  
  30.         }  
  31.     })  
  32. }  
  33.   
  34. function RetrieveMessagesFromFolder() {  
  35.     $.ajax({  
  36.         method: 'GET',  
  37.         url: "https://graph.microsoft.com/v1.0/users/vinodhvignesh@sharepointtechie.onmicrosoft.com/mailFolders/Inbox/Messages",  
  38.         headers: {  
  39.             'Authorization''Bearer ' + token,  
  40.             'Content-Type''application/json'  
  41.         },  
  42.     }).success(function(response) {  
  43.         var data = response.value;  
  44.         console.log(data);  
  45.         data.map(function(mailmessages) {  
  46.             $('#display').append('<li>From: ' + mailmessages.from.emailAddress.address + ', </br></br> Subject: ' + mailmessages.subject + ', </br></br> Body: ' + mailmessages.body.content + '</li>');  
  47.         })  
  48.     }).error(function(error) {});  
  49. }   
  50. < /script>     
  51. < ul id = "display" >   
  52. < /ul>  

The final result is on the screen below, I have bound the body content as HTML from the response

Read Email From Mailbox Folders Using Microsoft Graph API 
 
Read Email From Mailbox Folders Using Microsoft Graph API
 
So now, we are able to retrieve the mail from the folders using Microsoft Graph API. 
 
Here is the change that works well for system generated folders. If you need to retrieve the custom created folder messages from your mailbox, you need to pass the "mail folder id" instead of passing the generic folder name.
 
For an example, see below.
 
EndPoint: https://graph.microsoft.com/v1.0/users/{{userid}}/mailFolders/
 
Response

Read Email From Mailbox Folders Using Microsoft Graph API

 

EndPoint: https://graph.microsoft.com/v1.0/users/{{userid}}/mailFolders/{{Folderid}}/messages
 
https://graph.microsoft.com/v1.0/me/mailFolders/AAMkADJlNTkxOWYyLWUzMDQtNGQxOC05M2Y5LTU1MjQ0ZDIyMWI4YwAuAAAAAADwfY05XpcpSr7XjFASL6HoAQBwkKjIcfCzQY7kMM02uE45AAIPxmz5AAA=/messages 
 
Response
 
 Read Email From Mailbox Folders Using Microsoft Graph API
 
Now, you are able to understand how to retrieve emails from the mail folders using Microsoft Graph API.
 
Happy SharePointing!