Question

Is DB mail present in SQL Server hosted on Azure VM (IaaS)?

If DB mail is not supported, then what are the features available?

Was it helpful?

Solution

Database Mail is available on SQL Server on Azure IaaS and my suggestion is to configure it using SendGrid email delivery service (available on Azure Marketplace) that supplies your virtual machine with an SMTP relay. Leverage this service to setup database mail for your alerting or reporting needs.

Here you will find how to setup the SendGrid service on Microsoft Azure.

Once you have set up the SendGrid service you can configure it on your SQL Server VM as shown below.

First enable it on sp_configure:

/*
   Turn on database mail
*/

-- Select the correct database
USE [msdb]
GO

-- Just shows standard options
sp_configure
GO

-- Turn on advance options
sp_configure 'show advanced options', 1;
GO

-- Reconfigure server
RECONFIGURE;
GO

-- Turn on database xp's
sp_configure 'Database Mail XPs', 1;
GO

-- Reconfigure server
RECONFIGURE

Next, create an email account:

/*
   Creating mail account with Send Grid SMTP server
*/

-- Create a Database Mail account 1
EXEC msdb.dbo.sysmail_add_account_sp
  @account_name = 'act_Default_Email',
  @description = 'Mail account for use by all database users.',
  @email_address = 'craftydba@outlook.com',
  @replyto_address = 'craftydba@outlook.com',
  @display_name = 'SQL SERVER (IAAS-SQL16DEV)',
  @mailserver_name = 'smtp.sendgrid.net',
  @username = 'azure_d2dfaaa7a26e3f645f978bb723cd95cb@azure.com',
  @password = 'enter your unique password';
GO

-- Show the new mail accounts
EXEC msdb.dbo.sysmail_help_account_sp;
GO

In addition, you need to configure a mail profile.

/*
   Creating a mail profile
*/

-- Create a Database Mail profile
EXEC msdb.dbo.sysmail_add_profile_sp
  @profile_name = 'prf_Default_Email',
  @description = 'Profile used for administrative mail.' ;
GO

-- Show the new mail profile
EXEC msdb.dbo.sysmail_help_profile_sp;
GO

Next step is to link the email account to the profile:

/*
  Linking the mail profile to the account
*/

-- Add the account 1 to the profile
EXEC msdb.dbo.sysmail_add_profileaccount_sp
  @profile_name = 'prf_Default_Email',
  @account_name = 'act_Default_Email',
  @sequence_number = 1 ;
GO

-- Show the link between profile and accounts
EXEC msdb.dbo.sysmail_help_profileaccount_sp @profile_name = 'prf_Default_Email';

Next step, configuring database mail to give public access to the mail profile. This allows database users to send mail.

/*
   Given public access to profile
*/

-- Grant access to the profile to all users in the msdb database
EXEC msdb.dbo.sysmail_add_principalprofile_sp
  @profile_name = 'prf_Default_Email',
  @principal_name = 'public',
  @is_default = 1 ;

-- Show the new default profile
EXEC msdb.dbo.sysmail_help_principalprofile_sp

Finally use the following code to validate this solution.

/*
   Send test message
*/

-- Plain text message
EXEC msdb.dbo.sp_send_dbmail
  @profile_name = 'prf_Default_Email',
  @recipients = 'craftydba@outlook.com',
  @body = 'The stored procedure finished successfully.',
  @subject = 'Automated Success Message' ;
GO

OTHER TIPS

Yes It does with one restriction of SMTP Server. Please read these two articles to get around. Other than SMTP server I am not aware of any difference between on-premise and SQL Server on Azure Cloud.

Troubleshoot outbound SMTP connectivity issues in Azure books online

Setting up a SMTP Server on an Azure Virtual Machine by John Doyle

Licensed under: CC-BY-SA with attribution
Not affiliated with dba.stackexchange
scroll top