How to Send an Email Using a Graph API in SharePoint Framework (SPFx)


Before we get started, if you are new to SharePoint Framework web parts, please take a look at below articles about building SharePoint Framework web parts and extensions using the below links,

Graph API

Graph APIs are used to connect multiple Office365 services using a single endpoint. We have separate APIs for each of the services like Outlook and OneDrive, but we can’t access all with the same approach. This is because there are some differences in some Endpoints, Headers, Request Format, Response Format, etc...
Graph API solves this problem by sitting on top of Office365 services and act as a PROXY service to all other individual services. Learn more about Graph API by following this link.

Graph API configuration in SPFx

It’s not very difficult to do the configuration. Open your SPFx solution look for the file called “package-solution.json” under the “config” folder. Under the tag solution add the below configuration:
  1. "webApiPermissionRequests": [  
  2.       {  
  3.         "resource""Microsoft Graph",  
  4.         "scope""Mail.Send"  
  5.       }  
  6.     ]  
Since MS Graph API deals with multiple services we need to specify which scope/permission we need to perform our action, so access tokens will be generated based on that. That’s what we did here.

Generate Graph API Send Email Object

Now time to add some coding to send email. Below is the code snippet helps to create email post object to send email using Graph API. To learn more about the attributes involved in Graph API Email object check out the official documentation using this link.
  1. let emailPostBody: any = {  
  2.         “message”: {  
  3.           “subject”: “Mail Subject”,  
  4.           “body”: {  
  5.             “contentType”: “HTML”,  
  6.             “content”: “Mail Content”  
  7.           },  
  8.           “toRecipients”: [  
  9.             {  
  10.               “emailAddress”: {  
  11.                 “address”: “”  
  12.               }  
  13.             }  
  14.           ],  
  15.           “ccRecipients”: [  
  16.             {  
  17.               “emailAddress”: {  
  18.                 “address”: “”  
  19.               }  
  20.             }  
  21.           ],  
  22.         }  
  23.       };  

Invoking GraphClient in SPFx

In SPFx we will get the user context using the object “context”. We have msGraphClientFactory available in the same object which will take care of generating the Access Token based on the user context. Below is the code:
GraphClient Factory
  1. this.context.msGraphClientFactory  
Creating Graph Client,
  1. this.context.msGraphClientFactory.getClient()  
By default, this getClient will use the latest API version and takes care of creating a Graph API HTTP request with base URL. We just need to pass our endpoint and the required parameters.
Here is the full code to invoke graph client and send an email:
  1. this.context.msGraphClientFactory  
  2.             .getClient()  
  3.             .then((client: MSGraphClient): void => {  
  4.                 // get information about the current user from the Microsoft Graph  
  5.                 client  
  6.                     .api('/me/sendMail')  
  7.                     .post(emailPostBody, (error, response: any, rawResponse?: any) => {  
  8.                         // handle the response  
  9.                     });  
  10.             });  

Granting Permission to SPFx app

We need to approve the Graph API permission requested from our app through package-solution.json file. To do that first package the SPFx app and upload it in the SharePoint AppCatalog. Then go to the SharePoint Admin Center, using the URL
This will take you to the modern admin center, in left navigation go to Advanced - >API access where you can approve/reject the API permission requests for the apps.
How To Send Email Using Graph API In SharePoint Framework (SPFx)
In the upcoming screen, under “Pending Requests” you can find the permission request that we mentioned in our SPFx app. Click on the specific permission, Approve or Reject button will appear at the top, like the below screenshot. If you don’t find it under the Pending Requests section, it means the same permission is approved for different apps organization-wide, so you can find the same in the “Approved Requests” section.
How To Send Email Using Graph API In SharePoint Framework (SPFx)
Once you click on the Approve, it will open a popup Click “Approve” button in the popup. Now your request will be moved to the “Approved Requests” section.
Now we all set to run our SPFx application either from local or after deploying in any CDN, which will send email to the users based on your business logic.


I hope this article helps you understand how to send an email to any user from SharePoint Framework (SPFx) webparts. If you have any questions/issues about this article, please let me know in the comments.