

适用于 JavaScript 的 Amazon SDK v2 已终止支持。建议您迁移到 [适用于 JavaScript 的 Amazon SDK v3](https://docs.amazonaws.cn//sdk-for-javascript/v3/developer-guide/)。有关更多详情和如何迁移的信息，请参阅本[公告](https://www.amazonaws.cn/blogs//developer/announcing-end-of-support-for-aws-sdk-for-javascript-v2/)。

# 在 Amazon SES 中使用接收规则
<a name="ses-examples-receipt-rules"></a>

![JavaScript code example that applies to Node.js execution](http://docs.amazonaws.cn/sdk-for-javascript/v2/developer-guide/images/nodeicon.png)

**此 Node.js 代码示例演示：**
+ 创建和删除接收规则。
+ 将接收规则组织为接收规则集。

Amazon SES 中的接收规则指定从您拥有的电子邮件地址或域接收电子邮件后，将执行哪些操作。*接收规则* 包含一个条件和一个有序操作列表。如果传入电子邮件的收件人与接收规则条件中指定的收件人相匹配，则 Amazon SES 执行接收规则指定的操作。

要使用 Amazon SES 作为电子邮件接收方，您必须至少具有一个有效*接收规则集*。接收规则集是接收规则的有序集合，用于指定 Amazon SES 如何处理从您的已验证域接收的邮件。有关更多信息，请参阅《Amazon Simple Email Service 开发者指南》中的[为 Amazon SES 电子邮件接收创建接收规则](Amazon Simple Email Service Developer Guidereceiving-email-receipt-rules.html)和[为 Amazon SES 电子邮件接收创建接收规则集](Amazon Simple Email Service Developer Guidereceiving-email-receipt-rule-set.html)。

## 情景
<a name="ses-examples-receipt-rules-scenario"></a>

在本示例中，使用了一系列 Node.js 模块以多种方式发送电子邮件。这些 Node.js 模块使用 SDK for JavaScript，通过 `AWS.SES` 客户端类的以下方法来创建和使用电子邮件模板：
+ [https://docs.amazonaws.cn/AWSJavaScriptSDK/latest/AWS/SES.html#createReceiptRule-property](https://docs.amazonaws.cn/AWSJavaScriptSDK/latest/AWS/SES.html#createReceiptRule-property)
+ [https://docs.amazonaws.cn/AWSJavaScriptSDK/latest/AWS/SES.html#deleteReceiptRule-property](https://docs.amazonaws.cn/AWSJavaScriptSDK/latest/AWS/SES.html#deleteReceiptRule-property)
+ [https://docs.amazonaws.cn/AWSJavaScriptSDK/latest/AWS/SES.html#createReceiptRuleSet-property](https://docs.amazonaws.cn/AWSJavaScriptSDK/latest/AWS/SES.html#createReceiptRuleSet-property)
+ [https://docs.amazonaws.cn/AWSJavaScriptSDK/latest/AWS/SES.html#deleteReceiptRuleSet-property](https://docs.amazonaws.cn/AWSJavaScriptSDK/latest/AWS/SES.html#deleteReceiptRuleSet-property)

## 先决条件任务
<a name="ses-examples-receipt-rules-prerequisites"></a>

要设置和运行此示例，您必须先完成以下任务：
+ 安装 Node.js。有关安装 Node.js 的更多信息，请参阅 [Node.js 网站](https://nodejs.org)。
+ 使用用户凭证创建共享配置文件。有关提供凭证 JSON 文件的更多信息，请参阅[从共享凭证文件加载 Node.js 中的凭证](loading-node-credentials-shared.md)。

## 创建 Amazon S3 接收规则
<a name="ses-examples-creatingreceipt-rules"></a>

Amazon SES 的每个接收规则都包含一组有序操作。此示例创建具有 Amazon S3 操作的接收规则，该操作将邮件传送到 Amazon S3 存储桶。有关接收规则操作的详细信息，请参阅《Amazon Simple Email Service 开发人员指南》中的[操作选项](Amazon Simple Email Service Developer Guidereceiving-email-action.html)。

要使 Amazon SES 将电子邮件写入 Amazon S3 存储桶，请创建向 Amazon SES 提供 `PutObject` 权限的存储桶策略。有关创建此桶策略的信息，请参阅《Amazon Simple Email Service 开发人员指南》中的[授予 Amazon SES 写入 S3 存储桶的权限](Amazon Simple Email Service Developer Guidereceiving-email-permissions.html#receiving-email-permissions-s3.html)。

本示例使用 Node.js 模块在 Amazon SES 中创建接收规则，将收到的邮件保存到 Amazon S3 桶中。创建文件名为 `ses_createreceiptrule.js` 的 Node.js 模块。按前面所示配置 SDK。

创建一个参数对象来传递创建接收规则集所需的值。要调用 `createReceiptRuleSet` 方法，请创建一个 promise 来调用 Amazon SES 服务对象并传递参数。然后处理 promise 回调中的 `response`。

```
// Load the AWS SDK for Node.js
var AWS = require("aws-sdk");
// Set the region
AWS.config.update({ region: "REGION" });

// Create createReceiptRule params
var params = {
  Rule: {
    Actions: [
      {
        S3Action: {
          BucketName: "S3_BUCKET_NAME",
          ObjectKeyPrefix: "email",
        },
      },
    ],
    Recipients: [
      "DOMAIN | EMAIL_ADDRESS",
      /* more items */
    ],
    Enabled: true | false,
    Name: "RULE_NAME",
    ScanEnabled: true | false,
    TlsPolicy: "Optional",
  },
  RuleSetName: "RULE_SET_NAME",
};

// Create the promise and SES service object
var newRulePromise = new AWS.SES({ apiVersion: "2010-12-01" })
  .createReceiptRule(params)
  .promise();

// Handle promise's fulfilled/rejected states
newRulePromise
  .then(function (data) {
    console.log("Rule created");
  })
  .catch(function (err) {
    console.error(err, err.stack);
  });
```

要运行示例，请在命令行中键入以下内容。Amazon SES 将创建接收规则。

```
node ses_createreceiptrule.js
```

此示例代码可在 [GitHub 上的此处](https://github.com/awsdocs/aws-doc-sdk-examples/tree/master/javascript/example_code/ses/ses_createreceiptrule.js)找到。

## 删除接收规则
<a name="ses-examples-deletingreceipt-rules"></a>

在本示例中，使用 Node.js 模块通过 Amazon SES 发送电子邮件。创建文件名为 `ses_deletereceiptrule.js` 的 Node.js 模块。按前面所示配置 SDK。

创建一个参数对象以传递要删除的接收规则的名称。要调用 `deleteReceiptRule` 方法，请创建一个 promise 来调用 Amazon SES 服务对象并传递参数。然后处理 promise 回调中的 `response`。

```
// Load the AWS SDK for Node.js
var AWS = require("aws-sdk");
// Set the region
AWS.config.update({ region: "REGION" });

// Create deleteReceiptRule params
var params = {
  RuleName: "RULE_NAME" /* required */,
  RuleSetName: "RULE_SET_NAME" /* required */,
};

// Create the promise and SES service object
var newRulePromise = new AWS.SES({ apiVersion: "2010-12-01" })
  .deleteReceiptRule(params)
  .promise();

// Handle promise's fulfilled/rejected states
newRulePromise
  .then(function (data) {
    console.log("Receipt Rule Deleted");
  })
  .catch(function (err) {
    console.error(err, err.stack);
  });
```

要运行示例，请在命令行中键入以下内容。Amazon SES 将创建接收规则集列表。

```
node ses_deletereceiptrule.js
```

此示例代码可在 [GitHub 上的此处](https://github.com/awsdocs/aws-doc-sdk-examples/tree/master/javascript/example_code/ses/ses_deletereceiptrule.js)找到。

## 创建接收规则集
<a name="ses-examples-creatingreceiptrulesets"></a>

在本示例中，使用 Node.js 模块通过 Amazon SES 发送电子邮件。创建文件名为 `ses_createreceiptruleset.js` 的 Node.js 模块。按前面所示配置 SDK。

创建一个参数对象以传递新接收规则集的名称。要调用 `createReceiptRuleSet` 方法，请创建一个 promise 来调用 Amazon SES 服务对象并传递参数。然后处理 promise 回调中的 `response`。

```
// Load the AWS SDK for Node.js
var AWS = require("aws-sdk");
// Set the region
AWS.config.update({ region: "REGION" });

// Create the promise and SES service object
var newRulePromise = new AWS.SES({ apiVersion: "2010-12-01" })
  .createReceiptRuleSet({ RuleSetName: "NAME" })
  .promise();

// Handle promise's fulfilled/rejected states
newRulePromise
  .then(function (data) {
    console.log(data);
  })
  .catch(function (err) {
    console.error(err, err.stack);
  });
```

要运行示例，请在命令行中键入以下内容。Amazon SES 将创建接收规则集列表。

```
node ses_createreceiptruleset.js
```

此示例代码可在 [GitHub 上的此处](https://github.com/awsdocs/aws-doc-sdk-examples/tree/master/javascript/example_code/ses/ses_createreceiptruleset.js)找到。

## 删除接收规则集
<a name="ses-examples-deletingreceiptrulesets"></a>

在本示例中，使用 Node.js 模块通过 Amazon SES 发送电子邮件。创建文件名为 `ses_deletereceiptruleset.js` 的 Node.js 模块。按前面所示配置 SDK。

创建一个对象以传递要删除的接收规则集的名称。要调用 `deleeReceiptRuleSet` 方法，请创建一个 promise 来调用 Amazon SES 服务对象并传递参数。然后处理 promise 回调中的 `response`。

```
// Load the AWS SDK for Node.js
var AWS = require("aws-sdk");
// Set the region
AWS.config.update({ region: "REGION" });

// Create the promise and SES service object
var newRulePromise = new AWS.SES({ apiVersion: "2010-12-01" })
  .deleteReceiptRuleSet({ RuleSetName: "NAME" })
  .promise();

// Handle promise's fulfilled/rejected states
newRulePromise
  .then(function (data) {
    console.log(data);
  })
  .catch(function (err) {
    console.error(err, err.stack);
  });
```

要运行示例，请在命令行中键入以下内容。Amazon SES 将创建接收规则集列表。

```
node ses_deletereceiptruleset.js
```

此示例代码可在 [GitHub 上的此处](https://github.com/awsdocs/aws-doc-sdk-examples/tree/master/javascript/example_code/ses/ses_deletereceiptruleset.js)找到。