管理 Amazon SES 身分 - AWS SDK for JavaScript

我們宣布了即將推 end-of-support 出的 AWS SDK for JavaScript v2。我們建議您移轉至 AWS SDK for JavaScript v3。有關日期,其他詳細信息以及如何遷移的信息,請參閱鏈接的公告。

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

管理 Amazon SES 身分

JavaScript code example that applies to Node.js execution

這個 Node.js 程式碼範例會說明:

  • 如何驗證搭配 Amazon SES 使用的電子郵件地址和網域。

  • 如何為您的 Amazon SES 身分指派 IAM 政策。

  • 如何列出您AWS帳戶的所有 Amazon SES 身份。

  • 如何刪除與 Amazon SES 一起使用的身份。

Amazon SES 身分識別是 Amazon SES 用來傳送電子郵件的電子郵件地址或網域。Amazon SES 要求您驗證電子郵件身分,確認您擁有這些身分並防止其他人使用它們。

如需如何在 Amazon SES 中驗證電子郵件地址和網域的詳細資訊,請參閱 Amazon 簡單電子郵件服務開發人員指南中的 Amazon SES 驗證電子郵件地址和網域。如需在 Amazon SES 中傳送授權的相關資訊,請參閱 Amazon SES 傳送授權概觀

使用案例

在此範例中,您會使用一系列 Node.js 模組來驗證和管理 Amazon SES 身分識別。Node.js 模組會使用 SDK JavaScript 來驗證電子郵件地址和網域,使用用AWS.SES戶端類別的下列方法:

先決條件任務

若要設定和執行此範例,您必須先完成這些任務:

設定軟體開發套件

JavaScript 通過創建全局配置對象,然後為代碼設置區域來配置 SDK。在此範例中,區域會設為 us-west-2

// Load the SDK for JavaScript var AWS = require('aws-sdk'); // Set the Region AWS.config.update({region: 'us-west-2'});

列出您的身分

在此範例中,使用 Node.js 模組列出要搭配 Amazon SES 使用的電子郵件地址和網域。以檔名 ses_listidentities.js 建立一個 Node.js 模組。依前述內容設定軟體開發套件。

建立物件,以傳遞 AWS.SES 用戶端類別的 listIdentities 方法之 IdentityType 和其他參數。若要呼叫方listIdentities法,請建立用於叫用 Amazon SES 服務物件並傳遞參數物件的承諾。

然後,在 promise 回呼中處理 response。由 promise 傳回的 data 包含了一組網域身分的陣列,如 IdentityType 參數中所指定。

// Load the AWS SDK for Node.js var AWS = require("aws-sdk"); // Set region AWS.config.update({ region: "REGION" }); // Create listIdentities params var params = { IdentityType: "Domain", MaxItems: 10, }; // Create the promise and SES service object var listIDsPromise = new AWS.SES({ apiVersion: "2010-12-01" }) .listIdentities(params) .promise(); // Handle promise's fulfilled/rejected states listIDsPromise .then(function (data) { console.log(data.Identities); }) .catch(function (err) { console.error(err, err.stack); });

若要執行範例,請在命令列中輸入以下內容。

node ses_listidentities.js

您可以在這裡找到此範例程式碼 GitHub。

驗證電子郵件地址身分

在此範例中,請使用 Node.js 模組來驗證要與 Amazon SES 搭配使用的電子郵件寄件者。以檔名 ses_verifyemailidentity.js 建立一個 Node.js 模組。依前述內容設定軟體開發套件。若要存取 Amazon SES,請建立AWS.SES服務物件。

建立物件以傳遞 AWS.SES 用戶端類別的 verifyEmailIdentity 方法之 EmailAddress 參數。若要呼叫 verifyEmailIdentity 方法,請建立叫用 Amazon SES 服務物件的 promise 來傳遞參數。然後,在 promise 回呼中處理 response

// Load the AWS SDK for Node.js var AWS = require("aws-sdk"); // Set region AWS.config.update({ region: "REGION" }); // Create promise and SES service object var verifyEmailPromise = new AWS.SES({ apiVersion: "2010-12-01" }) .verifyEmailIdentity({ EmailAddress: "ADDRESS@DOMAIN.EXT" }) .promise(); // Handle promise's fulfilled/rejected states verifyEmailPromise .then(function (data) { console.log("Email verification initiated"); }) .catch(function (err) { console.error(err, err.stack); });

若要執行範例,請在命令列中輸入以下內容。網域已新增至 Amazon SES 以進行驗證。

node ses_verifyemailidentity.js

您可以在這裡找到此範例程式碼 GitHub。

驗證網域身分

在此範例中,請使用 Node.js 模組來驗證要與 Amazon SES 搭配使用的電子郵件網域。以檔名 ses_verifydomainidentity.js 建立一個 Node.js 模組。依前述內容設定軟體開發套件。

建立物件以傳遞 AWS.SES 用戶端類別的 verifyDomainIdentity 方法之 Domain 參數。若要呼叫方verifyDomainIdentity法,請建立用於叫用 Amazon SES 服務物件並傳遞參數物件的承諾。然後,在 promise 回呼中處理 response

// Load the AWS SDK for Node.js var AWS = require("aws-sdk"); // Set region AWS.config.update({ region: "REGION" }); // Create the promise and SES service object var verifyDomainPromise = new AWS.SES({ apiVersion: "2010-12-01" }) .verifyDomainIdentity({ Domain: "DOMAIN_NAME" }) .promise(); // Handle promise's fulfilled/rejected states verifyDomainPromise .then(function (data) { console.log("Verification Token: " + data.VerificationToken); }) .catch(function (err) { console.error(err, err.stack); });

若要執行範例,請在命令列中輸入以下內容。網域已新增至 Amazon SES 以進行驗證。

node ses_verifydomainidentity.js

您可以在這裡找到此範例程式碼 GitHub。

刪除身分

在此範例中,使用 Node.js 模組刪除與 Amazon SES 搭配使用的電子郵件地址或網域。以檔名 ses_deleteidentity.js 建立一個 Node.js 模組。依前述內容設定軟體開發套件。

建立物件以傳遞 AWS.SES 用戶端類別的 deleteIdentity 方法之 Identity 參數。要調用該deleteIdentity方法,請創建一個用request於調用 Amazon SES 服務對象,並傳遞參數。然後,在 promise 回呼中處理 response

// Load the AWS SDK for Node.js var AWS = require("aws-sdk"); // Set region AWS.config.update({ region: "REGION" }); // Create the promise and SES service object var deletePromise = new AWS.SES({ apiVersion: "2010-12-01" }) .deleteIdentity({ Identity: "DOMAIN_NAME" }) .promise(); // Handle promise's fulfilled/rejected states deletePromise .then(function (data) { console.log("Identity Deleted"); }) .catch(function (err) { console.error(err, err.stack); });

若要執行範例,請在命令列中輸入以下內容。

node ses_deleteidentity.js

您可以在這裡找到此範例程式碼 GitHub。