

 [適用於 JavaScript 的 AWS SDK V3 API 參考指南](https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/)詳細說明 第 3 版 適用於 JavaScript 的 AWS SDK (V3) 的所有 API 操作。

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

# 管理 Amazon SES 身分
<a name="ses-examples-managing-identities"></a>

![\[JavaScript code example that applies to Node.js execution\]](http://docs.aws.amazon.com/zh_tw/sdk-for-javascript/v3/developer-guide/images/nodeicon.png)

**這個 Node.js 程式碼範例會說明：**
+ 如何驗證與 Amazon SES 搭配使用的電子郵件地址和網域。
+ 如何將 AWS Identity and Access Management (IAM) 政策指派給您的 Amazon SES 身分。
+ 如何列出您 AWS 帳戶的所有 Amazon SES 身分。
+ 如何刪除與 Amazon SES 搭配使用的身分。

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

如需如何在 Amazon SES 中驗證電子郵件地址和網域的詳細資訊，請參閱《Amazon Simple Email Service [開發人員指南》中的在 Amazon SES 中驗證電子郵件地址和網域](https://docs.aws.amazon.com/ses/latest/DeveloperGuide/verify-addresses-and-domains.html)。如需在 Amazon SES 中傳送授權的資訊，請參閱 [Amazon SES 傳送授權概觀](Amazon Simple Email Service Developer Guidesending-authorization-overview.html)。

## 案例
<a name="ses-examples-verifying-identities-scenario"></a>

在此範例中，您使用一系列 Node.js 模組來驗證和管理 Amazon SES 身分。Node.js 模組使用以下`SES`用戶端類別的方法，使用適用於 JavaScript 的 SDK 來驗證電子郵件地址和網域：
+ [https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/Package/-aws-sdk-client-ses/Class/ListIdentitiesCommand/](https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/Package/-aws-sdk-client-ses/Class/ListIdentitiesCommand/)
+ [https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/Package/-aws-sdk-client-ses/Class/DeleteIdentityCommand/](https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/Package/-aws-sdk-client-ses/Class/DeleteIdentityCommand/)
+ [https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/Package/-aws-sdk-client-ses/Class/VerifyEmailIdentityCommand/](https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/Package/-aws-sdk-client-ses/Class/VerifyEmailIdentityCommand/)
+ [https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/Package/-aws-sdk-client-ses/Class/VerifyDomainIdentityCommand/](https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/Package/-aws-sdk-client-ses/Class/VerifyDomainIdentityCommand/)

## 先決條件任務
<a name="ses-examples-verifying-identities-prerequisites"></a>

若要設定和執行此範例，您必須先完成這些任務：
+ 設定專案環境以執行這些 Node TypeScript 範例，並安裝必要的 適用於 JavaScript 的 AWS SDK 和第三方模組。遵循[ GitHub](https://github.com/awsdocs/aws-doc-sdk-examples/tree/main/javascriptv3/example_code/ses/README.md) 上的指示。
+ 透過使用者登入資料建立共用組態檔。如需提供共用登入資料檔案的詳細資訊，請參閱 *AWS SDKs * [和工具參考指南中的共用組態和登入資料檔案](https://docs.aws.amazon.com/sdkref/latest/guide/file-format.html)。

**重要**  
這些範例示範如何使用 ECMAScript6 (ES6) 匯入/匯出用戶端服務物件和命令。  
這需要 Node.js 13.x 版或更新版本。若要下載並安裝最新版本的 Node.js，請參閱 [Node.js 下載。](https://nodejs.org/en/download)
如果您偏好使用 CommonJS 語法，請參閱 [JavaScript ES6/CommonJS 語法](sdk-example-javascript-syntax.md)。

## 列出您的身分
<a name="ses-examples-listing-identities"></a>

在此範例中，使用 Node.js 模組列出要與 Amazon SES 搭配使用的電子郵件地址和網域。

建立`libs`目錄，並建立檔案名稱為 的 Node.js 模組`sesClient.js`。複製下面的程式碼並將其貼入其中，這會建立 Amazon SES 用戶端物件。將 *REGION* 取代為您的 AWS 區域。

```
import { SESClient } from "@aws-sdk/client-ses";
// Set the AWS Region.
const REGION = "us-east-1";
// Credentials are automatically resolved using the AWS SDK credential provider chain.
// For more information, see https://docs.aws.amazon.com/sdk-for-javascript/v3/developer-guide/setting-credentials-node.html
// Create SES service object.
const sesClient = new SESClient({ region: REGION });
export { sesClient };
```

您可以在 [ GitHub 上找到](https://github.com/awsdocs/aws-doc-sdk-examples/blob/main/javascriptv3/example_code/ses/src/libs/sesClient.js)此範例程式碼。

以檔名 `ses_listidentities.js` 建立一個 Node.js 模組。如先前所示設定 SDK，包括安裝所需的用戶端和套件。

建立物件，以傳遞 `SES` 用戶端類別的 `ListIdentitiesCommand` 方法之 `IdentityType` 和其他參數。若要呼叫 `ListIdentitiesCommand`方法，請叫用 Amazon SES 服務物件，傳遞參數物件。

 `data` 傳回的 包含 `IdentityType` 參數指定的網域身分陣列。

**注意**  
將 *IdentityType* 取代為身分類型，可以是 "EmailAddress" 或 "Domain"。

```
import { ListIdentitiesCommand } from "@aws-sdk/client-ses";
import { sesClient } from "./libs/sesClient.js";

const createListIdentitiesCommand = () =>
  new ListIdentitiesCommand({ IdentityType: "EmailAddress", MaxItems: 10 });

const run = async () => {
  const listIdentitiesCommand = createListIdentitiesCommand();

  try {
    return await sesClient.send(listIdentitiesCommand);
  } catch (err) {
    console.log("Failed to list identities.", err);
    return err;
  }
};
```

若要執行範例，請在命令提示中輸入以下內容。

```
node ses_listidentities.js 
```

您可以在 [ GitHub 上找到](https://github.com/awsdocs/aws-doc-sdk-examples/blob/main/javascriptv3/example_code/ses/src/ses_listidentities.js)此範例程式碼。

## 驗證電子郵件地址身分
<a name="ses-examples-verifying-email"></a>

在此範例中，使用 Node.js 模組來驗證要與 Amazon SES 搭配使用的電子郵件寄件者。

建立`libs`目錄，並建立檔案名稱為 的 Node.js 模組`sesClient.js`。複製下面的程式碼並將其貼入其中，這會建立 Amazon SES 用戶端物件。將 *REGION* 取代為您的 AWS 區域。

```
import { SESClient } from "@aws-sdk/client-ses";
// Set the AWS Region.
const REGION = "us-east-1";
// Credentials are automatically resolved using the AWS SDK credential provider chain.
// For more information, see https://docs.aws.amazon.com/sdk-for-javascript/v3/developer-guide/setting-credentials-node.html
// Create SES service object.
const sesClient = new SESClient({ region: REGION });
export { sesClient };
```

您可以在 [ GitHub 上找到](https://github.com/awsdocs/aws-doc-sdk-examples/blob/main/javascriptv3/example_code/ses/src/libs/sesClient.js)此範例程式碼。

以檔名 `ses_verifyemailidentity.js` 建立一個 Node.js 模組。如先前所示設定 SDK，包括下載所需的用戶端和套件。

建立物件以傳遞 `SES` 用戶端類別的 `VerifyEmailIdentityCommand` 方法之 `EmailAddress` 參數。若要呼叫 `VerifyEmailIdentityCommand`方法，請叫用 Amazon SES 用戶端服務物件，傳遞參數。

**注意**  
將 *EMAIL\$1ADDRESS* 取代為電子郵件地址，例如 name@example.com。

```
// Import required AWS SDK clients and commands for Node.js
import { VerifyEmailIdentityCommand } from "@aws-sdk/client-ses";
import { sesClient } from "./libs/sesClient.js";

const EMAIL_ADDRESS = "name@example.com";

const createVerifyEmailIdentityCommand = (emailAddress) => {
  return new VerifyEmailIdentityCommand({ EmailAddress: emailAddress });
};

const run = async () => {
  const verifyEmailIdentityCommand =
    createVerifyEmailIdentityCommand(EMAIL_ADDRESS);
  try {
    return await sesClient.send(verifyEmailIdentityCommand);
  } catch (err) {
    console.log("Failed to verify email identity.", err);
    return err;
  }
};
```

若要執行範例，請在命令提示中輸入以下內容。網域會新增至要驗證的 Amazon SES。

```
node ses_verifyemailidentity.js 
```

您可以在 [ GitHub 上找到](https://github.com/awsdocs/aws-doc-sdk-examples/blob/main/javascriptv3/example_code/ses/src/ses_verifyemailidentity.js)此範例程式碼。

## 驗證網域身分
<a name="ses-examples-verifying-domains"></a>

在此範例中，使用 Node.js 模組來驗證要與 Amazon SES 搭配使用的電子郵件網域。

建立`libs`目錄，並建立檔案名稱為 的 Node.js 模組`sesClient.js`。複製下面的程式碼並將其貼入其中，這會建立 Amazon SES 用戶端物件。將 *REGION* 取代為您的 AWS 區域。

```
import { SESClient } from "@aws-sdk/client-ses";
// Set the AWS Region.
const REGION = "us-east-1";
// Credentials are automatically resolved using the AWS SDK credential provider chain.
// For more information, see https://docs.aws.amazon.com/sdk-for-javascript/v3/developer-guide/setting-credentials-node.html
// Create SES service object.
const sesClient = new SESClient({ region: REGION });
export { sesClient };
```

您可以在 [ GitHub 上找到](https://github.com/awsdocs/aws-doc-sdk-examples/blob/main/javascriptv3/example_code/ses/src/libs/sesClient.js)此範例程式碼。

以檔名 `ses_verifydomainidentity.js` 建立一個 Node.js 模組。如先前所示設定 SDK，包括安裝所需的用戶端和套件。

建立物件以傳遞 `SES` 用戶端類別的 `VerifyDomainIdentityCommand` 方法之 `Domain` 參數。若要呼叫 `VerifyDomainIdentityCommand`方法，請叫用 Amazon SES 用戶端服務物件，傳遞參數物件。

**注意**  
此範例會匯入並使用所需的 AWS Service V3 套件用戶端、V3 命令，並以非同步/等待模式使用 `send`方法。您可以使用 V2 命令建立此範例，方法是進行一些次要變更。如需詳細資訊，請參閱[使用 v3 命令](migrating.md#using_v3_commands)。

**注意**  
以網域名稱取代 *DOMAIN\$1NAME*。

```
import { VerifyDomainIdentityCommand } from "@aws-sdk/client-ses";
import {
  getUniqueName,
  postfix,
} from "@aws-doc-sdk-examples/lib/utils/util-string.js";
import { sesClient } from "./libs/sesClient.js";

/**
 * You must have access to the domain's DNS settings to complete the
 * domain verification process.
 */
const DOMAIN_NAME = postfix(getUniqueName("Domain"), ".example.com");

const createVerifyDomainIdentityCommand = () => {
  return new VerifyDomainIdentityCommand({ Domain: DOMAIN_NAME });
};

const run = async () => {
  const VerifyDomainIdentityCommand = createVerifyDomainIdentityCommand();

  try {
    return await sesClient.send(VerifyDomainIdentityCommand);
  } catch (err) {
    console.log("Failed to verify domain.", err);
    return err;
  }
};
```

若要執行範例，請在命令提示中輸入以下內容。網域會新增至要驗證的 Amazon SES。

```
node ses_verifydomainidentity.js  
```

您可以在 [ GitHub 上找到](https://github.com/awsdocs/aws-doc-sdk-examples/blob/main/javascriptv3/example_code/ses/src/ses_verifydomainidentity.js)此範例程式碼。

## 刪除身分
<a name="ses-examples-deleting-identities"></a>

在此範例中，使用 Node.js 模組來刪除與 Amazon SES 搭配使用的電子郵件地址或網域。

建立`libs`目錄，並建立檔案名稱為 的 Node.js 模組`sesClient.js`。複製下面的程式碼並將其貼入其中，這會建立 Amazon SES 用戶端物件。將 *REGION* 取代為您的 AWS 區域。

```
import { SESClient } from "@aws-sdk/client-ses";
// Set the AWS Region.
const REGION = "us-east-1";
// Credentials are automatically resolved using the AWS SDK credential provider chain.
// For more information, see https://docs.aws.amazon.com/sdk-for-javascript/v3/developer-guide/setting-credentials-node.html
// Create SES service object.
const sesClient = new SESClient({ region: REGION });
export { sesClient };
```

您可以在 [ GitHub 上找到此](https://github.com/awsdocs/aws-doc-sdk-examples/blob/main/javascriptv3/example_code/ses/src/libs/sesClient.js)範例程式碼。

以檔名 `ses_deleteidentity.js` 建立一個 Node.js 模組。如先前所示設定 SDK，包括安裝所需的用戶端和套件。

建立物件以傳遞 `SES` 用戶端類別的 `DeleteIdentityCommand` 方法之 `Identity` 參數。若要呼叫 `DeleteIdentityCommand`方法，請建立 `request`以叫用 Amazon SES 用戶端服務物件，並傳遞參數。

**注意**  
此範例會匯入並使用所需的 AWS Service V3 套件用戶端、V3 命令，並以非同步/等待模式使用 `send`方法。您可以使用 V2 命令建立此範例，方法是進行一些次要變更。如需詳細資訊，請參閱[使用 v3 命令](migrating.md#using_v3_commands)。

**注意**  
將 *IDENTITY\$1EMAIL* 取代為要刪除之身分的電子郵件。

```
import { DeleteIdentityCommand } from "@aws-sdk/client-ses";
import { sesClient } from "./libs/sesClient.js";

const IDENTITY_EMAIL = "fake@example.com";

const createDeleteIdentityCommand = (identityName) => {
  return new DeleteIdentityCommand({
    Identity: identityName,
  });
};

const run = async () => {
  const deleteIdentityCommand = createDeleteIdentityCommand(IDENTITY_EMAIL);

  try {
    return await sesClient.send(deleteIdentityCommand);
  } catch (err) {
    console.log("Failed to delete identity.", err);
    return err;
  }
};
```

若要執行範例，請在命令提示中輸入以下內容。

```
node ses_deleteidentity.js 
```

您可以在 [ GitHub 上找到](https://github.com/awsdocs/aws-doc-sdk-examples/blob/main/javascriptv3/example_code/ses/src/ses_deleteidentity.js)此範例程式碼。