How to setup Azure SQL email notifications

You must know what is happening in the database if you are a Database Administrator. You need to be aware of the activities going on in the database. Therefore, email notifications can help a lot in monitoring database activities. You can define a notification email triggered when a specific action triggers in the database.

How to configure email notification in Azure SQL

You can send email notifications in an Azure SQL database using a SendGrid account. This article will help you learn how to send email notifications with the help of a SendGrid account.

  • Create SendGrid account in Azure
  • Generate API key in SendGrid
  • Create a logic app in Azure

Create SendGrid account in Azure

This section will help you to create a SendGrid account in Microsoft Azure. Follow the below steps to create a SendGrid account:

  • Open your Azure portal and search for SendGrid in the search box.
  • Click on SendGrid Accounts.
Create sendgrid account in Azure
  • Click on Create to create a new SendGrid account.
  • Specify the subscription to use, resource group, account name, and plan. Then, click on Change plan to select the plan you want to use.
  • I am using the Free plan. You can select any plan depending on your requirements. Click on Review + subscribe.
how to configure email notifications SQL
  • Review the configuration of the SendGrid account and click on Subscribe.
  • The subscription is created. However, the process is not complete yet.
  • In the Azure portal, please search for your SendGrid account in the search box and click on it.
  • Then click on Open SaaS Account on the publisher’s site to configure your SendGrid account. The SendGrid website will open in a new tab.
  • Firstly, you have to verify your account details. Also, you have to create a sender. I am selecting the Single Sender type.
  • Enter the details of the sender like email address that will be used to send the email. After entering the details, click on Create.
how to configure email notification azure sql
  • Once you have created the sender, you must verify the email address.
  • After the verification, you can navigate to your dashboard and manage your SendGrid account.

Read: How to Create a Single Database in Azure SQL

Generate API key in SendGrid

The next step is to create an API key that you will use to authenticate to the SendGrid account while sending emails from the Azure SQL database.

  • To create an API key, navigate to Settings in the left-hand navigation bar, click on API Keys, and then click on Create API Key.
Configuring email notifications in Azure SQL database
  • Specify the name of your API Key and select the access type that best fits your requirements. Click on Create & View.
Create API key in SendGrid
  • You will see the API key you generated. Carefully copy it to a safe place.

Read: Pause and resume Azure SQL database

Create a logic app in Azure

The next step is to create a logic app in Azure. The logic app will help you to trigger an action. In our case, that action is to send an email using the SendGrid API. Follow the steps below to create the logic app:

  • In the Azure portal, search for Logic Apps and open it.
  • Click on Add to create a logic app.
  • In the Basics tab, specify the subscription to use, the resource group’s name, the logic app’s name, and the region. Click on Next: Hosting >.
send email from azure sql database
  • In the Hosting tab, select the Azure storage account to use and the plan type. If you do not have one, you must create one. Click Next to configure more options. At the end, click Create.
  • If you do not know how to create a storage account in Azure, you can read our article Backup and restore SQL Server to Azure Blob storage. In this article, we have defined how to create a storage account in Azure.
  • The next step is to create triggers and actions in the Azure logic app.
  • To create a trigger, open your logic app and click on Events in the left-side navigation bar.
  • Click on Logic Apps.
how to Create API key in SendGrid
  • Click on Templates and select the template named Blank Logic App.
how to send email from azure sql database
  • Search for SQL Server and select the trigger according to your requirements. I am selecting the When an item is created(v2). This trigger will fire an event when any new entry is made in the specified table in the database.
  • You have to make a connection with your database. Specify the necessary details about your database to make a connection. See the image below for reference:
Azure SQL database sendgrid
  • Now, you have to specify the table in the database to which an email will be sent when a record is created. Also, set the time frequency for the trigger.
  • For example, if you have set the time frequency to 1 minute, the trigger will check for any new record in the table after every 1 minute and send the email if any new record is found in the table. Click on New step.
azure sql sendgrid
  • Search for SendGrid in the search box and click on SendGrid.
  • In the SendGrid actions, select the Send email (v4) action.
how to Create sendgrid account in Azure
  • Specify the connection name of the connection to the Azure SQL database that we created earlier. In our example, it was TestCon. Also, specify the SendGrid API Key that you created earlier. Click on Create.
how to Create API key in SendGrid
  • Now, you have to specify the email parameters, such as the sender’s and receiver’s email addresses, the subject line, and the body.
  • In the body of the email, you can also select some dynamic content.
  • For example, you can create a column in the table to store HTML content and select this column in the email body. The HTML code will then be sent to the sender.
  • Click on Save As to save the app. Specify the app’s name and click on Create to create it.
send email from azure sql database
  • Now, if we insert a new row in the Student table, we will receive an email at the specified address.

You may also like to read the following articles on Azure SQL.

Thus, you might have learned how to send emails from an Azure SQL database.

Top 200 SQL Server Interview Questions and Answers

Free PDF On Top 200 SQL Server Interview Questions And Answers

Download A 40 pages PDF And Learn Now.