---
title: "Use Azure to Send Email with SendGrid, Graph, and Office 365"
description: "Learn how to send email in Azure using various methods, including SendGrid, Microsoft Graph, and Office 365."
canonical: "https://adamtheautomator.com/azure-send-email/"
---

# Use Azure to Send Email with SendGrid, Graph, and Office 365

> Learn how to send email in Azure using various methods, including SendGrid, Microsoft Graph, and Office 365.

Source: https://adamtheautomator.com/azure-send-email/

---

ATA Learning

Tap to hide

[

ATA Learning

](/)

*   [Home](/)
*   [Tutorials](/tutorials/)
*   [Instructors](/author/)
*   [Advertising](/advertising/)
*   [Recommended Resources](/resources/)
*   [About Adam](/about-adam/)

Search for:  

*   [](https://twitter.com/adbertram)
*   [](https://github.com/Adam-the-Automator)
*   [](https://www.linkedin.com/company/adam-the-automator-llc)
*   [](/feed/)

![Use Azure to Send Email with SendGrid, Graph, and Office 365](https://adamtheautomator.com/wp-content/uploads/2021/01/How-to-Send-Email-from-Azure.jpg)

# Use Azure to Send Email with SendGrid, Graph, and Office 365

[![](https://secure.gravatar.com/avatar/9a14f10ff1b1ec7d790d34f5b559e4d3de2d31b172e6ef266dfd8b479174d97b?s=192&d=mm&r=g)June Castillote](https://adamtheautomator.com/author/june/)13 January 202113 min. read

Categories: [Cloud](/category/cloud/)

Tags:[Microsoft Azure](/tag/microsoft-azure/)

Table of Contents

*   [Prerequisites](#h-prerequisites)
*   [Knowing Which Email Services to Use with Azure to Send Emails](#h-knowing-which-email-services-to-use-with-azure-to-send-emails)
*   [Using SendGrid](#h-using-sendgrid)
*   [Creating a SendGrid Account](#h-creating-a-sendgrid-account)
*   [Creating a SendGrid Sender Identity](#h-creating-a-sendgrid-sender-identity)
*   [Creating a SendGrid API Key](#h-creating-a-sendgrid-api-key)
*   [Sending an Email from Azure using SendGrid SMTP Relay](#h-sending-an-email-from-azure-using-sendgrid-smtp-relay)
*   [Sending an Email from Azure using SendGrid API](#h-sending-an-email-from-azure-using-sendgrid-api)
*   [Using Office 365 SMTP Auth](#h-using-office-365-smtp-auth)
*   [Creating an Office 365 Sender Address](#h-creating-an-office-365-sender-address)
*   [Sending an Email from Azure using Office 365 SMTP Relay](#h-sending-an-email-from-azure-using-office-365-smtp-relay)
*   [Using Microsoft Graph API to Send Azure Email](#h-using-microsoft-graph-api-to-send-azure-email)
*   [Registering a New App in Azure Active Directory](#h-registering-a-new-app-in-azure-active-directory)
*   [Creating a Client Secret Key](#h-creating-a-client-secret-key)
*   [Assigning API Permission and Granting Admin Consent](#h-assigning-api-permission-and-granting-admin-consent)
*   [Acquiring an Access Token](#h-acquiring-an-access-token)
*   [Sending an Email from Azure using Microsoft Graph API](#h-sending-an-email-from-azure-using-microsoft-graph-api)
*   [Next Steps](#next-steps)

Microsoft Azure does not have an email delivery service. Whether you are testing applications or developing scripts in Azure to send emails, know that there is no native email service available to you.

Outbound SMTP connection via port 25 is completely blocked in Azure, too. If you are an enterprise client, you may request to unblock port 25 for your Azure tenant. Otherwise, how else can you send emails from Azure?

There are typically two ways to let outbound emails through Azure; using an _[SMTP smart host](https://en.wikipedia.org/wiki/Smart_host)_ and _[REST API](https://www.smashingmagazine.com/2018/01/understanding-using-rest-api/)_ requests. Most email delivery services offer both SMTP and API methods of sending emails. In this article, you’ll learn how to send emails from Azure to the rest of the world.

## Prerequisites

This article is a How-To, and if you plan to follow the examples as they progress, there are some requirements you must meet.

*   An Azure subscription. You may register for a _[free trial](https://azure.microsoft.com/en-us/free/)_ Azure subscription if you don’t have one yet.
*   An _[Azure Virtual Machine (VM)](https://docs.microsoft.com/en-us/azure/virtual-machines/windows/quick-create-portal)._ Refer to _[Create a Windows virtual machine in the Azure portal](https://docs.microsoft.com/en-us/azure/virtual-machines/windows/quick-create-portal)_ to learn how to create a new VM if you don’t have one yet.
*   Windows PowerShell 5.1 or PowerShell 7.1.
*   A valid sender email address. Using a free email address domain, such as [_gmail.com_](http://gmail.com), is not recommended. As much as possible, use a sender email address that uses a domain that you own.

## Knowing Which Email Services to Use with Azure to Send Emails

There are several cloud email services available to use. Some of the prominent names are _[SendGrid](https://sendgrid.com/)_, _[MailJet](https://www.mailjet.com/)_, and _[MailGun](https://www.mailgun.com/)_. Whichever email service you choose, using them to send emails from Azure is similar; server address and port for SMTP and API endpoint address for REST.

SendGrid is an example of a third-party email service used in this article. Additional email services used as examples are [Office 365 SMTP relay](https://adamtheautomator.com/office-365-smtp-relay/ "Office 365 SMTP relay") and Microsoft Graph API.

**_Related: [Which Azure Email Service to Choose for 2021](https://adamtheautomator.com/azure-email-service/)_**

## Using SendGrid

_[SendGrid](https://sendgrid.com/)_ is probably the most popular email service option for sending emails from Azure. Azure and SendGrid are so popular because there used to be a free plan with 25,000 emails per month limit to Azure customers.

Although the free plan is no longer being offered in the Azure portal, as _[confirmed by Microsoft](https://github.com/MicrosoftDocs/azure-docs/issues/68153#issuecomment-751768435)_, it is possible to sign-up for a free subscription with 100 emails per day limit. You will then have access to SendGrid’s SMTP relay service or Email API to send emails from an Azure app or VM.

### Creating a SendGrid Account

Using SendGrid’s services requires you to have a SendGrid account first. Go to the _[Sign-up](https://signup.sendgrid.com/)_ page, and you should see the form as shown below. Enter your email address and your chosen password. Then, accept the terms and click on the **Create Account** button.

![Signing up for a free SendGrid account](https://adamtheautomator.com/wp-content/uploads/2021/01/Untitled-2021-01-05T101411.277.png)

Signing up for a free SendGrid account

On the next page, you need to enter your name and company information. You also need to select the appropriate options such as your role, emails to send per month, and the number of employees. Then, click on **Get Started.**

![Entering your account information](https://adamtheautomator.com/wp-content/uploads/2021/01/Untitled-2021-01-05T101439.564.png)

Entering your account information

> _When you first log in to SendGrid, you may get an error saying, “You are not authorized to access SendGrid; please contact Support.” When this happens, [open a ticket with SendGrid support](https://support.sendgrid.com/hc/en-us/requests/new#login-issue). They may need to ask some verification questions before they unlock your access._

> _You will be asked to enable Two-Factor authentication during your first log in. Make sure to follow the instructions accurately._

### Creating a SendGrid Sender Identity

After creating the SendGrid account, the next step is to authorize the sender’s identity. There are two options; authenticate a single sender (e.g., [sender@domain.com](mailto:sender@domain.com)) or authenticate an entire domain (e.g., [domain.com](https://www.domain.com/)).

In this example, you will create a single sender identity that will serve as your sender address. Only the verified sender address will be allowed to send messages through SendGrid’s service. Follow the instructions below to create the single sender identity.

1.  On the **Welcome** page, click on the **Create a Single Sender** button.

![Creating a single sender](https://adamtheautomator.com/wp-content/uploads/2021/01/Untitled-2021-01-05T101553.434.png)

Creating a single sender

2\. Next, on the **Create a Sender** fly-out, provide all the required information. The required fields are marked with a red asterisk next to them for easier reference. After entering the details, click on **Create**.

![Providing the sender details ](https://adamtheautomator.com/wp-content/uploads/2021/01/Untitled-2021-01-05T101648.638.png)

Providing the sender details

You will then see the new sender address listed under the **Single Sender Verification** page. As you can see from the example below, the address is not yet verified, as indicated by a red X under the **VERIFIED** column.

![Viewing the sender address verification status](https://adamtheautomator.com/wp-content/uploads/2021/01/Untitled-2021-01-05T101741.110.png)

Viewing the sender address verification status

3\. To verify the sender, locate the email sent to the sender’s address, similar to the screenshot below. Then, click on the **Verify Single Sender** button.

![Verifying the Single Sender address ](https://adamtheautomator.com/wp-content/uploads/2021/01/Untitled-2021-01-05T101825.585.png)

Verifying the Single Sender address

The sender’s address will become verified. You should see a similar page, as shown below, confirming that the sender verification is complete.

![Confirming that the sender address verification is complete](https://adamtheautomator.com/wp-content/uploads/2021/01/Untitled-2021-01-05T101908.601.png)

Confirming that the sender address verification is complete

### Creating a SendGrid API Key

After creating the single sender address, the interface to create API keys becomes available. Think of the SendGrid API key as the password. Without it, your code cannot authenticate with the SendGrid SMTP relay service. Follow the steps below to create a new API key.

1.  On the left-hand side menu, click on **Settings > API Keys**. Then, click on the **Create API Key** button on the top-right corner of the page.

![Creating a new SendGrid API key](https://adamtheautomator.com/wp-content/uploads/2021/01/Untitled-2021-01-05T102002.959.png)

Creating a new SendGrid API key

2\. The **Create API Key** fly-out appears. Enter the name of the API key you are creating. Use a name that makes sense, such as “_Azure Testing API Key._” Next, for simplicity’s sake, select **Full Access** as the permission. Finally, click on the **Create & View** button.

![Naming the API key and choosing permissions](https://adamtheautomator.com/wp-content/uploads/2021/01/Untitled-2021-01-05T102052.607.png)

Naming the API key and choosing permissions

3\. The new API Key is shown next. Now copy and save the key-value because it will not be shown to you again. After copying the key, click on **Done**.

![Saving the new SendGrid API key](https://adamtheautomator.com/wp-content/uploads/2021/01/Untitled-2021-01-05T102140.586.png)

Saving the new SendGrid API key

### Sending an Email from Azure using SendGrid SMTP Relay

Now you have the required components (account, sender, and key) to use SendGrid to send Azure emails. It is time to test whether the SendGrid SMTP relay service works. In this section, you are going to use PowerShell to send emails from an Azure VM.

Before running any code, you should know these requirements.

*   The SendGrid SMTP server address is _[smtp.sendgrid.net](http://smtp.sendgrid.net)_.
*   The username used for SMTP authentication is always _apikey._
*   The password to use is the value of the API key you created in SendGrid.
*   Do not use port 25. Use port 587 instead.
*   Only the verified sender address in SendGrid is valid to use as the sender of your emails. In this example, the authorized sender is _[mailer@lzex.ml](mailto:mailer@lzex.ml)_.

The script below will send an email through the SendGrid SMTP relay. Copy the code below and change the `$sendGridApiKey`, `From`, `To`, and `Body` values. Then, run the code in your PowerShell session. Refer to the comments to understand what each line of code does.

```powershell
# Set your API Key here
$sendGridApiKey = 'SG...........P258'

$SendGridEmail = @{
	# Use your verified sender address.
	From = 'mailer@lzex.ml'
	# Specify the email recipient. Any valid email address should work.
	To = 'june.castillote@gmail.com'
	# Update this with the email subject you want to use.
	Subject = 'This is a test message from Azure via SendGrid'
	# Update this with the email body or message that you want to send.
	Body = 'This is a test message from Azure via SendGrid'
	
	# DO NO CHANGE ANYTHING BELOW THIS LINE
	SmtpServer = 'smtp.sendgrid.net'
	Port = 587
	UseSSL = $true
	Credential = New-Object PSCredential 'apikey', (ConvertTo-SecureString $sendGridApiKey -AsPlainText -Force)	
}

# Send the email
Send-MailMessage @SendGridEmail
```

The demo below shows what running the code above in PowerShell looks like in real-time.

![Sending an Email from Azure using SendGrid SMTP Relay](https://adamtheautomator.com/wp-content/uploads/2021/01/sendgrid_smtp.gif)

Sending an Email from Azure using SendGrid SMTP Relay

To confirm email deliverability, check the recipient mailbox to find the test message you sent. The result would be similar to the one shown below. As you can see, the message came from the sender’s address _via sendgrid.net._

![Confirming the SMTP test message was delivered](https://adamtheautomator.com/wp-content/uploads/2021/01/Untitled-2021-01-05T102648.909.png)

Confirming the SMTP test message was delivered

**_Relate: [Send-MailMessage: The PowerShell Way to Send Email](https://adamtheautomator.com/send-mailmessage/)_**

### Sending an Email from Azure using SendGrid API

Another way to use SendGrid with Azure to send emails is using the _[SendGrid Web API V3](https://docs.sendgrid.com/api-reference/how-to-use-the-sendgrid-v3-api/authentication)_. Instead of communicating with SendGrid via SMTP, using the API communicates using HTTP. The HTTP request is sent to SendGrid’s API endpoint URL.

The example PowerShell script below uses the `Invoke-RestMethod` cmdlet to submit the email request from Azure to SendGrid. Copy the code and change the `$sendGridApiKey`, `$fromAddress`, `$toAddress`, `$mailSubject`, and `$mailMessage` variable values.

When you’re done updating the variables, run the code in PowerShell.

```powershell
Set your API Key here
 $sendGridApiKey = 'SG………..P258'
 Set the sender and recipient addresses
 $fromAddress = "mailer@lzex.ml"
 $toAddress = "june.castillote@gmail.com"
 Set the mail subject
 $mailSubject = "This is a test message from Azure via SendGrid API"
 Set the mail message
 $mailMessage = "This is a test message from Azure via SendGrid API"
 DO NOT CHANGE ANYTHING BELOW THIS LINE
 Compose the Mail Body
 $mailbody = @{
   personalizations = @(
     @{
       to      = @(
         @{
           email = $toAddress
         }
       )
       subject = $mailSubject
     }
   )
   from             = @{
     email = $fromAddress
   }
   content          = @(
     @{
       type  = "text/plain"
       value = $mailMessage
     }
   )
 } | ConvertTo-Json -Depth 10
 $headers = @{'Authorization' = "Bearer $($sendGridApiKey)" }
 $mailApiUri = 'https://api.sendgrid.com/v3/mail/send'
 Send the email
 Invoke-RestMethod -Method Post -Uri $mailApiUri -Body $mailbody -Headers $headers -ContentType application/json
```

After running the PowerShell code above, check the recipient’s mailbox and confirm that it received the test message. The example below shows a successfully delivered test message from Azure via SendGrid API.

![Confirming the API test message was delivered](https://adamtheautomator.com/wp-content/uploads/2021/01/Untitled-2021-01-05T102801.832.png)

Confirming the API test message was delivered

If you want to learn more about SendGrid Web API, visit the _[V3 Mail Send API](https://docs.sendgrid.com/api-reference/how-to-use-the-sendgrid-v3-api/authentication)_ page.

## Using Office 365 SMTP Auth

Another option to send Azure emails is to use the Office 365 SMTP relay. That is if your organization already has an Exchange Online subscription. Just like using SendGrid as an SMTP smart host, the Office 365 SMTP relay requires authentication and the use of port 587 only.

Before using Office 365 SMTP relay, you must know the following conditions for it to work.

*   The sender address must be a valid Exchange Online recipient object such as a mailbox or a mail user. But, if non-delivery receipts (NDR) are to be stored, use a mailbox instead.
*   The authentication user must have a valid Exchange Online license. Only licensed Exchange Online users are allowed to use the Office 365 SMTP relay.
*   Suppose the authentication user is different from the sender. In that case, the user must be _[assigned the Send As permission](https://learn.microsoft.com/en-us/powershell/module/exchange/add-recipientpermission?view=exchange-ps)_ to the sender account.
*   The authentication method used is _basic (legacy)_. This means that if SMTP basic authentication is disabled in your organization or when Microsoft finally drops basic authentication, SMTP Auth will no longer work.
*   The SMTP relay server address is [_smtp.office365.com_](http://smtp.office365.com/), and the port number is 587.

**_Related: [How to Send Email with Office 365 Direct Send and PowerShell](https://adamtheautomator.com/office-365-direct-send/)_**

### Creating an Office 365 Sender Address

In this example, the sender and the authentication user are separate. Make sure to _[connect to Exchange Online PowerShell](https://learn.microsoft.com/en-us/powershell/exchange/connect-to-exchange-online-powershell?view=exchange-ps)_ first. Once you’re connected, follow the steps below.

Create a shared mailbox using the command below. Change the `-Name` and `-PrimarySMTPAddress` to your correct values.

```powershell
New-Mailbox -Shared -Name 'SMTP Mailer 365' -PrimarySMTPAddress 'SMTPMailer365@lzex.ml'
```

The shared mailbox should be created and would return a result similar to the one below.

![Creating a new shared mailbox as the sender](https://adamtheautomator.com/wp-content/uploads/2021/01/Untitled-2021-01-05T102843.878.png)

Creating a new shared mailbox as the sender

Then, assign the _Send As_ permission to the authentication user. In the example below, the user is assigned the _Send As_ permission to the shared mailbox.

```powershell
Add-RecipientPermission -Identity 'SMTPMailer365@lzex.ml' -Trustee 'june@lzex.ml' -AccessRights SendAs -Confirm:$false
```

Once the command above is executed, you should get a result similar to the one below.

![Assigning Send As permission](https://adamtheautomator.com/wp-content/uploads/2021/01/Untitled-2021-01-05T102915.680.png)

Assigning Send As permission

### Sending an Email from Azure using Office 365 SMTP Relay

After the sender address creation and assigning the Send As permission, the next step is to test the email delivery using PowerShell and Office 365 SMTP relay.

The code below authenticates using _[june@lzex.ml](mailto:june@lzex.ml)._ The address _[SMTPmailer365@lzex.ml](mailto:SMTPmailer365@lzex.ml)_ appears as the sender. Make sure to assign your correct values before running the code.

```powershell
# Provide your SMTP credentials
$username = 'june@lzex.ml'
$password = '*************'

# Provide the sender and recipient email address
$fromAddress = 'SMTPMailer365@lzex.ml'
$toAddress = 'june.castillote@gmail.com'

# Specify the email subject and the message
$mailSubject = 'This is a test message from Azure via Office 365 SMTP Relay'
$mailMessage = 'This is a test message from Azure via Office 365 SMTP Relay'

# DO NO CHANGE ANYTHING BELOW THIS LINE
$Office365RelayEmail = @{
	From = $fromAddress
	To = $toAddress
	Subject = $mailSubject
	Body = $mailMessage	
	SmtpServer = 'smtp.office365.com'
	Port = 587
	UseSSL = $true
	Credential = New-Object PSCredential $username, (ConvertTo-SecureString $password -AsPlainText -Force)	
}

# Send the email
Send-MailMessage @Office365RelayEmail
```

> _Using credentials in scripts that are in plain text is not recommended. Production scripts must use credential encryption or secret management to secure usernames and passwords._

Now it’s time to check the recipient’s mailbox. Confirm that the test message was received.

![Confirming the Office 365 SMTP relay test message was delivered](https://adamtheautomator.com/wp-content/uploads/2021/01/Untitled-2021-01-05T103058.675.png)

Confirming the Office 365 SMTP relay test message was delivered

## Using Microsoft Graph API to Send Azure Email

Instead of using the Office 365 SMTP relay, the more secured and recommended way is to use _[Microsoft Graph API](https://docs.microsoft.com/en-us/graph/api/overview?view=graph-rest-1.0)_. With Microsoft Graph API, you can send emails from any mailbox in your organization using REST API calls.

To use Microsoft Graph API to send emails requires a _[registered Azure AD app](https://learn.microsoft.com/en-us/power-apps/developer/data-platform/walkthrough-register-app-azure-active-directory)._ The registered app must be assigned with the _Mail.Send_ API permission. The following sections will show you how to configure Microsoft Graph API and use it to send emails.

**_Related: [Using the Microsoft Graph API with PowerShell](https://adamtheautomator.com/azure-send-email/)_**

### Registering a New App in Azure Active Directory

In this section, you will register a new web app in the Azure active directory. The new app acts as the authentication identity for Microsoft Graph. Log in to the _[Azure Portal](https://aad.portal.azure.com)_ if you haven’t logged in yet.

1.  Navigate to Azure **Active Directory —> App registration.** Then, click on the **New registration** button.

![Creating a new app registration](https://adamtheautomator.com/wp-content/uploads/2021/01/Untitled-2021-01-05T103147.081.png)

Creating a new app registration

2\. On the **Register an application** page, enter the name of the app.

3\. Under the **Supported account types**, choose _Accounts in this organizational directory only_. Type in _HTTP://localhost_ as the **Redirect URI**. Finally, click on the **Register** button.

![Registering an application](https://adamtheautomator.com/wp-content/uploads/2021/01/Untitled-2021-01-05T103307.586.png)

Registering an application

4\. Wait for the registration to complete. The process only takes a few seconds. Don’t forget to note the resulting **Application (client) ID** and the **Directory (tenant) ID** values. You will need those later.

![Saving the Application ID and Directory values](https://adamtheautomator.com/wp-content/uploads/2021/01/Untitled-2021-01-05T103342.140.png)

Saving the Application ID and Directory values

### Creating a Client Secret Key

Think of the application ID that you created in the previous section as the username. And that application ID needs a password – which is the secret key.

To add a new application secret key, follow the steps below.

1.  Go to **Certificates & secrets**, then click on **New client secret**.
2.  Input the description for the client secret, such as _key1_.
3.  Select when the secret expires and click **Add.** In the example below, the secret key expires in 1 year.

![Adding a new client secret key](https://adamtheautomator.com/wp-content/uploads/2021/01/Untitled-2021-01-05T103432.037.png)

Adding a new client secret key

You’ll see the new key listed under the **Client secrets** section. Now is the only time you will see the key value, so save a copy.

![Viewing the client secret key value](https://adamtheautomator.com/wp-content/uploads/2021/01/Untitled-2021-01-05T103502.422.png)

Viewing the client secret key value

### Assigning API Permission and Granting Admin Consent

Now that you’ve created the application ID and secret, what’s left is to assign the required Microsoft Graph API permission. Without assigning permission, the application can authenticate but will have no authority to do anything, such as to send emails.

To start assigning permission, follow the steps below.

1.  Click on **API permissions** on the left-side menu.

2\. Then, under the **Configured permissions** page, click on the **Add a permission** button.

![Adding API permission](https://adamtheautomator.com/wp-content/uploads/2021/01/Untitled-2021-01-05T103553.824.png)

Adding API permission

3\. In the **Request API permissions** fly-out, click to select **Microsoft Graph API**.

![Selecting Microsoft Graph API](https://adamtheautomator.com/wp-content/uploads/2021/01/Untitled-2021-01-05T103632.208.png)

Selecting Microsoft Graph API

4\. When asked to choose the type of permission required by the application, click on **Application permissions**.

5\. In the search box, type in _Mail.Send_ to search for it. In the result, click to check the _Mail.Send_ permission. Lastly, click on **Add permissions**.

![Assigning mail.send permission](https://adamtheautomator.com/wp-content/uploads/2021/01/Untitled-2021-01-05T103724.767.png)

Assigning mail.send permission

6\. You’ll notice that the permission status is “**Not granted for <organization name>.”** At this point, you or a Global admin must first give consent for the application. To grant permission, click on the **Grant admin consent for <organization>** button.

![Granting Admin consent](https://adamtheautomator.com/wp-content/uploads/2021/01/Untitled-2021-01-05T103838.553.png)

Granting Admin consent

The API permission status then changes to “**Granted for <organization name>”**.

![Confirming the API permission status](https://adamtheautomator.com/wp-content/uploads/2021/01/Untitled-2021-01-05T103917.468.png)

Confirming the API permission status

### Acquiring an Access Token

Requests sent to Microsoft Graph API requires an access token. At this point, you already have your application ID, secret key, and tenant ID. Those three pieces of information are what’s needed to acquire an access token.

The PowerShell script below sends the access token request to the Microsoft Graph API endpoint. You must update the `$client_id`, `$client_secret`, and `$tenant_id` to your correct values first. Then, copy and paste the code in PowerShell to request the access token.

```powershell
# replace with your application ID
$client_id = 'APPLICATION ID'
# replace with your secret key
$client_secret = 'SECRET KEY'
# replace with your tenant ID
$tenant_id = 'TENANT ID'

# DO NOT CHANGE ANYTHING BELOW THIS LINE
$request = @{
        Method = 'POST'
        URI    = "https://login.microsoftonline.com/$tenant_id/oauth2/v2.0/token"
        body   = @{
            grant_type    = "client_credentials"
            scope         = "https://graph.microsoft.com/.default"
            client_id     = $client_id
            client_secret = $client_secret
        }
    }
# Get the access token
$token = (Invoke-RestMethod @request).access_token
# view the token value
$token
```

The demonstration below shows the PowerShell script above in action. Notice that the requested access token is stored to the `$token` variable.

![Requesting access token](https://adamtheautomator.com/wp-content/uploads/2021/01/acquire_token.gif)

Requesting access token

> _Access tokens are valid for one (1) hour only from the time it was acquired. You will need to request another access token after the previous token expired._

### Sending an Email from Azure using Microsoft Graph API

You are now ready to use Microsoft Graph API with Azure to [_send emails_](https://learn.microsoft.com/en-us/graph/api/user-sendmail?view=graph-rest-1.0&tabs=http). This time you will create the request to send an email using the access token you acquired in the previous section.

The script below composes a plain-text message with the basic components such as the sender, recipient, subject, and content. Make sure to change the values of `$fromAddress`, `$toAddress`, `$mailSubject`, and `$mailMessage`. Then, please copy the code and run it in PowerShell.

```powershell
# Provide the sender and recipient email address
$fromAddress = 'SENDER ADDRESS HERE'
$toAddress = 'RECIPIENT ADDRESS HERE'

# Specify the email subject and the message
$mailSubject = 'This is a test message from Azure via Microsoft Graph API'
$mailMessage = 'This is a test message from Azure via Microsoft Graph API'

# DO NOT CHANGE ANYTHING BELOW THIS LINE
# Build the Microsoft Graph API request
$params = @{
  "URI"         = "https://graph.microsoft.com/v1.0/users/$fromAddress/sendMail"
  "Headers"     = @{
    "Authorization" = ("Bearer {0}" -F $token)
  }
  "Method"      = "POST"
  "ContentType" = 'application/json'
  "Body" = (@{
    "message" = @{
      "subject" = $mailSubject
      "body"    = @{
        "contentType" = 'Text'
        "content"     = $mailMessage
      }
      "toRecipients" = @(
        @{
          "emailAddress" = @{
            "address" = $toAddress
          }
        }
      )
    }
  }) | ConvertTo-JSON -Depth 10
}

# Send the message
Invoke-RestMethod @params -Verbose
```

The demo below shows what happens when you run the PowerShell script above.

![Sending an email using Microsoft Graph API](https://adamtheautomator.com/wp-content/uploads/2021/01/send_the_message.gif)

Sending an email using Microsoft Graph API

Finally, to confirm email delivery, find the email in your recipient’s mailbox. You should see a similar message to the image below.

![Confirming the Microsoft Graph API test message was delivered](https://adamtheautomator.com/wp-content/uploads/2021/01/Untitled-2021-01-05T104138.463.png)

Confirming the Microsoft Graph API test message was delivered

Related:[How to Send Email Securely with PowerShell](https://adamtheautomator.com/powershell-email/)

## Next Steps

Having no native Azure email delivery service should not be a showstopper. Admins and developers can still write code to send emails from Azure. There are third-party email services that offer SMTP relay and email API integration.

Using the Office 365 SMTP relay and Microsoft Graph API are also great options if you already have an Office 365 subscription.

The next step for you is to try out the email delivery services that were not covered in this article, such as _MailJet_, _MailGun_, _SocketLabs_, and _SendInBlue_. Perhaps try using them with _[Azure Automation Runbooks](https://blog.ipswitch.com/getting-started-with-azure-automation-introduction)_ and _[Azure Web Apps](https://azure.microsoft.com/en-us/products/app-service/web/),_ too!

Share this article

[Share on X](https://twitter.com/intent/tweet?url=https%3A%2F%2Fadamtheautomator.com%2Fazure-send-email%2F&text=Use%20Azure%20to%20Send%20Email%20with%20SendGrid%2C%20Graph%2C%20and%20Office%20365)[Share on Facebook](https://www.facebook.com/sharer/sharer.php?u=https%3A%2F%2Fadamtheautomator.com%2Fazure-send-email%2F)[Share on LinkedIn](https://www.linkedin.com/sharing/share-offsite/?url=https%3A%2F%2Fadamtheautomator.com%2Fazure-send-email%2F)

## Related Posts

![](https://adamtheautomator.com/wp-content/uploads/2026/07/featured_image-1.webp)

### [Microsoft Azure Certification Roadmap: Choose the Right Path](/azure-certification-roadmap-2/)

Choose the right Microsoft Azure certification for your career goals. Compare AZ-900, AZ-104, AZ-305, AZ-400, AZ-700, DP-700, study timelines, and 2026 retirements.

![](https://adamtheautomator.com/wp-content/uploads/2021/05/How-to-RenameMove-Azure-Resource-Groups-GUI-and-CLI.jpg)

### [Smart Ways to Rename Azure Resource Groups (GUI and CLI)](/rename-azure-resource-group/)

Unlock the secret to Rename Azure Resource Groups! Dive into our easy guide for reshaping your Azure landscape with Portal, PowerShell, and CLI techniques.

![](https://adamtheautomator.com/wp-content/uploads/2021/03/azure-cli.jpg)

### [Master Azure CLI: Command Your Cloud with Confidence](/azure-cli/)

Unlock the full potential of cloud management with the Azure CLI. This guide provides the essentials to get you started with Azure resources efficiently.

## Categories

*   [IT Ops](/category/it-ops/)
*   [Cloud](/category/cloud/)
*   [DevOps](/category/devops/)
*   [Home Ops](/category/home-ops/)
*   [Information Security](/category/infosec/)
*   [Software Development](/category/software-development/)

## Site

*   [Home](/)
*   [Tutorials](/tutorials/)
*   [Instructors](/author/)
*   [Advertising](/advertising/)
*   [Recommended Resources](/resources/)
*   [About Adam](/about-adam/)

Copyright 2026© ATA Learning | [Privacy Policy](/privacy/)
