當我的 RDS for SQL Server 執行個體位於公有或私有子網路中時,如何使用 Amazon SES 功能傳送電子郵件?

2 分的閱讀內容
0

我想使用 Amazon Simple Email Service (Amazon SES) 功能傳送電子郵件。我想對託管在私有或公有子網路中的 SQL Server 執行個體使用 Amazon Relational Database Service (Amazon RDS)。

簡短說明

若要使用 Amazon RDS for SQL Server 資料庫執行個體透過 Amazon SES 傳送電子郵件,請先使用 Amazon SES 建立簡易郵件通訊傳輸協定 (SMTP) 憑證。然後,建立要搭配 Database Mail 使用的參數群組。如果您的資料庫執行個體託管在私有子網路中,請設定要在 Database Mail 設定檔中使用的 VPC 端點。

解決方法

設定 Amazon SES

1.    開啟 Amazon SES 主控台,然後選擇 SMTP 設定

2.    記下列出的伺服器名稱連接埠值。

3.    選擇建立 SMTP 憑證。這會開啟 AWS Identity and Access Management (IAM) 主控台。

4.    對於 IAM 使用者名稱,請輸入使用者或繼續使用預設使用者,然後選擇建立

5.    選擇下載憑證。這是您唯一可以下載 SMTP 憑證的時間,因此請務必將其儲存在安全的地方。

6.    開啟 Amazon SES 主控台,然後選擇電子郵件地址

7.    選擇驗證新的電子郵件地址,然後輸入您要接收確認電子郵件的電子郵件地址。

8.    確認電子郵件地址後,請確認您的驗證狀態已驗證

設定 Database Mail 參數群組

如果您已經有自訂參數群組可與 Database Mail 設定檔搭配使用,請略過此步驟。

1.    開啟 Amazon RDS 主控台,然後從導覽窗格中選擇參數群組

2.    選擇建立參數群組

3.    對於參數群組系列,請選擇您正在使用的 SQL Server 版本。例如,SQL Server 2016 Enterprise Edition 使用 sqlserver-ee-13.0

4.    輸入群組名稱說明,然後選擇建立

5.    在參數群組頁面上,選擇您建立的群組。

6.    選擇編輯參數,然後核取資料庫郵件 xps 旁邊的方塊。

7.    對於,請選擇 1

8.    儲存您的變更。

注意: 如果參數群組已經與您的 RDS for SQL Server 執行個體相關聯,請略過這些步驟。

9.    在 Amazon RDS 主控台中,選擇資料庫,然後選擇修改

10.    在資料庫選項下,選擇您先前設定的參數,然後選擇繼續

11.    在修改排程下,選擇立即

12.    選擇修改資料庫執行個體以套用變更,然後重新啟動執行個體。

設定 Database Mail

如果您的 RDS for SQL Server 執行個體託管在公用子網路中,請依照設定 Database Mail 中的步驟執行。

但是,如果您的執行個體位於私有子網路中,則在設定 Database Mail 時可能會看到此錯誤:

「The mail could not be sent to the recipients because of the mail server failure.(Sending Mail using Account 1 (2020-12-08T22:20:51).Exception Message: Could not connect to mail server.(A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond 52.63.118.124:587).」

此錯誤表示您的資料庫執行個體無法連線到郵件伺服器。若要解決此問題,請為 Amazon SES 建立 VPC 端點。

1.    識別執行個體的私有 IP

2.    建立新的安全性群組,其中包含與您執行個體相關聯之安全性群組的輸入規則**。對於類型**,請選擇自訂 TCP。在連接埠範圍中,輸入您要用來傳送電子郵件的通訊埠號碼。您可以使用 25、465、587、2465 或 2587。為來源類型選擇自訂。為來源輸入 RDS for SQL Server 執行個體的 私人 IP。

3.    建立 VPC 端點。VPC 端點處於可用狀態後,選擇端點,然後複製在 DNS 欄位下找到的第一個項目。

4.    重新建立 Database Mail 帳戶:

use msdb
go

EXECUTE msdb.dbo.sysmail_add_account_sp
@account_name = 'Acc1',
@description = 'Mail account for sending outgoing
notifications.',
@email_address = 'example@example.com',
@display_name = 'Automated Mailer',
@mailserver_name =
'vpce-0a9cxxxxxxxxxxxxx-xxxxxxxx.email-smtp.ap-southeast-2.vpce.amazonaws.com',
<---- VPC endpoint created in previous step
@port = 587,
@enable_ssl = 1,
@username = 'SMTP-username', <---- For user name and
password, please use the SES credentials
@password = 'SMTP-password' ;

**注意:**將 @mailserver\ _name 取代為您在建立 VPC 端點時取得的 DNS。

5.    如果您正確設定了 Database Mail,則在執行此 SQL 命令時,電子郵件傳送成功。

EXEC msdb.dbo.sp_send_dbmail @profile_name = 'Notifications', @recipients = 'success@simulator.amazonses.com',
@body = 'The database mail configuration was completed successfully.',
@subject = 'Automated Success Message';

6.    接下來,執行此存儲過程以列出並驗證所有電子郵件項目。在 sent_status 欄中,確認已傳送狀態。

SELECT * FROM msdb.dbo.sysmail_allitems

相關資訊

管理適用於 SQL Server 資料庫執行個體的 Amazon RDS Custom

在 Amazon RDS for SQL Server 上使用 Database Mail

AWS 官方
AWS 官方已更新 2 年前