

 [AWS SDK for JavaScript V3 API リファレンスガイド](https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/)では、 AWS SDK for JavaScript バージョン3 (V3) のすべての API オペレーションについて詳しく説明します。

翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。

# Amazon Simple Email Servicesの例
<a name="ses-examples"></a>

Amazon Simple Email Service (Amazon SES) は、デジタルマーケティング担当者やアプリケーションデベロッパーがマーケティング、通知、トランザクションに関する E メールを送信できるように設計された、クラウドベースの E メール送信サービスです。E メールを利用してお客様とのつながりを維持するあらゆる規模の企業を対象とした、コスト効率の高い信頼できるサービスです。

![\[JavaScript 環境、SDK、および Amazon SES の関係\]](http://docs.aws.amazon.com/ja_jp/sdk-for-javascript/v3/developer-guide/images/code-samples-ses.png)


JavaScript API for Amazon SES は `SES` クライアントクラスを通じて公開されます。Amazon SES クライアントクラスの使用についての詳細は、API リファレンスの[Class: SES](https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/Package/-aws-sdk-client-ses/Class/SES/)（クラス：SES)を参照してください。

**Topics**
+ [Amazon SES アイデンティティの管理](ses-examples-managing-identities.md)
+ [Amazon SESでのE メールテンプレートの操作](ses-examples-creating-template.md)
+ [Amazon SES を使用してEメールを送信します](ses-examples-sending-email.md)

# Amazon SES アイデンティティの管理
<a name="ses-examples-managing-identities"></a>

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

**この Node.js コード例は以下を示しています。**
+ Amazon SES で使用されている E メールアドレスとドメインを確認する方法。
+  AWS Identity and Access Management (IAM) ポリシーを Amazon SES ID に割り当てる方法。
+  AWS アカウントのすべての Amazon SES ID を一覧表示する方法。
+ Amazon SES で使用されているアイデンティティを削除する方法。

Amazon SES *アイデンティティ*は、Amazon SES が E メールの送信に使用する E メールアドレスまたはドメインです。Amazon SES では、E メールアイデンティティを検証して、それを所有していることを確認し、他のユーザーに使用されないようにする必要があります。

Amazon SES の E メールアドレスとドメインを確認する方法の詳細については、Amazon Simple Email Service デベロッパーガイドの[Amazon SESでのEメールアドレスとドメインの検証](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` クライアントクラスの次のメソッドを使用し、SDK for JavaScript を使用して E メールアドレスとドメインを検証します。
+ [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 の例を実行するようにプロジェクト環境を設定し、必要な AWS SDK for JavaScript モジュールとサードパーティーモジュールをインストールします。「[GitHub](https://github.com/awsdocs/aws-doc-sdk-examples/tree/main/javascriptv3/example_code/ses/README.md)」の指示に従います。　
+ ユーザーの認証情報を使用して、共有設定ファイルを作成します。共有認証情報ファイルの提供の詳細については、「*AWS SDK とツールのリファレンスガイド*」の「[共有設定ファイルおよび認証情報ファイル](https://docs.aws.amazon.com/sdkref/latest/guide/file-format.html)」を参照してください。

**重要**  
これらの例は、ECMAScript6 (ES6) を使用してクライアントサービスオブジェクトとコマンドをimport/export する方法を示します。  
これには 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 で使用する E メールアドレスとドメインを一覧表示します。

`libs`ディレクトリを作成し、ファイル名`sesClient.js`でNode.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`パラメーターで指定されたドメインIDの配列が含まれます。

**注記**  
*IdentityType*を「EmailAddress」または「Domain」のIDタイプに置き換えます。

```
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)で見つけられます。

## E メールアドレスアイデンティの検証
<a name="ses-examples-verifying-email"></a>

この例では、Node.js モジュールを使用して Amazon SES で使用する E メールの送信者を検証します。

`libs`ディレクトリを作成し、ファイル名`sesClient.js`でNode.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 などの E メールアドレスに置き換えます。

```
// 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)で見つけられます。

## ドメイン ID の検証
<a name="ses-examples-verifying-domains"></a>

この例では、Node.js モジュールを使用して Amazon SES で使用する E メールのドメインを検証します。

`libs`ディレクトリを作成し、ファイル名`sesClient.js`でNode.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 で使用されている E メールアドレスまたはドメインを削除します。

`libs`ディレクトリを作成し、ファイル名`sesClient.js`でNode.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` メソッドを呼び出すには、Amazon SES クライアントサービスオブジェクトを起動するための`request`を作成し、パラメータを渡します。

**注記**  
この例では、必要な AWS Service V3 パッケージクライアント、V3 コマンドをインポートして使用し、非同期/待機パターンで `send`メソッドを使用します。この例は、代わりに少し変更を加えてV2コマンドで作成できます。詳細については、「[v3 コマンドの使用](migrating.md#using_v3_commands)」を参照してください。

**注記**  
*IDENTITY\$1EMAIL* を削除対象の ID の E メールアドレスに置き換えます。

```
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)で見つけられます。

# Amazon SESでのE メールテンプレートの操作
<a name="ses-examples-creating-template"></a>

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

**この Node.js コード例は以下を示しています。**
+ すべてのEメールテンプレートの一覧表を取得する方法
+ E メールテンプレートを取得して更新する方法
+ E メールテンプレートを作成して削除する方法

Amazon SES では、Eメールテンプレートを使用してパーソナライズされたEメールメッセージを送信できます。Amazon SESでEメールテンプレートを作成および使用する方法の詳細については、Amazon シンプル Eメールサービス・ディベロッパーガイドの「[Amazon SES APIを使用したパーソナライズされたEメールの送信](https://docs.aws.amazon.com/ses/latest/DeveloperGuide/send-personalized-email-api.html)」を参照してください。

## シナリオ
<a name="ses-examples-creating-template-scenario"></a>

この例では、一連の Node.js モジュールを使用して E メールテンプレートを操作します。Node.js モジュールは SDK for JavaScript を使用し、`SES` クライアントクラスの次のメソッドを使用して E メールテンプレートを作成して使用します。
+ [https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/Package/-aws-sdk-client-ses/Class/ListTemplatesCommand/](https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/Package/-aws-sdk-client-ses/Class/ListTemplatesCommand/)
+ [https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/Package/-aws-sdk-client-ses/Class/CreateTemplateCommand/](https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/Package/-aws-sdk-client-ses/Class/CreateTemplateCommand/)
+ [https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/Package/-aws-sdk-client-ses/Class/GetTemplateCommand/](https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/Package/-aws-sdk-client-ses/Class/GetTemplateCommand/)
+ [https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/Package/-aws-sdk-client-ses/Class/DeleteTemplateCommand/](https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/Package/-aws-sdk-client-ses/Class/DeleteTemplateCommand/)
+ [https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/Package/-aws-sdk-client-ses/Class/UpdateTemplateCommand/](https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/Package/-aws-sdk-client-ses/Class/UpdateTemplateCommand/)

## 前提条件タスク
<a name="ses-examples-creating-template-prerequisites"></a>

この例をセットアップして実行するには、まず次のタスクを完了する必要があります。
+ これらの Node TypeScript の例を実行するようにプロジェクト環境を設定し、必要な AWS SDK for JavaScript モジュールとサードパーティーモジュールをインストールします。「[GitHub](https://github.com/awsdocs/aws-doc-sdk-examples/tree/main/javascriptv3/example_code/ses/README.md)」の指示に従います。　
+ ユーザーの認証情報を使用して、共有設定ファイルを作成します。共有認証情報ファイルの提供の詳細については、「*AWS SDK とツールのリファレンスガイド*」の「[共有設定ファイルおよび認証情報ファイル](https://docs.aws.amazon.com/sdkref/latest/guide/file-format.html)」を参照してください。

**重要**  
これらの例は、ECMAScript6 (ES6) を使用してクライアントサービスオブジェクトとコマンドをimport/export する方法を示します。  
これには Node.js バージョン13.x以降が必要です。Node.js の最新バージョンをダウンロードしてインストールするには、「[Node.js ダウンロード](https://nodejs.org/en/download)」を参照してください。
CommonJS 構文を使用する場合は、「[JavaScript ES6/CommonJS 構文](sdk-example-javascript-syntax.md)」を参照してください。

## Eメールテンプレートの一覧表示
<a name="ses-examples-listing-templates"></a>

この例では、Node.js モジュールを使用して Amazon SES で使用する E メールテンプレートを作成します。

`libs`ディレクトリを作成し、ファイル名`sesClient.js`でNode.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_listtemplates.js`というファイル名で Node.js モジュールを作成します。必要なクライアントとパッケージのインストールを含め、前述のようにSDKを設定します。

`SES` クライアントクラスの `ListTemplatesCommand` メソッドのパラメータを渡すオブジェクトを作成します。`ListTemplatesCommand`メソッドを呼び出すには、Amazon SES クライアントサービスオブジェクトを起動し、パラメータを渡します。

**注記**  
この例では、必要な AWS Service V3 パッケージクライアント、V3 コマンドをインポートして使用し、非同期/待機パターンで `send`メソッドを使用します。この例は、代わりに少し変更を加えてV2コマンドで作成できます。詳細については、「[v3 コマンドの使用](migrating.md#using_v3_commands)」を参照してください。

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

const createListTemplatesCommand = (maxItems) =>
  new ListTemplatesCommand({ MaxItems: maxItems });

const run = async () => {
  const listTemplatesCommand = createListTemplatesCommand(10);

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

この例を実行するには、コマンドプロンプトで以下を入力します。Amazon SES はテンプレートのリストを返します。

```
node ses_listtemplates.js  
```

このサンプルコードは、[この GitHub に](https://github.com/awsdocs/aws-doc-sdk-examples/blob/main/javascriptv3/example_code/ses/src/ses_listtemplates.js)で見つけられます。

## E メールテンプレートの取得
<a name="ses-examples-get-template"></a>

この例では、Node.js モジュールを使用して Amazon SES で使用する E メールテンプレートを取得します。

`libs`ディレクトリを作成し、ファイル名`sesClient.js`でNode.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_gettemplate.js`というファイル名で Node.js モジュールを作成します。必要なクライアントとパッケージのインストールを含め、前述のようにSDKを設定します。

`SES` クライアントクラスの `GetTemplateCommand` メソッドに `TemplateName` パラメータを渡すオブジェクトを作成します。`GetTemplateCommand`メソッドを呼び出すには、Amazon SES クライアントサービスオブジェクトを起動し、パラメータを渡します。

**注記**  
この例では、必要な AWS Service V3 パッケージクライアント、V3 コマンドをインポートして使用し、非同期/待機パターンで `send`メソッドを使用します。この例は、代わりに少し変更を加えてV2コマンドで作成できます。詳細については、「[v3 コマンドの使用](migrating.md#using_v3_commands)」を参照してください。

**注記**  
*TEMPLATE\$1NAME*（テンプレート名)を返すテンプレートの名前に置き換えます。

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

const TEMPLATE_NAME = getUniqueName("TemplateName");

const createGetTemplateCommand = (templateName) =>
  new GetTemplateCommand({ TemplateName: templateName });

const run = async () => {
  const getTemplateCommand = createGetTemplateCommand(TEMPLATE_NAME);

  try {
    return await sesClient.send(getTemplateCommand);
  } catch (caught) {
    if (caught instanceof Error && caught.name === "MessageRejected") {
      /** @type { import('@aws-sdk/client-ses').MessageRejected} */
      const messageRejectedError = caught;
      return messageRejectedError;
    }
    throw caught;
  }
};
```

この例を実行するには、コマンドプロンプトで以下を入力します。Amazon SES はテンプレートの詳細を返します。

```
node ses_gettemplate.js 
```

このサンプルコードは、[この GitHub に](https://github.com/awsdocs/aws-doc-sdk-examples/blob/main/javascriptv3/example_code/ses/src/ses_gettemplate.js)で見つけられます。

## Eメールテンプレートの作成
<a name="ses-examples-create-template"></a>

この例では、Node.js モジュールを使用して Amazon SES で使用する E メールテンプレートを作成します。

`libs`ディレクトリを作成し、ファイル名`sesClient.js`でNode.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_createtemplate.js`というファイル名で Node.js モジュールを作成します。必要なクライアントとパッケージのインストールを含め、前述のようにSDKを設定します。

`TemplateName`、`HtmlPart`、`SubjectPart` および `TextPart` を含む、`SES` クライアントクラスの `CreateTemplateCommand` メソッドのパラメータを渡すオブジェクトを作成します。`CreateTemplateCommand`メソッドを呼び出すには、Amazon SES クライアントサービスオブジェクトを起動し、パラメータを渡します。

**注記**  
この例では、必要な AWS Service V3 パッケージクライアント、V3 コマンドをインポートして使用し、非同期/待機パターンで `send`メソッドを使用します。この例は、代わりに少し変更を加えてV2コマンドで作成できます。詳細については、「[v3 コマンドの使用](migrating.md#using_v3_commands)」を参照してください。

**注記**  
*TEMPLATE\$1NAME* を新しいテンプレートの名前に、*HtmlPart* を E メールの HTML タグ付きコンテンツに、*SubjectPart* を E メールの件名に置き換えてください。

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

const TEMPLATE_NAME = getUniqueName("TestTemplateName");

const createCreateTemplateCommand = () => {
  return new CreateTemplateCommand({
    /**
     * The template feature in Amazon SES is based on the Handlebars template system.
     */
    Template: {
      /**
       * The name of an existing template in Amazon SES.
       */
      TemplateName: TEMPLATE_NAME,
      HtmlPart: `
        <h1>Hello, {{contact.firstName}}!</h1>
        <p>
        Did you know Amazon has a mascot named Peccy?
        </p>
      `,
      SubjectPart: "Amazon Tip",
    },
  });
};

const run = async () => {
  const createTemplateCommand = createCreateTemplateCommand();

  try {
    return await sesClient.send(createTemplateCommand);
  } catch (err) {
    console.log("Failed to create template.", err);
    return err;
  }
};
```

この例を実行するには、コマンドプロンプトで以下を入力します。テンプレートが Amazon SES に追加されます。

```
node ses_createtemplate.js  
```

このサンプルコードは、[この GitHub に](https://github.com/awsdocs/aws-doc-sdk-examples/blob/main/javascriptv3/example_code/ses/src/ses_createtemplate.js)で見つけられます。

## E メールテンプレートの更新
<a name="ses-examples-update-template"></a>

この例では、Node.js モジュールを使用して Amazon SES で使用する E メールテンプレートを作成します。

`libs`ディレクトリを作成し、ファイル名`sesClient.js`でNode.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_updatetemplate.js`というファイル名で Node.js モジュールを作成します。必要なクライアントとパッケージのインストールを含め、前述のようにSDKを設定します。

必要な `TemplateName` パラメータを `SES` クライアントクラスの `UpdateTemplateCommand` メソッドに渡して、テンプレートで更新する `Template` パラメータ値を渡すオブジェクトを作成します。`UpdateTemplateCommand`メソッドを呼び出すには、Amazon SES サービスオブジェクトを起動し、パラメータを渡します。

**注記**  
この例では、必要な AWS Service V3 パッケージクライアント、V3 コマンドをインポートして使用し、非同期/待機パターンで `send`メソッドを使用します。この例は、代わりに少し変更を加えてV2コマンドで作成できます。詳細については、「[v3 コマンドの使用](migrating.md#using_v3_commands)」を参照してください。

**注記**  
*TEMPLATE\$1NAME* をテンプレートの名前に、*HTML\$1PART* を E メールの HTML タグ付きコンテンツに置き換えます。

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

const TEMPLATE_NAME = getUniqueName("TemplateName");
const HTML_PART = "<h1>Hello, World!</h1>";

const createUpdateTemplateCommand = () => {
  return new UpdateTemplateCommand({
    Template: {
      TemplateName: TEMPLATE_NAME,
      HtmlPart: HTML_PART,
      SubjectPart: "Example",
      TextPart: "Updated template text.",
    },
  });
};

const run = async () => {
  const updateTemplateCommand = createUpdateTemplateCommand();

  try {
    return await sesClient.send(updateTemplateCommand);
  } catch (err) {
    console.log("Failed to update template.", err);
    return err;
  }
};
```

この例を実行するには、コマンドプロンプトで以下を入力します。Amazon SES はテンプレートの詳細を返します。

```
node ses_updatetemplate.js 
```

このサンプルコードは、[この GitHub に](https://github.com/awsdocs/aws-doc-sdk-examples/blob/main/javascriptv3/example_code/ses/src/ses_updatetemplate.js)で見つけられます。

## E メールテンプレートの削除
<a name="ses-examples-delete-template"></a>

この例では、Node.js モジュールを使用して Amazon SES で使用する E メールテンプレートを作成します。

`libs`ディレクトリを作成し、ファイル名`sesClient.js`でNode.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_deletetemplate.js`というファイル名で Node.js モジュールを作成します。必要なクライアントとパッケージのインストールを含め、前述のようにSDKを設定します。

必要な `TemplateName` パラメータを `SES` クライアントクラスの `DeleteTemplateCommand` メソッドに渡すオブジェクトを作成します。`DeleteTemplateCommand`メソッドを呼び出すには、Amazon SES サービスオブジェクトを起動し、パラメータを渡します。

**注記**  
この例では、必要な AWS Service V3 パッケージクライアント、V3 コマンドをインポートして使用し、非同期/待機パターンで `send`メソッドを使用します。この例は、代わりに少し変更を加えてV2コマンドで作成できます。詳細については、「[v3 コマンドの使用](migrating.md#using_v3_commands)」を参照してください。

**注記**  
*TEMPLATE\$1NAME* を削除するテンプレートの名前に置き換えてください。

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

const TEMPLATE_NAME = getUniqueName("TemplateName");

const createDeleteTemplateCommand = (templateName) =>
  new DeleteTemplateCommand({ TemplateName: templateName });

const run = async () => {
  const deleteTemplateCommand = createDeleteTemplateCommand(TEMPLATE_NAME);

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

この例を実行するには、コマンドプロンプトで以下を入力します。Amazon SES はテンプレートの詳細を返します。

```
node ses_deletetemplate.js 
```

このサンプルコードは、[この GitHub に](https://github.com/awsdocs/aws-doc-sdk-examples/blob/main/javascriptv3/example_code/ses/src/ses_deletetemplate.js)で見つけられます。

# Amazon SES を使用してEメールを送信します
<a name="ses-examples-sending-email"></a>

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

**この Node.js コード例は以下を示しています。**
+ テキストまたは HTML の E メールを送信します。
+ E メールテンプレートに基づいて E メールを送信します。
+ E メールテンプレートに基づいて一括 E メールを送信します。

Amazon SES API は、E メールメッセージの構成に対する制御の程度に応じて、フォーマット済みと raw の 2 つの異なる方法で E メールを送信できます。詳細については、[「Amazon SESAPIを使用したフォーマット済みEメールの送信」](https://docs.aws.amazon.com/ses/latest/DeveloperGuide/send-email-formatted.html)および[「AmazonSESAPIを使用した生のEメールの送信」](https://docs.aws.amazon.com/ses/latest/DeveloperGuide/send-email-raw.html)を参照してください。

## シナリオ
<a name="ses-examples-sending-email-scenario"></a>

この例では、一連の Node.js モジュールを使用してさまざまな方法で E メールを送信します。Node.js モジュールは SDK for JavaScript を使用し、`SES` クライアントクラスの次のメソッドを使用して E メールテンプレートを作成して使用します。
+ [https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/Package/-aws-sdk-client-ses/Class/SendEmailCommand/](https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/Package/-aws-sdk-client-ses/Class/SendEmailCommand/)
+ [https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/Package/-aws-sdk-client-ses/Class/SendTemplatedEmailCommand/](https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/Package/-aws-sdk-client-ses/Class/SendTemplatedEmailCommand/)
+ [https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/Package/-aws-sdk-client-ses/Class/SendBulkTemplatedEmailCommand/](https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/Package/-aws-sdk-client-ses/Class/SendBulkTemplatedEmailCommand/)

## 前提条件タスク
<a name="ses-examples-sending-emails-prerequisites"></a>

この例をセットアップして実行するには、まず次のタスクを完了する必要があります。
+ これらの Node TypeScript の例を実行するようにプロジェクト環境を設定し、必要な AWS SDK for JavaScript モジュールとサードパーティーモジュールをインストールします。「[GitHub](https://github.com/awsdocs/aws-doc-sdk-examples/tree/main/javascriptv3/example_code/ses/README.md)」の指示に従います。　
+ ユーザーの認証情報を使用して、共有設定ファイルを作成します。共有認証情報ファイルの提供の詳細については、「*AWS SDK とツールのリファレンスガイド*」の「[共有設定ファイルおよび認証情報ファイル](https://docs.aws.amazon.com/sdkref/latest/guide/file-format.html)」を参照してください。

**重要**  
これらの例は、ECMAScript6 (ES6) を使用してクライアントサービスオブジェクトとコマンドをimport/export する方法を示します。  
これには Node.js バージョン13.x以降が必要です。Node.js の最新バージョンをダウンロードしてインストールするには、「[Node.js ダウンロード](https://nodejs.org/en/download)」を参照してください。
CommonJS 構文を使用する場合は、「[JavaScript ES6/CommonJS 構文](sdk-example-javascript-syntax.md)」を参照してください。

## E メールメッセージの送信要件
<a name="ses-examples-sending-msail-reqs"></a>

Amazon SES は E メールメッセージを作成し、送信するメッセージをすぐにキューに入れます。`SendEmailCommand` メソッドを使用して E メールを送信するには、メッセージが以下の要件を満たしている必要があります。
+ 検証済みの E メールアドレスまたはドメインからメッセージを送信する必要があります。検証されていないアドレスまたはドメインを使用して E メールを送信しようとすると、`"Email address not verified"` エラーが発生します。
+ アカウントがまだ Amazon SES サンドボックスにある場合は、検証済みのアドレスまたはドメイン、または Amazon SES メールボックスシミュレーターに関連付けられた E メールアドレスにのみ送信できます。詳細については、Amazon Simple Email Service デベロッパーガイドの [[ Eメールアドレスとドメインの検証］](https://docs.aws.amazon.com/ses/latest/DeveloperGuide/verify-addresses-and-domains.html)を参照してください。
+ 添付ファイルを含むメッセージの合計サイズは 10 MB より小さくなければなりません。
+ メッセージには少なくとも 1 つの受信者の E メールアドレスを含める必要があります。受信者アドレスは、To: アドレス、CC: アドレス、または BCC: アドレスのいずれかです。受信者の E メールアドレスが無効な場合 (つまり、`UserName@[SubDomain.]Domain.TopLevelDomain` のフォーマットではない場合)、メッセージに他の有効な受信者が含まれていても、メッセージ全体が拒否されます。
+ メッセージには、To:、CC:、BCC: のフィールド全体で 50 人を超える受信者を含めることはできません。それ以上の数のユーザーに E メールメッセージを送信する必要がある場合は、受信者リストを 50 ユーザー以下のグループに分割し、`sendEmail` メソッドを数回呼び出して各グループにメッセージを送信することができます。

## E メールを送信する
<a name="ses-examples-sendmail"></a>

この例では、Node.js モジュールを使用して Amazon SES で E メールを送信します。

`libs`ディレクトリを作成し、ファイル名`sesClient.js`でNode.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_sendemail.js`というファイル名で Node.js モジュールを作成します。必要なクライアントとパッケージのインストールを含め、前述のようにSDKを設定します。

送信者と受信者のアドレス、件名、プレーンテキストおよび HTML 形式のEメール本文などを含む、送信するEメールを定義するパラメータ値を `SES`クライアントクラスの`SendEmailCommand`メソッドに渡すオブジェクトを作成します。`SendEmailCommand`メソッドを呼び出すには、Amazon SES サービスオブジェクトを起動し、パラメータを渡します。

**注記**  
この例では、必要な AWS Service V3 パッケージクライアント、V3 コマンドをインポートして使用し、非同期/待機パターンで `send`メソッドを使用します。この例は、代わりに少し変更を加えてV2コマンドで作成できます。詳細については、「[v3 コマンドの使用](migrating.md#using_v3_commands)」を参照してください。

**注記**  
*toAddress* をE メールの宛先の E メールアドレスに、*fromAddress* を E メールの送信元の E メールアドレスに置き換えます。

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

const createSendEmailCommand = (toAddress, fromAddress) => {
  return new SendEmailCommand({
    Destination: {
      /* required */
      CcAddresses: [
        /* more items */
      ],
      ToAddresses: [
        toAddress,
        /* more To-email addresses */
      ],
    },
    Message: {
      /* required */
      Body: {
        /* required */
        Html: {
          Charset: "UTF-8",
          Data: "HTML_FORMAT_BODY",
        },
        Text: {
          Charset: "UTF-8",
          Data: "TEXT_FORMAT_BODY",
        },
      },
      Subject: {
        Charset: "UTF-8",
        Data: "EMAIL_SUBJECT",
      },
    },
    Source: fromAddress,
    ReplyToAddresses: [
      /* more items */
    ],
  });
};

const run = async () => {
  const sendEmailCommand = createSendEmailCommand(
    "recipient@example.com",
    "sender@example.com",
  );

  try {
    return await sesClient.send(sendEmailCommand);
  } catch (caught) {
    if (caught instanceof Error && caught.name === "MessageRejected") {
      /** @type { import('@aws-sdk/client-ses').MessageRejected} */
      const messageRejectedError = caught;
      return messageRejectedError;
    }
    throw caught;
  }
};
```

この例を実行するには、コマンドプロンプトで以下を入力します。E メールは Amazon SES による送信のためにキューに登録されます。

```
node ses_sendemail.js 
```

このサンプルコードは、 [[ here on GitHub ]](https://github.com/awsdocs/aws-doc-sdk-examples/blob/main/javascriptv3/example_code/ses/src/ses_sendemail.js)で見つけられます。

## テンプレートを使用した E メールを送信する
<a name="ses-examples-sendtemplatedemail"></a>

この例では、Node.js モジュールを使用して Amazon SES で E メールを送信します。`ses_sendtemplatedemail.js` というファイル名で Node.js モジュールを作成します。必要なクライアントとパッケージのインストールを含め、前述のようにSDKを設定します。

送信者と受信者のアドレス、件名、プレーンテキストおよび HTML 形式の E メール本文など、送信する E メールを定義するパラメータ値を `SES` クライアントクラスの `SendTemplatedEmailCommand` メソッドに渡すオブジェクトを作成します。`SendTemplatedEmailCommand`メソッドを呼び出すには、Amazon SES クライアントサービスオブジェクトを起動し、パラメータを渡します。

**注記**  
この例では、必要な AWS Service V3 パッケージクライアント、V3 コマンドをインポートして使用し、非同期/待機パターンで `send`メソッドを使用します。この例は、代わりに少し変更を加えてV2コマンドで作成できます。詳細については、「[v3 コマンドの使用](migrating.md#using_v3_commands)」を参照してください。

**注記**  
*REGION* を AWS リージョンに、*USER* を E メールを送信する名前と E メールアドレスに、*VERIFIED\$1EMAIL* を E メールを送信する E メールアドレスに、*TEMPLATE\$1NAME* をテンプレートの名前に置き換えます。

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

/**
 * Replace this with the name of an existing template.
 */
const TEMPLATE_NAME = getUniqueName("ReminderTemplate");

/**
 * Replace these with existing verified emails.
 */
const VERIFIED_EMAIL = postfix(getUniqueName("Bilbo"), "@example.com");

const USER = { firstName: "Bilbo", emailAddress: VERIFIED_EMAIL };

/**
 *
 * @param { { emailAddress: string, firstName: string } } user
 * @param { string } templateName - The name of an existing template in Amazon SES.
 * @returns { SendTemplatedEmailCommand }
 */
const createReminderEmailCommand = (user, templateName) => {
  return new SendTemplatedEmailCommand({
    /**
     * Here's an example of how a template would be replaced with user data:
     * Template: <h1>Hello {{contact.firstName}},</h1><p>Don't forget about the party gifts!</p>
     * Destination: <h1>Hello Bilbo,</h1><p>Don't forget about the party gifts!</p>
     */
    Destination: { ToAddresses: [user.emailAddress] },
    TemplateData: JSON.stringify({ contact: { firstName: user.firstName } }),
    Source: VERIFIED_EMAIL,
    Template: templateName,
  });
};

const run = async () => {
  const sendReminderEmailCommand = createReminderEmailCommand(
    USER,
    TEMPLATE_NAME,
  );
  try {
    return await sesClient.send(sendReminderEmailCommand);
  } catch (caught) {
    if (caught instanceof Error && caught.name === "MessageRejected") {
      /** @type { import('@aws-sdk/client-ses').MessageRejected} */
      const messageRejectedError = caught;
      return messageRejectedError;
    }
    throw caught;
  }
};
```

この例を実行するには、コマンドプロンプトで以下を入力します。E メールは Amazon SES による送信のためにキューに登録されます。

```
node ses_sendtemplatedemail.js 
```

このサンプルコードは、[この GitHub に](https://github.com/awsdocs/aws-doc-sdk-examples/blob/main/javascriptv3/example_code/ses/src/ses_sendtemplatedemail.js)で見つけられます。

## テンプレートを使用した一括Eメールを送信します
<a name="ses-examples-sendbulktemplatedemail"></a>

この例では、Node.js モジュールを使用して Amazon SES で E メールを送信します。

`libs`ディレクトリを作成し、ファイル名`sesClient.js`でNode.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_sendbulktemplatedemail.js`というファイル名で Node.js モジュールを作成します。必要なクライアントとパッケージのインストールを含め、前述のようにSDKを設定します。

送信者と受信者のアドレス、件名、プレーンテキストおよび HTML 形式のEメール本文などを含む、送信するEメールを定義するパラメータ値を `SES`クライアントクラスの`SendBulkTemplatedEmailCommand`メソッドに渡すオブジェクトを作成します。`SendBulkTemplatedEmailCommand`メソッドを呼び出すには、Amazon SES サービスオブジェクトを起動し、パラメータを渡します。

**注記**  
この例では、必要な AWS Service V3 パッケージクライアント、V3 コマンドをインポートして使用し、非同期/待機パターンで `send`メソッドを使用します。この例は、代わりに少し変更を加えてV2コマンドで作成できます。詳細については、「[v3 コマンドの使用](migrating.md#using_v3_commands)」を参照してください。

**注記**  
*USERS* を E メールの宛先のユーザー名と E メールアドレスに、*VERIFIED\$1EMAIL\$11* を E メールの送信元の E メールアドレスに、*TEMPLATE\$1NAME* をテンプレート名に置き換えます。

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

/**
 * Replace this with the name of an existing template.
 */
const TEMPLATE_NAME = getUniqueName("ReminderTemplate");

/**
 * Replace these with existing verified emails.
 */
const VERIFIED_EMAIL_1 = postfix(getUniqueName("Bilbo"), "@example.com");
const VERIFIED_EMAIL_2 = postfix(getUniqueName("Frodo"), "@example.com");

const USERS = [
  { firstName: "Bilbo", emailAddress: VERIFIED_EMAIL_1 },
  { firstName: "Frodo", emailAddress: VERIFIED_EMAIL_2 },
];

/**
 *
 * @param { { emailAddress: string, firstName: string }[] } users
 * @param { string } templateName the name of an existing template in SES
 * @returns { SendBulkTemplatedEmailCommand }
 */
const createBulkReminderEmailCommand = (users, templateName) => {
  return new SendBulkTemplatedEmailCommand({
    /**
     * Each 'Destination' uses a corresponding set of replacement data. We can map each user
     * to a 'Destination' and provide user specific replacement data to create personalized emails.
     *
     * Here's an example of how a template would be replaced with user data:
     * Template: <h1>Hello {{name}},</h1><p>Don't forget about the party gifts!</p>
     * Destination 1: <h1>Hello Bilbo,</h1><p>Don't forget about the party gifts!</p>
     * Destination 2: <h1>Hello Frodo,</h1><p>Don't forget about the party gifts!</p>
     */
    Destinations: users.map((user) => ({
      Destination: { ToAddresses: [user.emailAddress] },
      ReplacementTemplateData: JSON.stringify({ name: user.firstName }),
    })),
    DefaultTemplateData: JSON.stringify({ name: "Shireling" }),
    Source: VERIFIED_EMAIL_1,
    Template: templateName,
  });
};

const run = async () => {
  const sendBulkTemplateEmailCommand = createBulkReminderEmailCommand(
    USERS,
    TEMPLATE_NAME,
  );
  try {
    return await sesClient.send(sendBulkTemplateEmailCommand);
  } catch (caught) {
    if (caught instanceof Error && caught.name === "MessageRejected") {
      /** @type { import('@aws-sdk/client-ses').MessageRejected} */
      const messageRejectedError = caught;
      return messageRejectedError;
    }
    throw caught;
  }
};
```

この例を実行するには、コマンドプロンプトで以下を入力します。E メールは Amazon SES による送信のためにキューに登録されます。

```
node ses_sendbulktemplatedemail.js
```

このサンプルコードは、[このGitHubに](https://github.com/awsdocs/aws-doc-sdk-examples/blob/main/javascriptv3/example_code/ses/src/ses_sendbulktemplatedemail.js)で見つけられます。