Home
%3CLINGO-SUB%20id%3D%22lingo-sub-386235%22%20slang%3D%22en-US%22%3ESending%20emails%20in%20Azure%20SQL%20Managed%20Instance%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-386235%22%20slang%3D%22en-US%22%3E%3CP%3E%3CSTRONG%3EFirst%20published%20on%20MSDN%20on%20Aug%2031%2C%202018%20%3C%2FSTRONG%3E%20%3CBR%20%2F%3E%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Fsql-database%2Fsql-database-managed-instance%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3E%20Azure%20SQL%20Database%20Managed%20Instance%20%3C%2FA%3E%20enables%20you%20to%20use%20most%20of%20the%20SQL%20Server%20features%20in%20fully%20managed%20cloud%20environment.%20One%20of%20the%20features%20is%20database%20mail%20system%20that%20enables%20you%20to%20send%20emails%20directly%20from%20Managed%20Instance.%20In%20this%20post%20we%20will%20see%20how%20to%20setup%20mail%20profile%20and%20send%20emails%20in%20Managed%20Instance.%26nbsp%3B%3C%2FP%3E%0A%3CH2%20id%3D%22toc-hId-1709323046%22%20id%3D%22toc-hId-1709323046%22%3ESetup%3C%2FH2%3E%0A%3CP%3E%3CBR%20%2F%3EFirst%2C%20you%20need%20to%20ensure%20that%20Managed%20Instance%20can%20reach%20your%20mail%20server.%20If%20your%20email%20server%20is%20using%20port%2025%20then%20you%20would%20need%20to%20open%20outbound%20NSG%20on%20port%2025%20to%20Internet.%20Managed%20Instance%20is%20placed%20in%20your%20VNet%2Fsubnet%20and%20you%20have%20control%20over%20the%20network%20traffic.%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThen%20you%20would%20need%20to%20setup%20email%20account%20information%20that%20contain%20address%20of%20the%20e-mail%20server%20that%26nbsp%3B%20will%20actually%20send%20the%20emails%2C%20with%20login%2Fpassword%20information%20required%20to%20access%20e-mail%20server.%20In%20the%20following%20script%20set%20%24(mailserver)%2C%20%24(loginEmail)%2C%20and%20%24(password)%20information%2C%20change%20titles%2Fdescription%20shown%20as%20'...'%20and%20run%20it%3A%3C%2FP%3E%0A%3CPRE%3E--%20Create%20a%20Database%20Mail%20account%20%0AEXECUTE%20msdb.dbo.sysmail_add_account_sp%20%0A%40account_name%20%3D%20'...'%2C%20%0A%40description%20%3D%20'...'%2C%20%0A%40email_address%20%3D%20'%24(loginEmail)'%2C%20%0A%40display_name%20%3D%20'...'%2C%20%0A%40mailserver_name%20%3D%20'%24(mailserver)'%20%2C%20%0A%40username%20%3D%20'%24(loginEmail)'%20%2C%20%0A%40password%20%3D%20'%24(password)'%20%0A%0A--%20Create%20a%20Database%20Mail%20profile%20%0AEXECUTE%20msdb.dbo.sysmail_add_profile_sp%20%0A%40profile_name%20%3D%20'AzureManagedInstance_dbmail_profile'%2C%20%0A%40description%20%3D%20'...'%20%3B%20%0A%0A--%20Add%20the%20account%20to%20the%20profile%20%0AEXECUTE%20msdb.dbo.sysmail_add_profileaccount_sp%20%0A%40profile_name%20%3D%20'AzureManagedInstance_dbmail_profile'%2C%20%0A%40account_name%20%3D%20'...'%2C%20%0A%40sequence_number%20%3D%201%3B%20%3C%2FPRE%3E%0A%3CP%3E%3CBR%20%2F%3EThe%20important%20thing%20is%20that%20you%20can%20use%20any%20name%20for%20the%20DbMail%20profile%20(and%20you%20can%20have%20several%20db%20Mail%20profiles)%20for%20Db%20Mail%20procedures.%20However%2C%20if%20you%20want%20to%20send%20e-mail%20using%20SQL%20Agent%20jobs%2C%20there%20should%20be%20a%20profile%20that%20must%20be%20called%20'%3CSTRONG%3EAzureManagedInstance_dbmail_profile%3C%2FSTRONG%3E'.%20Otherwise%2C%20Managed%20Instance%20will%20be%20unable%20to%20send%20emails%20via%20SQL%20Agent.%20If%20you%20are%20using%20one%26nbsp%3B%20profile%20in%20your%20instance%20and%20you%20want%20to%20use%20it%20both%20for%20classic%20emails%20and%20SQL%20Agent%2C%20rename%20the%20profile%20to%26nbsp%3B%3CSPAN%20style%3D%22display%3A%20inline%20!important%3B%20float%3A%20none%3B%20background-color%3A%20%23ffffff%3B%20color%3A%20%23333333%3B%20cursor%3A%20text%3B%20font-family%3A%20inherit%3B%20font-size%3A%2016px%3B%20font-style%3A%20normal%3B%20font-variant%3A%20normal%3B%20font-weight%3A%20300%3B%20letter-spacing%3A%20normal%3B%20line-height%3A%201.7142%3B%20orphans%3A%202%3B%20text-align%3A%20left%3B%20text-decoration%3A%20none%3B%20text-indent%3A%200px%3B%20text-transform%3A%20none%3B%20-webkit-text-stroke-width%3A%200px%3B%20white-space%3A%20normal%3B%20word-spacing%3A%200px%3B%22%3E'%3C%2FSPAN%3E%3CSTRONG%20style%3D%22box-sizing%3A%20border-box%3B%20color%3A%20%23333333%3B%20font-family%3A%20%26amp%3Bquot%3B%20segoeui%26amp%3Bquot%3B%2C%26amp%3Bquot%3Blato%26amp%3Bquot%3B%2C%26amp%3Bquot%3Bhelvetica%20neue%26amp%3Bquot%3B%2Chelvetica%2Carial%2Csans-serif%3B%20font-size%3A%2016px%3B%20font-style%3A%20normal%3B%20font-variant%3A%20normal%3B%20font-weight%3A%20bold%3B%20letter-spacing%3A%20normal%3B%20orphans%3A%202%3B%20text-align%3A%20left%3B%20text-decoration%3A%20none%3B%20text-indent%3A%200px%3B%20text-transform%3A%20none%3B%20-webkit-text-stroke-width%3A%200px%3B%20white-space%3A%20normal%3B%20word-spacing%3A%200px%3B%22%3EAzureManagedInstance_dbmail_profile%3C%2FSTRONG%3E%3CSPAN%20style%3D%22display%3A%20inline%20!important%3B%20float%3A%20none%3B%20background-color%3A%20%23ffffff%3B%20color%3A%20%23333333%3B%20cursor%3A%20text%3B%20font-family%3A%20inherit%3B%20font-size%3A%2016px%3B%20font-style%3A%20normal%3B%20font-variant%3A%20normal%3B%20font-weight%3A%20300%3B%20letter-spacing%3A%20normal%3B%20line-height%3A%201.7142%3B%20orphans%3A%202%3B%20text-align%3A%20left%3B%20text-decoration%3A%20none%3B%20text-indent%3A%200px%3B%20text-transform%3A%20none%3B%20-webkit-text-stroke-width%3A%200px%3B%20white-space%3A%20normal%3B%20word-spacing%3A%200px%3B%22%3E'%20so%20it%20can%20be%20used%20on%20both%20places.%3C%2FSPAN%3E%3CBR%20%2F%3E%3CBR%20%2F%3EThen%2C%20you%20would%20need%20to%20enable%20Database%20email%20extended%20procedure%20using%20Database%20Mail%20XPs%20configuration%20option%3A%20%3C%2FP%3E%0A%3CPRE%3EEXEC%20sp_configure%20'show%20advanced%20options'%2C%201%3B%20%0AGO%20%0ARECONFIGURE%3B%20%0AGO%20%0AEXEC%20sp_configure%20'Database%20Mail%20XPs'%2C%201%3B%20%0AGO%20%0ARECONFIGURE%20%0AGO%20%3C%2FPRE%3E%0A%3CP%3ENow%20you%20can%20test%20the%20configuration%20by%20sending%20emails%20using%20sp_send%20and%20sp_notify_operator%20procedures.%3C%2FP%3E%0A%3CH2%20id%3D%22toc-hId--842833915%22%20id%3D%22toc-hId--842833915%22%3ESending%20emails%3C%2FH2%3E%0A%3CP%3E%3CBR%20%2F%3Esp_send%20procedure%20enables%20you%20to%20send%20email%20messages%20directly%20to%20the%20specified%20email%20address.%20An%20example%20of%26nbsp%3B%20the%20code%20that%20sends%20an%20email%20message%20is%20shown%20in%20the%20following%20listing%3A%20%3C%2FP%3E%0A%3CPRE%3EDECLARE%20%40body%20VARCHAR(4000)%20%3D%20'The%20email%20is%20sent%20with%20msdb.dbo.sp_send_dbmail%20from%20'%20%2B%20%40%40SERVERNAME%3B%20%0AEXEC%20msdb.dbo.sp_send_dbmail%20%0A%40profile_name%20%3D%20'AzureManagedInstance_dbmail_profile'%2C%20%0A%40recipients%20%3D%20'%24(email)'%2C%20%0A%40body%20%3D%20%40body%2C%20%0A%40subject%20%3D%20'Azure%20SQL%20Instance%20-%20test%20email'%20%3B%20%3C%2FPRE%3E%0A%3CP%3EIf%20everything%20is%20fine%2C%20you%20will%20get%20the%20email%20on%20the%20%24(email)%20address.%3C%2FP%3E%0A%3CH2%20id%3D%22toc-hId-899976420%22%20id%3D%22toc-hId-899976420%22%3ESending%20email%20notifications%20to%20operators%3C%2FH2%3E%0A%3CP%3E%3CBR%20%2F%3EYou%20can%20also%20define%20the%20operators%20with%20assigned%20email%20addresses%20and%20send%20emails%20to%20them%20using%20sp_notify_operator%20procedure.%20First%2C%20you%20need%20to%20add%20an%20operator%20and%20specify%20his%20email%20address%3A%20%3C%2FP%3E%0A%3CPRE%3EEXEC%20msdb.dbo.sp_add_operator%20%0A%40name%20%3D%20N'SQL%20DevOp'%2C%20%0A%40enabled%20%3D%201%2C%20%0A%40email_address%20%3D%20N'%24(email)'%2C%20%0A%40weekday_pager_start_time%20%3D%20080000%2C%20%0A%40weekday_pager_end_time%20%3D%20170000%2C%20%0A%40pager_days%20%3D%2062%20%3B%20%3C%2FPRE%3E%0A%3CP%3EThen%2C%20you%20can%20send%20an%20email%20notification%20to%20the%20operator%3A%20%3C%2FP%3E%0A%3CPRE%3EDECLARE%20%40body%20VARCHAR(4000)%20%3D%20'The%20email%20is%20sent%20using%20sp_notify_operator%20from%20'%20%2B%20%40%40SERVERNAME%3B%20%0AEXEC%20msdb.dbo.sp_notify_operator%20%0A%40profile_name%20%3D%20N'AzureManagedInstance_dbmail_profile'%2C%20%0A%40name%20%3D%20N'SQL%20DevOp'%2C%20%0A%40subject%20%3D%20N'Azure%20SQL%20Instance%20-%20Test%20Notification'%2C%20%0A%40body%20%3D%20%40body%3B%20%3C%2FPRE%3E%0A%3CP%3E%3CBR%20%2F%3EAgain%2C%20the%20important%20thing%20here%20is%20that%20you%20need%20to%20specify%20%3CSTRONG%3E%20AzureManagedInstance_dbmail_profile%20%3C%2FSTRONG%3E%20as%20the%20email%20profile%20that%20will%20be%20used%20to%20send%20the%20notifications.%3C%2FP%3E%0A%3CH2%20id%3D%22toc-hId--1652180541%22%20id%3D%22toc-hId--1652180541%22%3EJob%20notifications%3C%2FH2%3E%0A%3CP%3E%3CBR%20%2F%3EManaged%20Instance%20enables%20you%20to%20notify%20an%20operator%20via%20email%20when%20a%20job%20succeeds%20or%20fails%20using%20the%20following%20script%3A%20%3C%2FP%3E%0A%3CPRE%3EEXEC%20msdb.dbo.sp_update_job%20%0A%40job_name%3DN'My%20job%20name'%2C%20%0A%40notify_level_email%3D2%2C%20%0A%40notify_level_page%3D2%2C%20%0A%40notify_email_operator_name%3DN'SQL%20DevOp'%20%3C%2FPRE%3E%0A%3CP%3EThis%20script%20will%20configure%20SQL%20Agent%20job%20to%20notify%20operator%20if%20the%20job%20fails.%20If%20you%20run%20a%20job%20and%20it%20fails%2C%20the%20operator%20should%20get%20the%20email.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CFONT%20size%3D%224%22%3E%3CSTRONG%3EImportant%3C%2FSTRONG%3E%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3EIf%20you%20experience%20any%20issue%20with%20emails%20-%20look%20at%20the%20article%20that%20is%20explaining%20how%20to%20troubleshoot%20%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2FAzure-SQL-Database%2FTroubleshooting-Database-Mail-issues-in-Azure-SQL-Managed%2Fba-p%2F583018%22%20target%3D%22_self%22%3EDb%20Mail%20issues%20on%20Managed%20Instance%3C%2FA%3E.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-TEASER%20id%3D%22lingo-teaser-386235%22%20slang%3D%22en-US%22%3E%3CP%3EFirst%20published%20on%20MSDN%20on%20Aug%2031%2C%202018%20Azure%20SQL%20Database%20Managed%20Instance%20enables%20you%20to%20use%20most%20of%20the%20SQL%20Server%20features%20in%20fully%20managed%20cloud%20environment.%3C%2FP%3E%3C%2FLINGO-TEASER%3E%3CLINGO-LABS%20id%3D%22lingo-labs-386235%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EInteroperability%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EManaged%20Instance%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E
Microsoft

First published on MSDN on Aug 31, 2018
Azure SQL Database Managed Instance enables you to use most of the SQL Server features in fully managed cloud environment. One of the features is database mail system that enables you to send emails directly from Managed Instance. In this post we will see how to setup mail profile and send emails in Managed Instance. 

Setup


First, you need to ensure that Managed Instance can reach your mail server. If your email server is using port 25 then you would need to open outbound NSG on port 25 to Internet. Managed Instance is placed in your VNet/subnet and you have control over the network traffic. 

 

Then you would need to setup email account information that contain address of the e-mail server that  will actually send the emails, with login/password information required to access e-mail server. In the following script set $(mailserver), $(loginEmail), and $(password) information, change titles/description shown as '...' and run it:

-- Create a Database Mail account 
EXECUTE msdb.dbo.sysmail_add_account_sp 
@account_name = '...', 
@description = '...', 
@email_address = '$(loginEmail)', 
@display_name = '...', 
@mailserver_name = '$(mailserver)' , 
@username = '$(loginEmail)' , 
@password = '$(password)' 

-- Create a Database Mail profile 
EXECUTE msdb.dbo.sysmail_add_profile_sp 
@profile_name = 'AzureManagedInstance_dbmail_profile', 
@description = '...' ; 

-- Add the account to the profile 
EXECUTE msdb.dbo.sysmail_add_profileaccount_sp 
@profile_name = 'AzureManagedInstance_dbmail_profile', 
@account_name = '...', 
@sequence_number = 1; 


The important thing is that you can use any name for the DbMail profile (and you can have several db Mail profiles) for Db Mail procedures. However, if you want to send e-mail using SQL Agent jobs, there should be a profile that must be called 'AzureManagedInstance_dbmail_profile'. Otherwise, Managed Instance will be unable to send emails via SQL Agent. If you are using one  profile in your instance and you want to use it both for classic emails and SQL Agent, rename the profile to 'AzureManagedInstance_dbmail_profile' so it can be used on both places.

Then, you would need to enable Database email extended procedure using Database Mail XPs configuration option:

EXEC sp_configure 'show advanced options', 1; 
GO 
RECONFIGURE; 
GO 
EXEC sp_configure 'Database Mail XPs', 1; 
GO 
RECONFIGURE 
GO 

Now you can test the configuration by sending emails using sp_send and sp_notify_operator procedures.

Sending emails


sp_send procedure enables you to send email messages directly to the specified email address. An example of  the code that sends an email message is shown in the following listing:

DECLARE @body VARCHAR(4000) = 'The email is sent with msdb.dbo.sp_send_dbmail from ' + @@SERVERNAME; 
EXEC msdb.dbo.sp_send_dbmail 
@profile_name = 'AzureManagedInstance_dbmail_profile', 
@recipients = '$(email)', 
@body = @body, 
@subject = 'Azure SQL Instance - test email' ; 

If everything is fine, you will get the email on the $(email) address.

Sending email notifications to operators


You can also define the operators with assigned email addresses and send emails to them using sp_notify_operator procedure. First, you need to add an operator and specify his email address:

EXEC msdb.dbo.sp_add_operator 
@name = N'SQL DevOp', 
@enabled = 1, 
@email_address = N'$(email)', 
@weekday_pager_start_time = 080000, 
@weekday_pager_end_time = 170000, 
@pager_days = 62 ; 

Then, you can send an email notification to the operator:

DECLARE @body VARCHAR(4000) = 'The email is sent using sp_notify_operator from ' + @@SERVERNAME; 
EXEC msdb.dbo.sp_notify_operator 
@profile_name = N'AzureManagedInstance_dbmail_profile', 
@name = N'SQL DevOp', 
@subject = N'Azure SQL Instance - Test Notification', 
@body = @body; 


Again, the important thing here is that you need to specify AzureManagedInstance_dbmail_profile as the email profile that will be used to send the notifications.

Job notifications


Managed Instance enables you to notify an operator via email when a job succeeds or fails using the following script:

EXEC msdb.dbo.sp_update_job 
@job_name=N'My job name', 
@notify_level_email=2, 
@notify_level_page=2, 
@notify_email_operator_name=N'SQL DevOp' 

This script will configure SQL Agent job to notify operator if the job fails. If you run a job and it fails, the operator should get the email.

 

Important

If you experience any issue with emails - look at the article that is explaining how to troubleshoot Db Mail issues on Managed Instance.