Send An Email Using Graph API With Certificates In A .NET Application

Introduction 

 
Microsoft Graph is a RESTful web API that enables you to access Microsoft Cloud service resources. Once you register your app and get authentication tokens for a user to make a request to the Graph API. For more info, click here.
 
In order to get the token for calling send email service we need app (client) ID, tenant (Directory) ID, and certificate file. App ID and tenant ID, you can get it after you register your app in the portal.
 
Configure the required APIs permission in your register app by default. It’ll have read access of Microsoft Graph.
 
Prerequisite
  1. Register your application and get the app ID and tenant ID, I have explained this in my previous blog.
  2. Certificate file.

Steps

 
Create a .NET console application and install the “Microsoft.Graph” nuget package from nuget package manager.
 
Send an Email using graph api with certificates in a .net application
 
I have created a SendEmail class that has SendMailAsync Method and it accepts four parameters, i.e. Message (email body), emailFrom( [email protected]), emailSubject (Subject of an email), emailTo (to email ID, can be multiple ID with a comma separator).
 
We have a AuthProvider class that is inherited from the IAuthenticationProvider and it implements AuthenticateRequestAsync, the AuthenticateRequestAsync method get the access token that is required to call out the Microsoft Graph Api service.
  • For acquiring token, we will use AuthenticationContext(using Microsoft.IdentityModel.Clients.ActiveDirectory)
Send an Email using graph api with certificates in a .net application
  • We will read the certificate file (I have stored certificate file in content folder) and create an instance of X509Certicate2 (using System.Security.Cryptography.X509Certificates)
  • We will use ClientAssertionCertificate and pass the appid, certificate2.
  • And finally, we will use the object of authentication context to acquire token, we pass the resource and client assertion certificate instance that helps us in getting the access token.
  • Return the access token.
Send an Email using graph api with certificates in a .net application 
We will be passing the object of AuthProvider to GraphServiceClient that accepts IAuthenticationProvider as a parameter. This step says that we have the authentication token and now we can call the send email service of graph API.
 
ToEmails separates the comma separated mail ID and creates a new list of Recipient that has email address, message is an instance of the Microsoft.Graph.Message that we will pass in graph service client object to send an email and finally we will send the email using instance of the GraphServiceClient.
 
We can use send in two ways:
  1. await _graphClient.Users[emailFrom]  
  2. .SendMail(message, false)  
  3. .Request()  
  4. .PostAsync();  
and...
  1. await _graphClient.Me  
  2. .SendMail(message, false)  
  3. .Request()  
  4. .PostAsync();   
Here, I have used the first one that uses ([email protected]).
 
Send an Email using graph api with certificates in a .net application
 
In order to send email, just call out the SendMailAsync method by creating the object of sendMail.
 
Send an Email using graph api with certificates in a .net application