

文档 AWS SDK 示例 GitHub 存储库中还有更多 [S AWS DK 示例](https://github.com/awsdocs/aws-doc-sdk-examples)。

本文属于机器翻译版本。若本译文内容与英语原文存在差异，则一律以英文原文为准。

# 使用 Amazon SES 示例 AWS CLI
<a name="cli_2_ses_code_examples"></a>

以下代码示例向您展示了如何在 Amazon SES 中使用来执行操作和实现常见场景。 AWS Command Line Interface 

*操作*是大型程序的代码摘录，必须在上下文中运行。您可以通过操作了解如何调用单个服务函数，还可以通过函数相关场景的上下文查看操作。

每个示例都包含一个指向完整源代码的链接，您可以从中找到有关如何在上下文中设置和运行代码的说明。

**Topics**
+ [操作](#actions)

## 操作
<a name="actions"></a>

### `delete-identity`
<a name="ses_DeleteIdentity_cli_2_topic"></a>

以下代码示例演示了如何使用 `delete-identity`。

**AWS CLI**  
**删除身份**  
以下示例使用 `delete-identity` 命令从通过 Amazon SES 验证的身份列表中删除身份：  

```
aws ses delete-identity --identity user@example.com
```
有关已验证身份的更多信息，请参阅《Amazon Simple Email Service 开发人员指南》**中的“在 Amazon SES 中验证电子邮件地址和域”。  
+  有关 API 的详细信息，请参阅*AWS CLI 命令参考[DeleteIdentity](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/ses/delete-identity.html)*中的。

### `get-identity-dkim-attributes`
<a name="ses_GetIdentityDkimAttributes_cli_2_topic"></a>

以下代码示例演示了如何使用 `get-identity-dkim-attributes`。

**AWS CLI**  
**获取身份列表的 Amazon SES Easy DKIM 属性**  
以下示例使用 `get-identity-dkim-attributes` 命令检索身份列表的 Amazon SES Easy DKIM 属性：  

```
aws ses get-identity-dkim-attributes --identities "example.com" "user@example.com"
```
输出：  

```
{
   "DkimAttributes": {
       "example.com": {
           "DkimTokens": [
               "EXAMPLEjcs5xoyqytjsotsijas7236gr",
               "EXAMPLEjr76cvoc6mysspnioorxsn6ep",
               "EXAMPLEkbmkqkhlm2lyz77ppkulerm4k"
           ],
           "DkimEnabled": true,
           "DkimVerificationStatus": "Success"
       },
       "user@example.com": {
           "DkimEnabled": false,
           "DkimVerificationStatus": "NotStarted"
       }
   }
}
```
如果调用此命令时，列表包含从未提交验证的身份，则该身份将不会出现在输出中。  
有关 Easy DKIM 的更多信息，请参阅《Amazon Simple Email Service 开发人员指南》**中的“Amazon SES 中的 Easy DKIM”。  
+  有关 API 的详细信息，请参阅*AWS CLI 命令参考[GetIdentityDkimAttributes](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/ses/get-identity-dkim-attributes.html)*中的。

### `get-identity-notification-attributes`
<a name="ses_GetIdentityNotificationAttributes_cli_2_topic"></a>

以下代码示例演示了如何使用 `get-identity-notification-attributes`。

**AWS CLI**  
**获取身份列表的 Amazon SES 通知属性**  
以下示例使用 `get-identity-notification-attributes` 命令检索身份列表的 Amazon SES 通知属性：  

```
aws ses get-identity-notification-attributes --identities "user1@example.com" "user2@example.com"
```
输出：  

```
{
   "NotificationAttributes": {
       "user1@example.com": {
           "ForwardingEnabled": false,
           "ComplaintTopic": "arn:aws:sns:us-east-1:EXAMPLE65304:MyTopic",
           "BounceTopic": "arn:aws:sns:us-east-1:EXAMPLE65304:MyTopic",
           "DeliveryTopic": "arn:aws:sns:us-east-1:EXAMPLE65304:MyTopic"
       },
       "user2@example.com": {
           "ForwardingEnabled": true
       }
   }
}
```
此命令返回电子邮件反馈转发的状态，如果适用，还会返回向其发送退信、投诉和送达通知的 Amazon SNS 主题的亚马逊资源名称 (ARNs)。  
如果调用此命令时，列表包含从未提交验证的身份，则该身份将不会出现在输出中。  
有关通知的更多信息，请参阅《Amazon Simple Email Service 开发人员指南》**中的“使用 Amazon SES 的通知”。  
+  有关 API 的详细信息，请参阅*AWS CLI 命令参考[GetIdentityNotificationAttributes](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/ses/get-identity-notification-attributes.html)*中的。

### `get-identity-verification-attributes`
<a name="ses_GetIdentityVerificationAttributes_cli_2_topic"></a>

以下代码示例演示了如何使用 `get-identity-verification-attributes`。

**AWS CLI**  
**获取身份列表的 Amazon SES 验证状态**  
以下示例使用 `get-identity-verification-attributes` 命令检索身份列表的 Amazon SES 验证状态：  

```
aws ses get-identity-verification-attributes --identities "user1@example.com" "user2@example.com"
```
输出：  

```
{
   "VerificationAttributes": {
       "user1@example.com": {
           "VerificationStatus": "Success"
       },
       "user2@example.com": {
           "VerificationStatus": "Pending"
       }
   }
}
```
如果调用此命令时，列表包含从未提交验证的身份，则该身份将不会出现在输出中。  
有关已验证身份的更多信息，请参阅《Amazon Simple Email Service 开发人员指南》**中的“在 Amazon SES 中验证电子邮件地址和域”。  
+  有关 API 的详细信息，请参阅*AWS CLI 命令参考[GetIdentityVerificationAttributes](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/ses/get-identity-verification-attributes.html)*中的。

### `get-send-quota`
<a name="ses_GetSendQuota_cli_2_topic"></a>

以下代码示例演示了如何使用 `get-send-quota`。

**AWS CLI**  
**获取 Amazon SES 发送限制**  
以下示例使用 `get-send-quota` 命令返回 Amazon SES 发送限制：  

```
aws ses get-send-quota
```
输出：  

```
{
   "Max24HourSend": 200.0,
   "SentLast24Hours": 1.0,
   "MaxSendRate": 1.0
}
```
Max24 HourSend 是您的发送配额，这是您在 24 小时内可以发送的最大电子邮件数量。发送配额反映一个滚动的时段。每当您尝试发送电子邮件时，Amazon SES 都会检查您在过去 24 小时内发送的电子邮件数量。只要您发送电子邮件总数小于您的配额，发送请求就会被接受，并发送您的电子邮件。  
SentLast24 小时是您在过去 24 小时内发送的电子邮件数量。  
MaxSendRate 是您每秒可以发送的最大电子邮件数。  
请注意，发送限制基于收件人而不是消息。例如，一封包含 10 个收件人的电子邮件占用 10 份发送配额。  
有关更多信息，请参阅《Amazon Simple Email Service 开发人员指南》**中的“管理您的 Amazon SES 发送限制”。  
+  有关 API 的详细信息，请参阅*AWS CLI 命令参考[GetSendQuota](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/ses/get-send-quota.html)*中的。

### `get-send-statistics`
<a name="ses_GetSendStatistics_cli_2_topic"></a>

以下代码示例演示了如何使用 `get-send-statistics`。

**AWS CLI**  
**获取 Amazon SES 发送统计信息**  
以下示例使用 `get-send-statistics` 命令返回 Amazon SES 发送统计信息  

```
aws ses get-send-statistics
```
输出：  

```
{
   "SendDataPoints": [
       {
           "Complaints": 0,
           "Timestamp": "2013-06-12T19:32:00Z",
           "DeliveryAttempts": 2,
           "Bounces": 0,
           "Rejects": 0
       },
       {
           "Complaints": 0,
           "Timestamp": "2013-06-12T00:47:00Z",
           "DeliveryAttempts": 1,
           "Bounces": 0,
           "Rejects": 0
       }
   ]
}
```
结果是数据点的列表，表示过去两周的发送活动。列表中的每个数据点都包含 15 分钟间隔的统计信息。  
在此示例中，只有两个数据点，因为用户在过去两周内发送的唯一电子邮件是在两个 15 分钟间隔内发送的。  
有关更多信息，请参阅《Amazon Simple Email Service 开发人员指南》**中的“监控您的 Amazon SES 使用情况统计信息”。  
+  有关 API 的详细信息，请参阅*AWS CLI 命令参考[GetSendStatistics](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/ses/get-send-statistics.html)*中的。

### `list-identities`
<a name="ses_ListIdentities_cli_2_topic"></a>

以下代码示例演示了如何使用 `list-identities`。

**AWS CLI**  
**列出特定 AWS 账户的所有身份（电子邮件地址和域名）**  
以下示例使用 `list-identities` 命令列出已提交到 Amazon SES 进行验证的所有身份：  

```
aws ses list-identities
```
输出：  

```
{
    "Identities": [
      "user@example.com",
      "example.com"
    ]
}
```
返回的列表包含所有身份，无论验证状态如何（已验证、待验证、失败等）。  
在此示例中，由于未指定 identity-type 参数，因此返回了电子邮件地址*和* 域。  
有关验证的更多信息，请参阅《Amazon Simple Email Service 开发人员指南》**中的“在 Amazon SES 中验证电子邮件地址和域”。  
+  有关 API 的详细信息，请参阅*AWS CLI 命令参考[ListIdentities](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/ses/list-identities.html)*中的。

### `send-email`
<a name="ses_SendEmail_cli_2_topic"></a>

以下代码示例演示了如何使用 `send-email`。

**AWS CLI**  
**使用 Amazon SES 发送格式化的电子邮件**  
以下示例使用 `send-email` 命令发送格式化的电子邮件：  

```
aws ses send-email --from sender@example.com --destination file://destination.json --message file://message.json
```
输出：  

```
{
   "MessageId": "EXAMPLEf3a5efcd1-51adec81-d2a4-4e3f-9fe2-5d85c1b23783-000000"
}
```
目标和消息是 JSON 数据结构，保存在当前目录下的 .json 文件中。这些文件如下所示：  
`destination.json`:  

```
{
  "ToAddresses":  ["recipient1@example.com", "recipient2@example.com"],
  "CcAddresses":  ["recipient3@example.com"],
  "BccAddresses": []
}
```
`message.json`:  

```
{
   "Subject": {
       "Data": "Test email sent using the AWS CLI",
       "Charset": "UTF-8"
   },
   "Body": {
       "Text": {
           "Data": "This is the message body in text format.",
           "Charset": "UTF-8"
       },
       "Html": {
           "Data": "This message body contains HTML formatting. It can, for example, contain links like this one: <a class=\"ulink\" href=\"http://docs.aws.amazon.com/ses/latest/DeveloperGuide\" target=\"_blank\">Amazon SES Developer Guide</a>.",
           "Charset": "UTF-8"
       }
   }
}
```
请将发件人和收件人的电子邮件地址替换为您要使用的电子邮件地址。请注意，发件人的电子邮件地址必须已通过 Amazon SES 验证。在您获得 Amazon SES 的生产访问权限之前，您还必须验证每个收件人的电子邮件地址，除非收件人是 Amazon SES 邮箱模拟器。有关验证的更多信息，请参阅《Amazon Simple Email Service 开发人员指南》**中的“在 Amazon SES 中验证电子邮件地址和域”。  
输出中的消息 ID 表示 send-email 调用成功。  
如果您没有收到电子邮件，请检查垃圾邮件。  
有关发送格式化电子邮件的更多信息，请参阅《Amazon Simple Email Service 开发人员指南》**中的“使用 Amazon SES API 发送格式化的电子邮件”。  
+  有关 API 的详细信息，请参阅*AWS CLI 命令参考[SendEmail](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/ses/send-email.html)*中的。

### `send-raw-email`
<a name="ses_SendRawEmail_cli_2_topic"></a>

以下代码示例演示了如何使用 `send-raw-email`。

**AWS CLI**  
**使用 Amazon SES 发送原始电子邮件**  
以下示例使用 `send-raw-email` 命令发送带有 TXT 附件的电子邮件：  

```
aws ses send-raw-email --raw-message file://message.json
```
输出：  

```
{
   "MessageId": "EXAMPLEf3f73d99b-c63fb06f-d263-41f8-a0fb-d0dc67d56c07-000000"
}
```
原始消息是一个 JSON 数据结构，保存在当前目录下名为 `message.json` 的文件中。其中包含以下内容：  

```
{
   "Data": "From: sender@example.com\nTo: recipient@example.com\nSubject: Test email sent using the AWS CLI (contains an attachment)\nMIME-Version: 1.0\nContent-type: Multipart/Mixed; boundary=\"NextPart\"\n\n--NextPart\nContent-Type: text/plain\n\nThis is the message body.\n\n--NextPart\nContent-Type: text/plain;\nContent-Disposition: attachment; filename=\"attachment.txt\"\n\nThis is the text in the attachment.\n\n--NextPart--"
}
```
如您所见，“Data”是一个长字符串，包含 MIME 格式的全部原始电子邮件内容，其中包括一个名为 attachment.txt 的附件。  
请将 sender@example.com 和 recipient@example.com 替换为您要使用的地址。请注意，发件人的电子邮件地址必须已通过 Amazon SES 验证。在您获得 Amazon SES 的生产访问权限之前，您还必须验证收件人的电子邮件地址，除非收件人是 Amazon SES 邮箱模拟器。有关验证的更多信息，请参阅《Amazon Simple Email Service 开发人员指南》**中的“在 Amazon SES 中验证电子邮件地址和域”。  
输出中的消息 ID 表示对的调用 send-raw-email成功。  
如果您没有收到电子邮件，请检查垃圾邮件。  
有关发送原始电子邮件的更多信息，请参阅《Amazon Simple Email Service 开发人员指南》**中的“使用 Amazon SES API 发送原始电子邮件”。  
+  有关 API 的详细信息，请参阅*AWS CLI 命令参考[SendRawEmail](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/ses/send-raw-email.html)*中的。

### `set-identity-dkim-enabled`
<a name="ses_SetIdentityDkimEnabled_cli_2_topic"></a>

以下代码示例演示了如何使用 `set-identity-dkim-enabled`。

**AWS CLI**  
**为经 Amazon SES 验证的身份启用或禁用 Easy DKIM**  
以下示例使用 `set-identity-dkim-enabled` 命令禁用已验证电子邮件地址的 DKIM：  

```
aws ses set-identity-dkim-enabled --identity user@example.com --no-dkim-enabled
```
有关 Easy DKIM 的更多信息，请参阅《Amazon Simple Email Service 开发人员指南》**中的“Amazon SES 中的 Easy DKIM”。  
+  有关 API 的详细信息，请参阅*AWS CLI 命令参考[SetIdentityDkimEnabled](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/ses/set-identity-dkim-enabled.html)*中的。

### `set-identity-feedback-forwarding-enabled`
<a name="ses_SetIdentityFeedbackForwardingEnabled_cli_2_topic"></a>

以下代码示例演示了如何使用 `set-identity-feedback-forwarding-enabled`。

**AWS CLI**  
**为经 Amazon SES 验证的身份启用或禁用退信和投诉电子邮件反馈转发**  
以下示例使用 `set-identity-feedback-forwarding-enabled` 命令使经验证的电子邮件地址能够通过电子邮件接收退信和投诉通知：  

```
aws ses set-identity-feedback-forwarding-enabled --identity user@example.com --forwarding-enabled
```
您必须通过 Amazon SNS 或电子邮件反馈转发接收退信和投诉通知，因此，只有在为退信和投诉通知选择 Amazon SNS 主题时，才可以禁用电子邮件反馈转发。  
有关通知的更多信息，请参阅《Amazon Simple Email Service 开发人员指南》**中的“使用 Amazon SES 的通知”。  
+  有关 API 的详细信息，请参阅*AWS CLI 命令参考[SetIdentityFeedbackForwardingEnabled](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/ses/set-identity-feedback-forwarding-enabled.html)*中的。

### `set-identity-notification-topic`
<a name="ses_SetIdentityNotificationTopic_cli_2_topic"></a>

以下代码示例演示了如何使用 `set-identity-notification-topic`。

**AWS CLI**  
**设置 Amazon SES 将向其发布已验证身份的退信、投诉、 and/or 送达通知的 Amazon SNS 主题**  
以下示例使用 `set-identity-notification-topic` 命令指定经验证的电子邮件地址将接收退信通知的 Amazon SNS 主题：  

```
aws ses set-identity-notification-topic --identity user@example.com --notification-type Bounce --sns-topic arn:aws:sns:us-east-1:EXAMPLE65304:MyTopic
```
有关通知的更多信息，请参阅《Amazon Simple Email Service 开发人员指南》**中的“使用 Amazon SES 的通知”。  
+  有关 API 的详细信息，请参阅*AWS CLI 命令参考[SetIdentityNotificationTopic](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/ses/set-identity-notification-topic.html)*中的。

### `verify-domain-dkim`
<a name="ses_VerifyDomainDkim_cli_2_topic"></a>

以下代码示例演示了如何使用 `verify-domain-dkim`。

**AWS CLI**  
**生成经验证的域的 DKIM 令牌，以便使用 Amazon SES 进行 DKIM 签名**  
以下示例使用 `verify-domain-dkim` 命令为已通过 Amazon SES 验证的域生成 DKIM 令牌：  

```
aws ses verify-domain-dkim --domain example.com
```
输出：  

```
{
   "DkimTokens": [
       "EXAMPLEq76owjnks3lnluwg65scbemvw",
       "EXAMPLEi3dnsj67hstzaj673klariwx2",
       "EXAMPLEwfbtcukvimehexktmdtaz6naj"
   ]
}
```
要设置 DKIM，您必须使用返回的 DKIM 令牌更新域名的 DNS 设置，使其具有指向 Amazon SES 托管的 DKIM 公钥的 CNAME 记录。有关更多信息，请参阅《Amazon Simple Email Service 开发人员指南》**中的“Amazon SES 中的 Easy DKIM”。  
+  有关 API 的详细信息，请参阅*AWS CLI 命令参考[VerifyDomainDkim](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/ses/verify-domain-dkim.html)*中的。

### `verify-domain-identity`
<a name="ses_VerifyDomainIdentity_cli_2_topic"></a>

以下代码示例演示了如何使用 `verify-domain-identity`。

**AWS CLI**  
**使用 Amazon SES 验证域**  
以下示例使用 `verify-domain-identity` 命令验证域：  

```
aws ses verify-domain-identity --domain example.com
```
输出：  

```
{
   "VerificationToken": "eoEmxw+YaYhb3h3iVJHuXMJXqeu1q1/wwmvjuEXAMPLE"
}
```
要完成域验证，您必须在域的 DNS 设置中添加一条包含返回的验证令牌的 TXT 记录。有关更多信息，请参阅《Amazon Simple Email Service 开发人员指南》**中的“在 Amazon SES 中验证域”。  
+  有关 API 的详细信息，请参阅*AWS CLI 命令参考[VerifyDomainIdentity](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/ses/verify-domain-identity.html)*中的。

### `verify-email-identity`
<a name="ses_VerifyEmailIdentity_cli_2_topic"></a>

以下代码示例演示了如何使用 `verify-email-identity`。

**AWS CLI**  
**使用 Amazon SES 验证电子邮件地址**  
以下示例使用 `verify-email-identity` 命令验证电子邮件地址：  

```
aws ses verify-email-identity --email-address user@example.com
```
在使用 Amazon SES 发送电子邮件之前，您必须验证从中发送电子邮件的地址或域，以证明您拥有该地址或域。如果您尚未获得生产访问权限，则还需要验证所有收件电子邮件地址，Amazon SES 邮箱模拟器提供的电子邮件地址除外。  
接 verify-email-identity到电话后，该电子邮件地址将收到一封验证电子邮件。用户必须单击此电子邮件中的链接才能完成验证过程。  
有关更多信息，请参阅《Amazon Simple Email Service 开发人员指南》**中的“在 Amazon SES 中验证电子邮件地址”。  
+  有关 API 的详细信息，请参阅*AWS CLI 命令参考[VerifyEmailIdentity](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/ses/verify-email-identity.html)*中的。