How To Send Email Using Microsoft Graph API

Author: Prathamesh P. Kulkarni

In this article, we are going to see how to send emails using Microsoft Graph API. Usually, we send emails with the help of an Email connector and for that, we need a username and password. To avoid this Microsoft Graph API has a provision to send emails with the help of API.

What is Microsoft Graph API? 

Microsoft Graph is a RESTful web API that enables you to access Microsoft Cloud service resources. After you register your app and get authentication tokens for a user or service, you can make requests to the Microsoft Graph API. 

To use this service you will need an account with Azure and the application. 

  • How to do the setup in Azure. 
    • Create an Azure AD app with Microsoft Graph permissions.
    • Ensuring the account is licensed to send e-mails.
    • Build the code.

To allow our applications to send e-mails as a given user or service account, we need to configure an Azure AD application with the appropriate permission. Additionally, we need to ensure that the user or service account has a license assigned for sending e-mails.

The process of configuring our Azure AD apps and users is very simple and easy.

Create an Azure Active Directory Application 
  • Azure Portal > Active Directory > App registrations > New registration
  • Name: Whatever you want.
  • Type: Accounts in this organizational directory only (Single-tenant)
  • Redirect URI: Not required.

The Microsoft Graph can support both the traditional ClientID + ClientSecret approach, as well as using the Managed Identity approach. Here I am using ClientID + ClientSecret approach\

We then need to create a new secret and securely store the value of the said secret, along with the Tenant ID and the app’s Client ID.

Click on this highlighted application number, you can create a new application there.

I have already created a test application for that. 

  • Certificates & Secrets > New client secret
  • Copy the secret and store it in a safe location so you can use it again.

Next, make sure you copy the Application ID (Client ID) and the Tenant ID for your application and place them in a safe location as you need them in the future. You can find these on the Overview page of your app which you can see in the first image. 

Set up Permissions

 Now is the time to set up permissions 

 From the app page in the Azure Portal:

API permissions > Add a permission

Microsoft Graph > Application Permissions > Mail.Send > click Add Permission

Use delegated permissions if you want a user to consent to the app explicitly, and allow that one user to send e-mails from the application.

Use application permissions if you want to allow sending e-mails as any user in the organization. 

select Mail.send as shown in the below image

Your configured permissions should look something like this

Great, we’re done with the preparations. At this point, we should have:

  • New Azure AD application.
  • Configured the appropriate permissions for sending e-mails.
  • Ensured there was a license assigned to the user account.

With that, we’re ready to head over to the next part. Building the application in mule 4.

Building the application

we would need two requestors over here. 

1. To fetch token with the help of client credentials

2. To send an email with the help of fetched token. 

This is the overall structure of the application

As I said, the First step is to fetch the token. 

the necessary format is given below also it should be in x-www-form-urlencoded

In the next step, you will need your Tenant ID to request the token. 

The Tenant ID is part of the URL which is masked. 

Once you get the token store it in a variable because we will need it in future

Now it’s time to create a message body with an attachment, to send the attachment you need to convert your payload into base64 format. 

This is the required payload for to send email

cc recipients are optional, attachment is also optional, you can send any type of message in the body by giving content type also in the attachment in which format you want output that also you can specify in MIME-TYPE like I mentioned application/json

you can have multiple addresses as well which will be an array of email-address objects. 

Now, configure the Requestor to sendEmail

The URL used is POST  and send Bearer Token in the headers section. 

Run Application

 It’s time to run the application and test it. 

The application ran successfully, time to check inbox,

Tadddaa… we got the email

Let’s open the inbox and see what we got there

We got the email body with an attachment. 

Pasting code for reference.


<?xml version=”1.0″ encoding=”UTF-8″?>

<mule xmlns:email=”” xmlns:ee=””


xmlns=”” xmlns:doc=”” xmlns:xsi=”” xsi:schemaLocation=””>

<http:listener-config name=”HTTP_Listener_config” doc:name=”HTTP Listener config” doc:id=”12efcb11-2560-475d-9c43-f1121237d700″ >

<http:listener-connection host=”″ port=”8081″ />


<http:request-config name=”HTTP_Request_configuration_Send_Email” doc:name=”HTTP Request configuration” doc:id=”b79ac069-6cfa-4e0d-bd05-d8d7f39745e1″ >

<http:request-connection host=”” protocol=”HTTPS”/>


<http:request-config name=”HTTP_Request_configuration_Token” doc:name=”HTTP Request configuration” doc:id=”0809b1a9-73b7-4c16-904e-83b502df0687″ >

<http:request-connection protocol=”HTTPS” host=”” />


<flow name=”send-email-graph-api-code” doc:id=”bd3b54f7-a9bd-4fc6-91d9-353177cb942b” >

<http:listener doc:name=”Listener” doc:id=”10800bec-5217-4b31-a461-9c3e48e6bd43″ config-ref=”HTTP_Listener_config” path=”/testEmail”/>

<ee:transform doc:name=”Set Client Cred to fetch token” doc:id=”732271f2-0ad8-4da9-a0c8-6fe736b45c18″ >

<ee:message >

<ee:set-payload ><![CDATA[%dw 2.0

output application/x-www-form-urlencoded


“client_secret” : “app_client_secret”,

“scope” : “”,

“grant_type” : “client_credentials”,

“client_id” : “app_client_id”




<http:request method=”POST” doc:name=”Request To Fetch Token” doc:id=”14b64477-75d0-4094-988e-55c35dfa749d” config-ref=”HTTP_Request_configuration_Token” path=”/{Tenant_ID}/oauth2/v2.0/token” target=”bearerToken”>

<http:headers ><![CDATA[#[output application/java


“Content-Type” : “application/x-www-form-urlencoded”



<ee:transform doc:name=”Create attachement payload” doc:id=”b068276c-2598-4a70-99e8-d3d731c59c84″ >

<ee:message >


<ee:variables >

<ee:set-variable variableName=”attachment” ><![CDATA[%dw 2.0

output application/json


“Name” : “Prathamesh Kulkarni”




<ee:transform doc:name=”Create Request Body For Send Email” doc:id=”8f69879d-b804-40fc-b8f0-7431aaf123d4″ >

<ee:message >

<ee:set-payload ><![CDATA[%dw 2.0

output application/json

import toBase64 from dw::core::Binaries


  “message”: {

    “subject”: “This is test mail for Demo”,

    “body”: {

      “contentType”: “Html”,

      “content”: “<!DOCTYPE html>



<h1>This is Demo 1</h1>


</html>”    },

    “toRecipients”: [


        “emailAddress”: {

          “address”: “”




   “ccRecipients”: [


        “emailAddress”: {

          “address”: “”




     “attachments”: [


        “@odata.type”: “#microsoft.graph.fileAttachment”,

        “name”: “test.json”,

        “contentType”: “application/json”,

        “contentBytes”: toBase64(write(vars.attachment,”application/json”))




  “saveToSentItems”: “false”




<http:request method=”POST” doc:name=”Request to Send Email” doc:id=”8e04b0a1-9c73-4715-8b4f-8146ec81f10b” config-ref=”HTTP_Request_configuration_Send_Email” path=”/v1.0/users/{}/sendMail”>

<http:headers ><![CDATA[#[output application/java


“Authorization” : vars.bearerToken.access_token,

“Content-type” : “application/json”



<logger level=”INFO” doc:name=”Log-Flow End” doc:id=”13e07383-cafb-4f41-855d-4a398722cbdf” message=”Flow End”/>



This is how you send an email with Microsoft graph API.

We use cookies on this site to enhance your user experience. For a complete overview of how we use cookies, please see our privacy policy.