Send Email Using Microsoft Graph API using C#

Jatinder Singh
4 min readOct 12, 2020

Before writing code to access Graph API and send email, you need to have either your organization’s Azure account access or Microsoft Developer account (https://developer.microsoft.com/en-us/store/register/) and an azure account linked with Microsoft Developer account.

Please note that you cannot call the Graph API to send email if you only have an individual Azure account. The reason is you need license to access Office 365 from your Azure account. The license cannot be assigned to an individual account.

Please follow along for

Steps to register a New App in Azure

Assign Office 365 license

C# code to call Graph API.

You need to follow these steps to Register a New App in Azure

  • Search for App Registrations on the home page
App Registration
  • Click on App Registrations link. You will get the following screen
App Registration listing
  • Click on New registration link on the top left. You will get the following screen
Register an application to call Graph API
  • As highlighted above, type in the name of the app, Select Single tenant or multi-tenant (For this demo purpose, Single tenant is sufficient) and finally, select first option — “Public client/native (mobile & desktop)” and then click Register.
  • Your app will be registered and you will land on the following page. Note down the ClientId and TenantId. You will require these while writing code to access Graph API.
Registered App screen
  • Click on the Certificates & secrets screen as below and Create new Secret. Once generated, copy the client secret somewhere as once you leave the screen, you will not be able to get the secret value again.
Client Secret
Client Secret
  • Next step is to Click on API Permissions (in the left menu) and then Click on “Add a Permission” button. You will get the following screen
API Permissions
  • Click on Microsoft Graph (as highlighted above) and then Application Permissions and then search for Mail and select Mail.Send (see below). Please note : You will get the Mail permissions from Delegated permissions as well but that will not work with our code. The reason is mentioned itself in the Delegated and Application Permissions below.
  • Click on “Add Permissions” button to add the permissions to the API.
  • Once the permissions are added to the API, click on “Grant admin consent” button to grant admin consent so that your code can have access to the required permissions and you can successfully send the Email. Make sure you have “Green tick” on the API as highlighted below

That’s it for the first step. Note down the Tenant ID, Client ID and Client Secret which we are going to use in our code in the final step.

In the next Step, we will learn how to Assign Office 365 license.

In the last Step of accessing the Graph API to send emails, we will write few lines of actual code to send emails :)

Wrap-up

Thanks for reading. Share to give this post some love if you found it helpful.

Jatinder

--

--