

# 使用数据库邮件发送电子邮件
<a name="SQLServer.DBMail.Send"></a>

您可以使用数据库邮件的 [sp\_send\_dbmail](https://docs.microsoft.com/en-us/sql/relational-databases/system-stored-procedures/sp-send-dbmail-transact-sql) 存储过程发送电子邮件。

## 用法
<a name="SQLServer.DBMail.Send.Usage"></a>

```
EXEC msdb.dbo.sp_send_dbmail
@profile_name = '{{profile_name}}',
@recipients = '{{recipient1@example.com}}[; {{recipient2}}; ... {{recipientn}}]',
@subject = '{{subject}}',
@body = '{{message_body}}',
[@body_format = 'HTML'],
[@file_attachments = '{{file_path1}}; {{file_path2}}; ... {{file_pathn}}'],
[@query = '{{SQL_query'}}],
[@attach_query_result_as_file = {{0|1}}]';
```

以下参数为必需参数：
+ `@profile_name` – 要从中发送消息的数据库邮件配置文件的名称。
+ `@recipients` – 要向其发送消息的电子邮件地址列表，用分号分隔。
+ `@subject` – 消息主题。
+ `@body` – 消息的正文。您也可以使用声明的变量作为主体。

以下参数为可选参数：
+ `@body_format` – 此参数与声明的变量一起使用，从而以 HTML 格式发送电子邮件。
+ `@file_attachments` – 以分号分隔的消息附件列表。文件路径必须是绝对路径。
+ `@query` – 要运行的 SQL 查询。查询结果可以作为文件附加，也可以包含在消息正文中。
+ `@attach_query_result_as_file` – 是否将查询结果附加为文件。设置为 0 表示“否”，设置为 1 表示“是”。默认值是 0。

## 示例
<a name="SQLServer.DBMail.Send.Examples"></a>

以下示例演示了如何发送电子邮件。

**Example 向单个收件人发送消息**  

```
USE msdb
GO

EXEC msdb.dbo.sp_send_dbmail
     @profile_name       = 'Notifications',
     @recipients         = 'nobody@example.com',
     @subject            = 'Automated DBMail message - 1',
     @body               = 'Database Mail configuration was successful.';
GO
```

**Example 向多个收件人发送消息**  

```
USE msdb
GO

EXEC msdb.dbo.sp_send_dbmail
     @profile_name       = 'Notifications',
     @recipients         = 'recipient1@example.com;recipient2@example.com',
     @subject            = 'Automated DBMail message - 2',
     @body               = 'This is a message.';
GO
```

**Example 将 SQL 查询结果作为文件附件发送**  

```
USE msdb
GO

EXEC msdb.dbo.sp_send_dbmail
     @profile_name       = 'Notifications',
     @recipients         = 'nobody@example.com',
     @subject            = 'Test SQL query',
     @body               = 'This is a SQL query test.',
     @query              = 'SELECT * FROM abc.dbo.test',
     @attach_query_result_as_file = 1;
GO
```

**Example 以 HTML 格式发送消息**  

```
USE msdb
GO

DECLARE @HTML_Body as NVARCHAR(500) = 'Hi, <h4> Heading </h4> </br> See the report. <b> Regards </b>';

EXEC msdb.dbo.sp_send_dbmail
     @profile_name       = 'Notifications',
     @recipients         = 'nobody@example.com',
     @subject            = 'Test HTML message',
     @body               = @HTML_Body,
     @body_format        = 'HTML';
GO
```

**Example 数据库中发生特定事件时，使用触发器发送消息**  

```
USE AdventureWorks2017
GO
IF OBJECT_ID ('Production.iProductNotification', 'TR') IS NOT NULL
DROP TRIGGER Purchasing.iProductNotification
GO

CREATE TRIGGER iProductNotification ON Production.Product
   FOR INSERT
   AS
   DECLARE @ProductInformation nvarchar(255);
   SELECT
   @ProductInformation = 'A new product, ' + Name + ', is now available for $' + CAST(StandardCost AS nvarchar(20)) + '!'
   FROM INSERTED i;

EXEC msdb.dbo.sp_send_dbmail
     @profile_name       = 'Notifications',
     @recipients         = 'nobody@example.com',
     @subject            = 'New product information',
     @body               = @ProductInformation;
GO
```