Below are the setup steps:
-- 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;
Then enable Database Mail XP on the server:
EXEC sp_configure 'show advanced options', 1;
GO
RECONFIGURE;
GO
EXEC sp_configure 'Database Mail XPs', 1;
GO
RECONFIGURE
GO
Then you can send emails as shown below:
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' ;