跳至內容

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

3 分的閱讀內容
0

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

解決方法

設定 Amazon SES

使用 Amazon SES 主控台來取得簡易郵件傳輸通訊協定 (SMTP) 憑證。

若要接收確認電子郵件,請完成以下步驟:

  1. 開啟 Amazon SES console (Amazon SES 主控台),然後選擇 Email Addresses (電子郵件地址)。
  2. 選擇 Verify a new email address (驗證新的電子郵件地址),然後輸入您要用來接收確認電子郵件的電子郵件地址。
  3. 確認 Verification status (驗證狀態) 為已驗證。

設定 Database Mail 參數群組

注意:如果您已經有可搭配 Database Mail 使用的自訂參數群組,請前往設定 Database Mail 區段。如果您的參數群組已經與您的 RDS for SQL Server 執行個體建立關聯,請前往本節的下一組步驟。

若要建立自訂參數群組,請完成以下步驟:

  1. 開啟 Aurora and RDS console (Aurora 和 RDS 主控台)。
  2. 選擇 Parameter groups (參數群組),然後選擇 Create parameter group (建立參數群組)。
  3. 在 Parameter group family (參數群組系列),選擇您使用的 SQL Server 版本和版本類型。例如,SQL Server 2016 Enterprise Edition 使用 sqlserver-ee-13.0.
  4. 輸入群組名稱和描述,然後選擇 Create (建立)。
  5. 在 Parameter groups (參數群組) 頁面上,選擇該群組。
  6. 選擇 Edit parameters (編輯參數),然後選取 database mail xps。
  7. 在 Values (值),選擇 1。
  8. 選擇 Save (儲存)。

若要將參數群組與您的 RDS for SQL Server 執行個體建立關聯,請完成以下步驟:

  1. 開啟 Aurora and RDS console (Aurora 和 RDS 主控台)。
  2. 選擇 Database (資料庫),然後選擇 Modify (修改)。
  3. 在 Database options (資料庫選項) 下,選擇參數群組,然後選擇 Continue (繼續)。
  4. 在 Scheduling of modifications (修改排程) 下,選擇 Immediately (立即)。
  5. 選擇 Modify DB Instance (修改資料庫執行個體) 來套用變更,然後重新啟動您的執行個體。

設定 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.Exception Message: Could not connect to mail server.」

如果您收到前述錯誤訊息,代表您的資料庫執行個體無法連線到郵件伺服器。若要解決此問題,請為 Amazon SES 建立 Amazon Virtual Private Cloud (Amazon VPC) 端點。

請完成以下步驟:

  1. 識別您執行個體的私有 IP 位址。

  2. 建立新的安全群組,並在其中加入一條傳入規則,指向與您執行個體相關聯的安全群組: 
    在 Type (類型),選擇 Custom TCP (自訂 TCP)。
    在 Port range (連接埠範圍),輸入您要用來傳送電子郵件的連接埠號碼。您可以使用 25、465、587、2465 或 2587。
    在 Source type (來源類型),選擇 Custom (自訂)。
    在 Source (來源),輸入您 RDS for SQL Server 執行個體的私有 IP 位址。

  3. 建立 VPC 端點。

  4. 選擇端點,然後記下 DNS 欄位下的第一個名稱。

  5. 重新建立 Database Mail 帳戶:

    use msdbgo
    
    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 替換為您從 DNS 欄位記下的 DNS 名稱。

  6. 執行以下命令來確認 Database Mail 已正確設定:

    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';
    

    **注意:**如果 Database Mail 已正確設定,電子郵件就會傳送。

  7. 執行以下預存程序來驗證所有電子郵件項目:

    SELECT * FROM msdb.dbo.sysmail_allitems
    
  8. 在 sent_status 欄中,確認狀態為已傳送。

相關資訊

管理 Amazon RDS Custom for SQL Server 資料庫執行個體

在 Amazon RDS for SQL Server 上使用 Database Mail

AWS 官方已更新 2 年前