Search
StarWind is a hyperconverged (HCI) vendor with focus on Enterprise ROBO, SMB & Edge

Sending an Email from Azure using Microsoft Graph API

  • March 30, 2023
  • 5 min read
IT Production Manager. Nicolas is primarily focused on Microsoft technologies, he is a Microsoft MVP in Cloud and Datacenter Management.
IT Production Manager. Nicolas is primarily focused on Microsoft technologies, he is a Microsoft MVP in Cloud and Datacenter Management.


Sending emails from Azure is a recurrent task for sysadmins or developers. To send email, there are many options, you can use SendGrid which is a great service for sending emails from Azure, you can use Graph API which is the option I will describe in this article. In next months, you will be able to use Azure communication service, which is still in public preview, so not ready for production.

What is interesting with this option is that you don’t need to use login/password in your script or code. With Graph API, you can send emails from any mailbox in your organization using REST API calls.

Getting started

First, we need a new app registration to authenticate against Azure AD. Go to the Azure Portal, Azure Active Directory section, then App registrations:

App registrations

Click New registration to create a new one.

Click New registration to create a new one

Enter the name of the application and select “Single tenant”:

Enter the name of the application and select “Single tenant”

After registering the new app registration, copy the following value:

After registering the new app registration, copy the following value

We will need these values later in our PowerShell code. Next, we need to create a new application secret key, go to Certificates & secrets and click New client secret

Go to Certificates & secrets and click New client secret

Enter the description and select when the secret will expire (recommended is 6 months).

Add a client secret

Don’t forget to note the value, you will need it later. Don’t forget to note the value, you will need it later

Now we need to assign permission to this application in order to send email. Without permissions, the application can only authenticate but will not be able to send emails.

Go to the API permissions tab, and click Add a permission

Go to the API permissions tab, and click Add a permission

Select Microsoft Graph and click Application permissions

Select Microsoft Graph and click Application permissions

In the search box, type in Mail.Send. Select the Mail.Send permission.

In the search box, type in Mail.Send. Select the Mail.Send permission

To grant permission, click on the Grant admin consent button.

To grant permission, click on the Grant admin consent button

Now, we can use our Azure App to send a test email.

Requests sent to Microsoft Graph API require an access token that you must generate. More information can be found here: https://learn.microsoft.com/en-us/graph/auth/auth-concepts

After generating the access token, you can use the following code. Below is a PowerShell code to send an email using Azure App. Replace the variables:

 

 

Found Nicolas’s article helpful? Looking for a reliable, high-performance, and cost-effective shared storage solution for your production cluster?
Dmytro Malynka
Dmytro Malynka StarWind Virtual SAN Product Manager
We’ve got you covered! StarWind Virtual SAN (VSAN) is specifically designed to provide highly-available shared storage for Hyper-V, vSphere, and KVM clusters. With StarWind VSAN, simplicity is key: utilize the local disks of your hypervisor hosts and create shared HA storage for your VMs. Interested in learning more? Book a short StarWind VSAN demo now and see it in action!