

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# 使用內部部署 SMTP 伺服器和 Database Mail 傳送 Amazon RDS for SQL Server 資料庫執行個體的通知
<a name="send-notifications-for-an-amazon-rds-for-sql-server-database-instance-by-using-an-on-premises-smtp-server-and-database-mail"></a>

*Nishad Mankar，Amazon Web Services*

## 總結
<a name="send-notifications-for-an-amazon-rds-for-sql-server-database-instance-by-using-an-on-premises-smtp-server-and-database-mail-summary"></a>

[Database Mail](https://learn.microsoft.com/en-us/sql/relational-databases/database-mail/database-mail?view=sql-server-ver16) (Microsoft 文件） 使用 Simple Mail Transfer Protocol (SMTP) 伺服器，從 Microsoft SQL Server 資料庫傳送電子郵件訊息，例如通知或提醒。Amazon Relational Database Service (Amazon RDS) for Microsoft SQL Server 文件提供使用 Amazon Simple Email Service (Amazon SES) 做為 Database Mail SMTP 伺服器的說明。如需詳細資訊，請參閱[在 Amazon RDS for SQL Server 上使用 Database Mail](https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/SQLServer.DBMail.html)。做為替代組態，此模式說明如何使用內部部署 SMTP 伺服器做為郵件伺服器，將 Database Mail 設定為從 Amazon RDS for SQL Server 資料庫 (DB) 執行個體傳送電子郵件。

## 先決條件和限制
<a name="send-notifications-for-an-amazon-rds-for-sql-server-database-instance-by-using-an-on-premises-smtp-server-and-database-mail-prereqs"></a>

**先決條件**
+ 作用中的 AWS 帳戶
+ 執行 SQL Server 標準版或企業版的 Amazon RDS 資料庫執行個體
+ 內部部署 SMTP 伺服器的 IP 地址或主機名稱
+ 傳入[安全群組規則](https://docs.aws.amazon.com/vpc/latest/userguide/security-group-rules.html#working-with-security-group-rules)，允許從 SMTP 伺服器的 IP 地址連線至 Amazon RDS for SQL Server 資料庫執行個體
+ 內部部署網路與包含 Amazon RDS 資料庫執行個體的虛擬私有雲端 (VPC) 之間的連線，例如 [AWS Direct Connect](https://docs.aws.amazon.com/directconnect/latest/UserGuide/Welcome.html) 連線

**限制**
+ 不支援 SQL Server 的 Express 版本。
+ 如需限制的詳細資訊，請參閱《*Amazon RDS 文件》中的在 Amazon RDS for SQL Server 上使用資料庫郵件*中的[限制](https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/CHAP_SQLServer.html#SQLServer.Concepts.General.FeatureSupport.Limits)。

**產品版本**
+ [RDS 中支援的 SQL Server 版本](https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/CHAP_SQLServer.html#SQLServer.Concepts.General.VersionSupport)標準版和企業版

## Architecture
<a name="send-notifications-for-an-amazon-rds-for-sql-server-database-instance-by-using-an-on-premises-smtp-server-and-database-mail-architecture"></a>

**目標技術堆疊**
+ Amazon RDS for SQL Server 資料庫執行個體
+ Amazon Route 53 轉送規則
+ 資料庫郵件
+ 內部部署 SMTP 伺服器
+ Microsoft SQL Server Management Studio (SSMS)

**目標架構**

下圖顯示此模式的目標架構。發生事件或動作啟動有關資料庫執行個體的通知或提醒時，Amazon RDS for SQL Server 會使用 Database Mail 傳送電子郵件通知。Database Mail 使用內部部署 SMTP 伺服器來傳送電子郵件。

![\[Amazon RDS for SQL Server 使用內部部署 SMTP 伺服器傳送電子郵件通知給使用者。\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/images/pattern-img/e5599724-43cf-4fe1-8c5a-8fca1a424993/images/47efb12f-3505-4a60-ac43-194a176e71c8.png)


## 工具
<a name="send-notifications-for-an-amazon-rds-for-sql-server-database-instance-by-using-an-on-premises-smtp-server-and-database-mail-tools"></a>

**AWS 服務**
+ [適用於 Microsoft SQL Server 的 Amazon Relational Database Service (Amazon RDS)](https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/CHAP_SQLServer.html) 可協助您在 AWS 雲端中設定、操作和擴展 SQL Server 關聯式資料庫。
+ [Amazon Route 53](https://docs.aws.amazon.com/Route53/latest/DeveloperGuide/Welcome.html) 是一種可用性高、可擴展性強的 DNS Web 服務。

**其他工具**
+ [Database Mail](https://learn.microsoft.com/en-us/sql/relational-databases/database-mail/database-mail) 是一種工具，可將通知和提醒等電子郵件訊息從 SQL Server 資料庫引擎傳送給使用者。
+ [Microsoft SQL Server Management Studio (SSMS)](https://docs.microsoft.com/en-us/sql/ssms/sql-server-management-studio-ssms) 是一種用於管理 SQL Server 的工具，包括存取、設定和管理 SQL Server 元件。在此模式中，您可以使用 SSMS 執行 SQL 命令，在 Amazon RDS for SQL Server 資料庫執行個體上設定 Database Mail。 

## 史詩
<a name="send-notifications-for-an-amazon-rds-for-sql-server-database-instance-by-using-an-on-premises-smtp-server-and-database-mail-epics"></a>

### 啟用與內部部署 SMTP 伺服器的網路連線
<a name="enable-network-connectivity-with-the-on-premises-smtp-server"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 從 RDS 資料庫執行個體移除異地同步備份。 | 如果您使用的是多區域 RDS 資料庫執行個體，請將多可用區域執行個體轉換為單一可用區域執行個體。當您完成設定 Database Mail 後，會將資料庫執行個體轉換回異地同步備份部署。Database Mail 組態接著可在主要節點和次要節點中運作。如需說明，請參閱[從 Microsoft SQL Server 資料庫執行個體移除異地](https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/USER_SQLServerMultiAZ.html#USER_SQLServerMultiAZ.Removing)同步備份。 | DBA | 
| 在內部部署 SMTP 伺服器上建立 Amazon RDS 端點或 IP 地址的允許清單。 | SMTP 伺服器位於 AWS 網路之外。在內部部署 SMTP 伺服器上，建立允許清單，允許伺服器與 Amazon RDS 執行個體或 Amazon RDS 上託管的 Amazon Elastic Compute Cloud (Amazon EC2) 執行個體的傳出端點或 IP 地址進行通訊。此程序因組織而異。如需資料庫執行個體端點的詳細資訊，請參閱[尋找資料庫執行個體端點和連接埠號碼](https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/USER_ConnectToMicrosoftSQLServerInstance.html#sqlserver-endpoint)。 | DBA | 
| 移除連接埠 25 限制。 | 根據預設，AWS 會限制 EC2 執行個體上的連接埠 25。若要移除連接埠 25 限制，請執行下列動作：[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/send-notifications-for-an-amazon-rds-for-sql-server-database-instance-by-using-an-on-premises-smtp-server-and-database-mail.html)[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/send-notifications-for-an-amazon-rds-for-sql-server-database-instance-by-using-an-on-premises-smtp-server-and-database-mail.html) | 一般 AWS | 
| 新增 Route 53 規則來解析 SMTP 伺服器的 DNS 查詢。 | 使用 Route 53 解析 AWS 資源與內部部署 SMTP 伺服器之間的 DNS 查詢。您必須建立將 DNS 查詢轉送至 SMTP 伺服器網域的規則，例如 `example.com`。如需說明，請參閱 Route 53 文件中的[建立轉送規則](https://docs.aws.amazon.com/Route53/latest/DeveloperGuide/resolver-rules-managing.html#resolver-rules-managing-creating-rules)。 | 網路管理員 | 

### 在 Amazon RDS for SQL Server 資料庫執行個體上設定 Database Mail
<a name="set-up-database-mail-on-the-amazon-rds-for-sql-server-db-instance"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 啟用資料庫郵件。 | 建立 Database Mail 的參數群組，將 `database mail xps` 參數設定為 `1`，然後將 Database Mail 參數群組與目標 RDS 資料庫執行個體建立關聯。如需說明，請參閱 Amazon RDS 文件中的[啟用資料庫郵件](https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/SQLServer.DBMail.html#SQLServer.DBMail.Enable)。請勿繼續進行這些說明中的*設定資料庫郵件*一節。內部部署 SMTP 伺服器的組態與 Amazon SES 不同。 | DBA | 
| 連線到資料庫執行個體。 | 從堡壘主機，使用 Microsoft SQL Server Management Studio (SSMS) 連線至 Amazon RDS for SQL Server 資料庫執行個體。如需說明，請參閱[連線至執行 Microsoft SQL Server 資料庫引擎的資料庫執行個體](https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/USER_ConnectToMicrosoftSQLServerInstance.html)。如果您遇到任何錯誤，請參閱[相關資源](#send-notifications-for-an-amazon-rds-for-sql-server-database-instance-by-using-an-on-premises-smtp-server-and-database-mail-resources)區段中的連線故障診斷參考。 | DBA | 
| 建立設定檔。 | 在 SSMS 中，輸入下列 SQL 陳述式來建立 Database Mail 設定檔。取代以下的值：[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/send-notifications-for-an-amazon-rds-for-sql-server-database-instance-by-using-an-on-premises-smtp-server-and-database-mail.html)如需此預存程序及其引數的詳細資訊，請參閱 Microsoft 文件中的 [sysmail\$1add\$1profile\$1sp](https://learn.microsoft.com/en-us/sql/relational-databases/system-stored-procedures/sysmail-add-profile-sp-transact-sql)。<pre>EXECUTE msdb.dbo.sysmail_add_profile_sp<br /> @profile_name = 'SQL Alerts profile',<br /> @description = 'Profile used for sending outgoing notifications using OM SMTP Server.';</pre> | DBA | 
| 將主體新增至設定檔。 | 輸入下列 SQL 陳述式，將公有或私有主體新增至 Database Mail 設定檔。*主體*是可以要求 SQL Server 資源的實體。取代以下的值：[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/send-notifications-for-an-amazon-rds-for-sql-server-database-instance-by-using-an-on-premises-smtp-server-and-database-mail.html)如需此預存程序及其引數的詳細資訊，請參閱 Microsoft 文件中的 [sysmail\$1add\$1principalprofile\$1sp](https://learn.microsoft.com/en-us/sql/relational-databases/system-stored-procedures/sysmail-add-principalprofile-sp-transact-sql)。<pre>EXECUTE msdb.dbo.sysmail_add_principalprofile_sp<br /> @profile_name = 'SQL Alerts profile',<br /> @principal_name = 'public',<br /> @is_default = 1 ;</pre> | DBA | 
| 建立 帳戶。 | 輸入下列 SQL 陳述式來建立 Database Mail 帳戶。取代以下的值：[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/send-notifications-for-an-amazon-rds-for-sql-server-database-instance-by-using-an-on-premises-smtp-server-and-database-mail.html)如需此預存程序及其引數的詳細資訊，請參閱 Microsoft 文件中的 [sysmail\$1add\$1account\$1sp](https://learn.microsoft.com/en-us/sql/relational-databases/system-stored-procedures/sysmail-add-account-sp-transact-sql)。<pre>EXECUTE msdb.dbo.sysmail_add_account_sp<br /> @account_name = 'SQL Alerts account',<br /> @description = 'Database Mail account for sending outgoing notifications.',<br /> @email_address = 'xyz@example.com',<br /> @display_name = 'xyz@example.com',<br /> @mailserver_name = 'test_smtp.example.com',<br /> @port = 25,<br /> @enable_ssl = 1,<br /> @username = 'SMTP-username',<br /> @password = 'SMTP-password';</pre> | DBA | 
| 將帳戶新增至設定檔。 | 輸入下列 SQL 陳述式，將 Database Mail 帳戶新增至 Database Mail 設定檔。取代以下的值：[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/send-notifications-for-an-amazon-rds-for-sql-server-database-instance-by-using-an-on-premises-smtp-server-and-database-mail.html)如需此預存程序及其引數的詳細資訊，請參閱 Microsoft 文件中的 [sysmail\$1add\$1profileaccount\$1sp](https://learn.microsoft.com/en-us/sql/relational-databases/system-stored-procedures/sysmail-add-profileaccount-sp-transact-sql)。<pre>EXECUTE msdb.dbo.sysmail_add_profileaccount_sp<br /> @profile_name = 'SQL Alerts profile',<br /> @account_name = 'SQL Alerts account',<br /> @sequence_number = 1;</pre> | DBA | 
| （選用） 將異地同步備份新增至 RDS 資料庫執行個體。 | 如果您想要使用資料庫鏡像 (DBM) 或 Always On 可用性群組 (AGs) 新增異地同步備份，請參閱[將異地同步備份新增至 Microsoft SQL Server 資料庫執行個體](https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/USER_SQLServerMultiAZ.html#USER_SQLServerMultiAZ.Adding)中的指示。 | DBA | 

## 相關資源
<a name="send-notifications-for-an-amazon-rds-for-sql-server-database-instance-by-using-an-on-premises-smtp-server-and-database-mail-resources"></a>
+ [在 Amazon RDS for SQL Server 上使用資料庫郵件](https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/SQLServer.DBMail.html) (Amazon RDS 文件）
+ [使用檔案附件](https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/SQLServer.DBMail.html#SQLServer.DBMail.MAZ) (Amazon RDS 文件）
+ [針對 SQL Server 資料庫執行個體的連線進行故障診斷 ](https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/USER_ConnectToMicrosoftSQLServerInstance.html#USER_ConnectToMicrosoftSQLServerInstance.Troubleshooting)(Amazon RDS 文件）
+ [無法連線至 Amazon RDS 資料庫執行個體 ](https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/CHAP_Troubleshooting.html#CHAP_Troubleshooting.Connecting)(Amazon RDS 文件）