Azure VM (IaaS) 上托管的 SQL Server 是否支持数据库邮件?

sac*_*iee 2 sql-server azure-sql-database azure-vm azure

Azure VM (IaaS) 上托管的 SQL Server 中是否存在数据库邮件?

如果不支持 DB 邮件,那么可以使用哪些功能?

Alb*_*llo 5

数据库邮件可在 Azure IaaS 上的 SQL Server 上使用,我的建议是使用 SendGrid 电子邮件传送服务(在 Azure Marketplace 上提供)来配置它,该服务为虚拟机提供 SMTP 中继。利用此服务来设置数据库邮件以满足您的警报或报告需求。

您将在此处了解如何在 Microsoft Azure 上设置 SendGrid 服务。

设置 SendGrid 服务后,您可以在 SQL Server VM 上对其进行配置,如下所示。

首先在 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
Run Code Online (Sandbox Code Playgroud)

接下来,创建一个电子邮件帐户:

/*
   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
Run Code Online (Sandbox Code Playgroud)

此外,您还需要配置邮件配置文件。

/*
   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
Run Code Online (Sandbox Code Playgroud)

下一步是将电子邮件帐户链接到个人资料:

/*
  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';
Run Code Online (Sandbox Code Playgroud)

下一步,配置数据库邮件以允许公众访问邮件配置文件。这允许数据库用户发送邮件。

/*
   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
Run Code Online (Sandbox Code Playgroud)

最后使用下面的代码来验证这个解决方案。

/*
   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
Run Code Online (Sandbox Code Playgroud)