View a markdown version of this page

使用 Amazon SES 发送电子邮件 AWS SDK - Amazon Simple Email Service

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

使用 Amazon SES 发送电子邮件 AWS SDK

您可以使用 AWS 软件开发工具包通过亚马逊 SES 发送电子邮件。 AWS SDK 可用于多种编程语言。有关更多信息,请参阅用于 Amazon Web Services 的工具

先决条件

要完成下一节中的任何代码示例,必须完成以下先决条件:

  • 如果您尚未执行此操作,请完成设置 Amazon Simple Email Service中的任务。

  • 使用 Amazon SES 验证您的电子邮件地址 – 您必须先验证您拥有发件人的电子邮件地址,然后才能使用 Amazon SES 发送电子邮件。如果您的账户仍在 Amazon SES 沙盒中,您还必须验证收件人的电子邮件地址。我们建议您使用 Amazon SES 控制台来验证电子邮件地址。有关更多信息,请参阅 创建电子邮件地址身份

  • 获取您的 AWS 证书 — 您需要访问密钥 ID 和 AWS 私有 AWS 访问密钥才能使用 SDK 访问 Amazon SES。您可以使用 中的安全凭证 AWS 管理控制台页面来查找您的凭证。有关凭证的更多信息,请参阅Amazon SES 凭证的类型

  • 创建共享凭证文件 – 为了使此部分中的示例代码正常运行,您必须创建一个共享凭证文件。有关更多信息,请参阅 创建共享凭证文件以在使用 Amazon SES 发送电子邮件时使用 AWS SDK

代码示例

重要

在以下教程中,您将向自己发送电子邮件,以便检查是否收到了该电子邮件。如需进一步试验或进行负载测试,请使用 Amazon SES 邮箱模拟器。您发送到邮箱模拟器的电子邮件不会计入您的发送配额或您的退信率和投诉率。有关更多信息,请参阅 手动使用邮箱模拟器

选择一种编程语言以查看该语言的示例:
    .NET

    以下过程介绍如何使用 Visual Studio 和 AWS SDK for .NET通过 Amazon SES 发送电子邮件。

    已使用以下组件测试此解决方案:

    • Microsoft Visual Studio Community 2017 版本 15.4.0。

    • Microsoft .NET Framework 版本 4.6.1。

    • 该 AWSSDK.Core 软件包(版本 3.3.19),使用安装。 NuGet

    • 该 AWSSDK.SimpleEmail 软件包(版本 3.3.6.1),使用安装。 NuGet

    在开始前,请执行以下任务:
    要使用发送电子邮件 AWS SDK for .NET
    1. 通过执行以下步骤创建新项目:

      1. 启动 Visual Studio。

      2. File 菜单上,依次选择 NewProject

      3. New Project 窗口上的左侧面板中,展开 Installed,然后展开 Visual C#

      4. 在右侧面板中,选择 Console App(.NET Framework)

      5. 对于名称,键入 AmazonSESSample,然后选择 确定

    2. 通过完成以下步骤,使用 NuGet 将亚马逊 SES 包添加到您的解决方案中:

      1. 在 “解决方案资源管理器” 窗格中,右键单击您的项目,然后选择 “管理 NuGet 包”

      2. NuGet:AmazonsesSample 选项卡上,选择浏览。

      3. 在搜索框中,键入 AWSSDK.SimpleEmail

      4. 选择AWSSDK.SimpleEmail软件包,然后选择 “安装”

      5. Preview Changes 窗口中,选择 OK

    3. Program.cs选项卡上,粘贴以下代码:

      using Amazon; using System; using System.Collections.Generic; using Amazon.SimpleEmail; using Amazon.SimpleEmail.Model; namespace AmazonSESSample { class Program { // Replace sender@example.com with your "From" address. // This address must be verified with Amazon SES. static readonly string senderAddress = "sender@example.com"; // Replace recipient@example.com with a "To" address. If your account // is still in the sandbox, this address must be verified. static readonly string receiverAddress = "recipient@example.com"; // The configuration set to use for this email. If you do not want to use a // configuration set, comment out the following property and the // ConfigurationSetName = configSet argument below. static readonly string configSet = "ConfigSet"; // The subject line for the email. static readonly string subject = "Amazon SES test (AWS SDK for .NET)"; // The email body for recipients with non-HTML email clients. static readonly string textBody = "Amazon SES Test (.NET)\r\n" + "This email was sent through Amazon SES " + "using the AWS SDK for .NET."; // The HTML body of the email. static readonly string htmlBody = @"<html> <head></head> <body> <h1>Amazon SES Test (SDK for .NET)</h1> <p>This email was sent with <a href='https://aws.amazon.com/ses/'>Amazon SES</a> using the <a href='https://aws.amazon.com/sdk-for-net/'> AWS SDK for .NET</a>.</p> </body> </html>"; static void Main(string[] args) { // Replace USWest2 with the AWS Region you're using for Amazon SES. // Acceptable values are EUWest1, USEast1, and USWest2. using (var client = new AmazonSimpleEmailServiceClient(RegionEndpoint.USWest2)) { var sendRequest = new SendEmailRequest { Source = senderAddress, Destination = new Destination { ToAddresses = new List<string> { receiverAddress } }, Message = new Message { Subject = new Content(subject), Body = new Body { Html = new Content { Charset = "UTF-8", Data = htmlBody }, Text = new Content { Charset = "UTF-8", Data = textBody } } }, // If you are not using a configuration set, comment // or remove the following line ConfigurationSetName = configSet }; try { Console.WriteLine("Sending email using Amazon SES..."); var response = client.SendEmail(sendRequest); Console.WriteLine("The email was sent successfully."); } catch (Exception ex) { Console.WriteLine("The email was not sent."); Console.WriteLine("Error message: " + ex.Message); } } Console.Write("Press any key to continue..."); Console.ReadKey(); } } }
    4. 在代码编辑器中,执行下列操作:

      • sender@example.com替换为 “发件人:” 电子邮件地址。必须验证此地址。有关更多信息,请参阅 Amazon SES 中已验证的身份

      • recipient@example.com替换为 “收件人:” 地址。如果您的账户仍处于沙盒中,则还必须验证此地址。

      • ConfigSet替换为发送此电子邮件时使用的配置集的名称。

      • USWest2替换为您用于使用 Amazon SES 发送电子邮件的 AWS 区域 终端节点的名称。有关已推出 Amazon SES 的区域的列表,请参阅《AWS 一般参考》中的 Amazon Simple Email Service(Amazon SES)

      完成后,保存 Program.cs

    5. 通过完成以下步骤来生成并运行应用程序:

      1. Build 菜单上,选择 Build Solution

      2. Debug 菜单上,选择 Start Debugging。此时显示一个控制台窗口。

    6. 检查控制台的输出。如果已成功发送电子邮件,则控制台会显示“The email was sent successfully.

    7. 如果已成功发送电子邮件,请登录收件人地址的电子邮件客户端。您将看到已发送的电子邮件。

    Java

    以下过程向您展示如何使用适用于 Java EE 开发人员的 E clipse IDE,以及AWS Toolkit for Eclipse如何创建 AWS SDK 项目和修改 Java 代码以通过亚马逊 SES 发送电子邮件。

    在开始前,请执行以下任务:
    • 安装 Eclipse —Eclipse 可在https://www.eclipse.org/downloads以下地址获得 本教程中的代码使用运行 Java 运行环境 1.8 版的 Eclipse Neon.3 (版本 4.6.3)进行了测试。

    • 安装 AWS Toolkit for Eclipse —有关在 Eclipse 安装中 AWS Toolkit for Eclipse 添加的说明,请访问。 https://aws.amazon.com/eclipse 本教程中的代码已使用 2.3.1 版本的 AWS Toolkit for Eclipse进行了测试。

    要使用发送电子邮件 AWS SDK for Java
    1. 通过执行以下步骤在 Eclipse 中创建 AWS Java 项目:

      1. 启动 Eclipse。

      2. File 菜单上,选择 New,然后选择 Other。在 New 窗口中,展开 AWS 文件夹,然后选择 AWS Java Project

      3. 在 “新建 AWS Java 项目” 对话框中,执行以下操作:

        1. 对于 Project name,键入项目的名称。

        2. 在 “AWS SDK for Java 示例” 下,选择 “亚马逊简单电子邮件服务 JavaMail 示例”

        3. 选择结束

    2. 在 Eclipse 中的 Package Explorer 窗格中,展开您的项目。

    3. 在您的项目下,展开 src/main/java 文件夹,展开 com.amazon.aws.samples 文件夹,然后双击 AmazonSESSample.java

    4. AmazonSESSample.java 的整个内容替换为以下代码:

      package com.amazonaws.samples; import java.io.IOException; import com.amazonaws.regions.Regions; import com.amazonaws.services.simpleemail.AmazonSimpleEmailService; import com.amazonaws.services.simpleemail.AmazonSimpleEmailServiceClientBuilder; import com.amazonaws.services.simpleemail.model.Body; import com.amazonaws.services.simpleemail.model.Content; import com.amazonaws.services.simpleemail.model.Destination; import com.amazonaws.services.simpleemail.model.Message; import com.amazonaws.services.simpleemail.model.SendEmailRequest; public class AmazonSESSample { // Replace sender@example.com with your "From" address. // This address must be verified with Amazon SES. static final String FROM = "sender@example.com"; // Replace recipient@example.com with a "To" address. If your account // is still in the sandbox, this address must be verified. static final String TO = "recipient@example.com"; // The configuration set to use for this email. If you do not want to use a // configuration set, comment the following variable and the // .withConfigurationSetName(CONFIGSET); argument below. static final String CONFIGSET = "ConfigSet"; // The subject line for the email. static final String SUBJECT = "Amazon SES test (AWS SDK for Java)"; // The HTML body for the email. static final String HTMLBODY = "<h1>Amazon SES test (AWS SDK for Java)</h1>" + "<p>This email was sent with <a href='https://aws.amazon.com/ses/'>" + "Amazon SES</a> using the <a href='https://aws.amazon.com/sdk-for-java/'>" + "AWS SDK for Java</a>"; // The email body for recipients with non-HTML email clients. static final String TEXTBODY = "This email was sent through Amazon SES " + "using the AWS SDK for Java."; public static void main(String[] args) throws IOException { try { AmazonSimpleEmailService client = AmazonSimpleEmailServiceClientBuilder.standard() // Replace US_WEST_2 with the AWS Region you're using for // Amazon SES. .withRegion(Regions.US_WEST_2).build(); SendEmailRequest request = new SendEmailRequest() .withDestination( new Destination().withToAddresses(TO)) .withMessage(new Message() .withBody(new Body() .withHtml(new Content() .withCharset("UTF-8").withData(HTMLBODY)) .withText(new Content() .withCharset("UTF-8").withData(TEXTBODY))) .withSubject(new Content() .withCharset("UTF-8").withData(SUBJECT))) .withSource(FROM) // Comment or remove the next line if you are not using a // configuration set .withConfigurationSetName(CONFIGSET); client.sendEmail(request); System.out.println("Email sent!"); } catch (Exception ex) { System.out.println("The email was not sent. Error message: " + ex.getMessage()); } } }
    5. AmazonSESSample.java 中,将以下内容替换为您自己的值:

      重要

      电子邮件地址区分大小写。请确保此处的地址与经验证的地址完全相同。

      • SENDER@EXAMPLE.COM:替换为您的 From (发件人) 电子邮件地址。运行此程序之前,您必须验证该地址。有关更多信息,请参阅 Amazon SES 中已验证的身份

      • RECIPIENT@EXAMPLE.COM:替换为您的 To (收件人) 电子邮件地址。如果您的账户仍处于沙盒中,您还必须验证此地址,然后才能使用它。有关更多信息,请参阅 请求生产访问权限(从 Amazon SES 沙盒中移出)

      • (可选)us-west-2 - 如果您要在美国西部(俄勒冈州)以外的区域中使用 Amazon SES,请将它替换为您要使用的区域。有关已推出 Amazon SES 的区域的列表,请参阅《AWS 一般参考》中的 Amazon Simple Email Service(Amazon SES)

    6. 保存 AmazonSESSample.java

    7. 要构建项目,请选择 Project,然后选择 Build Project

      注意

      如果禁用此选项,则可能启用自动构建;如果是这样,请跳过此步骤。

    8. 要开始程序和发送电子邮件,请选择 Run,然后再次选择 Run

    9. 在 Eclipse 中查看控制台窗格的输出。如果已成功发送电子邮件,则控制台会显示“Email sent!”,否则将显示一条错误消息。

    10. 如果已成功发送电子邮件,请登录收件人地址的电子邮件客户端。您将看到已发送的电子邮件。

    PHP

    本主题说明如何使用 适用于 PHP 的 AWS SDK 通过 Amazon SES 发送电子邮件。

    在开始前,请执行以下任务:
    • 安装 PHP —PHP 可在以下网址获得。 http://php.net/downloads.php 本教程需要 PHP 版本 5.5 或更高版本。安装 PHP 后,在环境变量中添加 PHP 的路径,这样就能通过任何命令提示符运行 PHP。本教程中的代码已使用 PHP 7.2.7 进行测试。

    • 安装 适用于 PHP 的 AWS SDK 版本 3 —有关下载和安装说明,请参阅适用于 PHP 的 AWS SDK 文档。本教程中的代码已使用版本 3.64.13 的软件开发工具包进行测试。

    要通过 Amazon SES 发送电子邮件,请使用 适用于 PHP 的 AWS SDK
    1. 在文本编辑器中,创建一个名为 amazon-ses-sample.php 的文件。粘贴以下代码:

      <?php // If necessary, modify the path in the require statement below to refer to the // location of your Composer autoload.php file. require 'vendor/autoload.php'; use Aws\Ses\SesClient; use Aws\Exception\AwsException; // Create an SesClient. Change the value of the region parameter if you're // using an AWS Region other than US West (Oregon). Change the value of the // profile parameter if you want to use a profile in your credentials file // other than the default. $SesClient = new SesClient([ 'profile' => 'default', 'version' => '2010-12-01', 'region' => 'us-west-2' ]); // Replace sender@example.com with your "From" address. // This address must be verified with Amazon SES. $sender_email = 'sender@example.com'; // Replace these sample addresses with the addresses of your recipients. If // your account is still in the sandbox, these addresses must be verified. $recipient_emails = ['recipient1@example.com','recipient2@example.com']; // Specify a configuration set. If you do not want to use a configuration // set, comment the following variable, and the // 'ConfigurationSetName' => $configuration_set argument below. $configuration_set = 'ConfigSet'; $subject = 'Amazon SES test (适用于 PHP 的 AWS SDK)'; $plaintext_body = 'This email was sent with Amazon SES using the AWS SDK for PHP.' ; $html_body = '<h1>AWS Amazon Simple Email Service Test Email</h1>'. '<p>This email was sent with <a href="https://aws.amazon.com/ses/">'. 'Amazon SES</a> using the <a href="https://aws.amazon.com/sdk-for-php/">'. '适用于 PHP 的 AWS SDK</a>.</p>'; $char_set = 'UTF-8'; try { $result = $SesClient->sendEmail([ 'Destination' => [ 'ToAddresses' => $recipient_emails, ], 'ReplyToAddresses' => [$sender_email], 'Source' => $sender_email, 'Message' => [ 'Body' => [ 'Html' => [ 'Charset' => $char_set, 'Data' => $html_body, ], 'Text' => [ 'Charset' => $char_set, 'Data' => $plaintext_body, ], ], 'Subject' => [ 'Charset' => $char_set, 'Data' => $subject, ], ], // If you aren't using a configuration set, comment or delete the // following line 'ConfigurationSetName' => $configuration_set, ]); $messageId = $result['MessageId']; echo("Email sent! Message ID: $messageId"."\n"); } catch (AwsException $e) { // output error message if fails echo $e->getMessage(); echo("The email was not sent. Error message: ".$e->getAwsErrorMessage()."\n"); echo "\n"; }
    2. amazon-ses-sample.php 中,将以下内容替换为您自己的值:

      • path_to_sdk_inclusion—替换为将包含在程序 适用于 PHP 的 AWS SDK 中所需的路径。有关详情,请参阅 适用于 PHP 的 AWS SDK 文档

      • sender@example.com – 替换为您已使用 Amazon SES 验证过的电子邮件地址。有关更多信息,请参阅 已验证的身份。Amazon SES 中的电子邮件地址区分大小写。请确保您输入的地址与经验证的地址完全相同。

      • recipient1@example.comrecipient2@example.com:替换为收件人的地址。如果您的账户仍处于沙盒中,则还必须验证收件人地址。有关更多信息,请参阅 请求生产访问权限(从 Amazon SES 沙盒中移出)。请确保您输入的地址与经验证的地址完全相同。

      • (可选)ConfigSet:如果您要在发送此电子邮件时使用配置集,请将此值替换为配置集的名称。有关配置集的更多信息,请参阅在 SES 中使用配置集

      • (可选)us-west-2 - 如果您要在美国西部(俄勒冈州)以外的区域中使用 Amazon SES,请将它替换为您要使用的区域。有关已推出 Amazon SES 的区域的列表,请参阅《AWS 一般参考》中的 Amazon Simple Email Service(Amazon SES)

    3. 保存 amazon-ses-sample.php

    4. 要运行程序,请在 amazon-ses-sample.php 所在的同一目录中打开命令提示符,然后键入以下命令:

      $ php amazon-ses-sample.php
    5. 检查输出。如果已成功发送电子邮件,则控制台会显示“Email sent!”,否则将显示一条错误消息。

      注意

      如果在运行程序时遇到“cURL error 60: SSL certificate problem”错误,请下载最新的 CA 服务包,如适用于 PHP 的 AWS SDK文档中所述。然后,在 amazon-ses-sample.php 中,将以下行添加到 SesClient::factory 数组,将 path_of_certs 替换为您下载的 CA 捆绑的路径,然后重新运行程序。

      'http' => [ 'verify' => 'path_of_certs\ca-bundle.crt' ]
    6. 登录收件人地址的电子邮件客户端。您将看到已发送的电子邮件。

    Ruby

    本主题说明如何使用 适用于 Ruby 的 AWS SDK 通过 Amazon SES 发送电子邮件。

    在开始前,请执行以下任务:
    • 安装 Ruby —Ruby 可在以下网址获得。 https://www.ruby-lang.org/en/downloads/ 本教程中的代码已使用 Ruby 1.9.3 进行测试。安装 Ruby 后,在环境变量中添加 Ruby 的路径,这样就能通过任何命令提示符运行 Ruby。

    • 安装 适用于 Ruby 的 AWS SDK —有关下载和安装说明,请参阅适用于 Ruby 的 AWS SDK 开发人员指南 适用于 Ruby 的 AWS SDK中的安装。本教程中的示例代码已使用 2.9.36 版本的 适用于 Ruby 的 AWS SDK进行了测试。

    • 创建共享凭证文件 – 为了使此部分中的示例代码正常运行,您必须创建一个共享凭证文件。有关更多信息,请参阅 创建共享凭证文件以在使用 Amazon SES 发送电子邮件时使用 AWS SDK

    要通过 Amazon SES 发送电子邮件,请使用 适用于 Ruby 的 AWS SDK
    1. 在文本编辑器中,创建一个名为 amazon-ses-sample.rb 的文件。将以下代码粘贴到该文件中:

      require 'aws-sdk' # Replace sender@example.com with your "From" address. # This address must be verified with Amazon SES. sender = "sender@example.com" # Replace recipient@example.com with a "To" address. If your account # is still in the sandbox, this address must be verified. recipient = "recipient@example.com" # Specify a configuration set. If you do not want to use a configuration # set, comment the following variable and the # configuration_set_name: configsetname argument below. configsetname = "ConfigSet" # Replace us-west-2 with the AWS Region you're using for Amazon SES. awsregion = "us-west-2" # The subject line for the email. subject = "Amazon SES test (适用于 Ruby 的 AWS SDK)" # The HTML body of the email. htmlbody = '<h1>Amazon SES test (适用于 Ruby 的 AWS SDK)</h1>'\ '<p>This email was sent with <a href="https://aws.amazon.com/ses/">'\ 'Amazon SES</a> using the <a href="https://aws.amazon.com/sdk-for-ruby/">'\ '适用于 Ruby 的 AWS SDK</a>.' # The email body for recipients with non-HTML email clients. textbody = "This email was sent with Amazon SES using the 适用于 Ruby 的 AWS SDK." # Specify the text encoding scheme. encoding = "UTF-8" # Create a new SES resource and specify a region ses = Aws::SES::Client.new(region: awsregion) # Try to send the email. begin # Provide the contents of the email. resp = ses.send_email({ destination: { to_addresses: [ recipient, ], }, message: { body: { html: { charset: encoding, data: htmlbody, }, text: { charset: encoding, data: textbody, }, }, subject: { charset: encoding, data: subject, }, }, source: sender, # Comment or remove the following line if you are not using # a configuration set configuration_set_name: configsetname, }) puts "Email sent!" # If something goes wrong, display an error message. rescue Aws::SES::Errors::ServiceError => error puts "Email not sent. Error message: #{error}" end
    2. amazon-ses-sample.rb 中,将以下内容替换为您自己的值:

      • sender@example.com – 替换为您已使用 Amazon SES 验证过的电子邮件地址。有关更多信息,请参阅 已验证的身份。Amazon SES 中的电子邮件地址区分大小写。请确保您输入的地址与经验证的地址完全相同。

      • recipient@example.com – 替换为收件人的地址。如果您的账户仍处于沙盒中,您还必须验证此地址,然后才能使用它。有关更多信息,请参阅 请求生产访问权限(从 Amazon SES 沙盒中移出)。请确保您输入的地址与经验证的地址完全相同。

      • (可选)us-west-2 - 如果您要在美国西部(俄勒冈州)以外的区域中使用 Amazon SES,请将它替换为您要使用的区域。有关已推出 Amazon SES 的区域的列表,请参阅《AWS 一般参考》中的 Amazon Simple Email Service(Amazon SES)

    3. 保存 amazon-ses-sample.rb

    4. 要运行程序,请在 amazon-ses-sample.rb 所在的目录中打开命令提示符,然后键入 ruby amazon-ses-sample.rb

    5. 检查输出。如果已成功发送电子邮件,则控制台会显示“Email sent!”,否则将显示一条错误消息。

    6. 登录收件人地址的电子邮件客户端。您将找到已发送的电子邮件。

    Python

    本主题说明如何使用 适用于 Python (Boto) 的 AWS SDK 通过 Amazon SES 发送电子邮件。

    在开始前,请执行以下任务:
    • 使用 Amazon SES 验证您的电子邮件地址 – 您必须先验证您拥有发件人的电子邮件地址,然后才能使用 Amazon SES 发送电子邮件。如果您的账户仍在 Amazon SES 沙盒中,您还必须验证收件人的电子邮件地址。我们建议您使用 Amazon SES 控制台来验证电子邮件地址。有关更多信息,请参阅 创建电子邮件地址身份

    • 获取您的 AWS 证书 — 您需要访问密钥 ID 和 AWS 私有 AWS 访问密钥才能使用 SDK 访问 Amazon SES。您可以通过 安全凭证 AWS 管理控制台页面来查找您的凭证。有关凭证的更多信息,请参阅Amazon SES 凭证的类型

    • 安装 Python —Python 可在以下网址获得。 https://www.python.org/downloads/ 本教程中的代码已使用 Python 2.7.6 和 Python 3.6.1 进行了测试。安装 Python 后,在环境变量中添加 Python 的路径,这样就能通过任何命令提示符运行 Python。

    • 安装 适用于 Python (Boto) 的 AWS SDK —有关下载和安装说明,请参阅适用于 Python (Boto) 的 AWS SDK 文档。本教程中的示例代码已使用 1.4.4 版本的 SDK for Python 进行了测试。

    使用 SDK for Python 通过 Amazon SES 发送电子邮件
    1. 在文本编辑器中,创建一个名为 amazon-ses-sample.py 的文件。将以下代码粘贴到该文件中:

      import boto3 from botocore.exceptions import ClientError # Replace sender@example.com with your "From" address. # This address must be verified with Amazon SES. SENDER = "Sender Name <sender@example.com>" # Replace recipient@example.com with a "To" address. If your account # is still in the sandbox, this address must be verified. RECIPIENT = "recipient@example.com" # Specify a configuration set. If you do not want to use a configuration # set, comment the following variable, and the # ConfigurationSetName=CONFIGURATION_SET argument below. CONFIGURATION_SET = "ConfigSet" # If necessary, replace us-west-2 with the AWS Region you're using for Amazon SES. AWS_REGION = "us-west-2" # The subject line for the email. SUBJECT = "Amazon SES Test (SDK for Python)" # The email body for recipients with non-HTML email clients. BODY_TEXT = ("Amazon SES Test (Python)\r\n" "This email was sent with Amazon SES using the " "适用于 Python (Boto) 的 AWS SDK." ) # The HTML body of the email. BODY_HTML = """<html> <head></head> <body> <h1>Amazon SES Test (SDK for Python)</h1> <p>This email was sent with <a href='https://aws.amazon.com/ses/'>Amazon SES</a> using the <a href='https://aws.amazon.com/sdk-for-python/'> 适用于 Python (Boto) 的 AWS SDK</a>.</p> </body> </html> """ # The character encoding for the email. CHARSET = "UTF-8" # Create a new SES resource and specify a region. client = boto3.client('ses',region_name=AWS_REGION) # Try to send the email. try: #Provide the contents of the email. response = client.send_email( Destination={ 'ToAddresses': [ RECIPIENT, ], }, Message={ 'Body': { 'Html': { 'Charset': CHARSET, 'Data': BODY_HTML, }, 'Text': { 'Charset': CHARSET, 'Data': BODY_TEXT, }, }, 'Subject': { 'Charset': CHARSET, 'Data': SUBJECT, }, }, Source=SENDER, # If you are not using a configuration set, comment or delete the # following line ConfigurationSetName=CONFIGURATION_SET, ) # Display an error if something goes wrong. except ClientError as e: print(e.response['Error']['Message']) else: print("Email sent! Message ID:"), print(response['MessageId'])
    2. amazon-ses-sample.py 中,将以下内容替换为您自己的值:

      • sender@example.com – 替换为您已使用 Amazon SES 验证过的电子邮件地址。有关更多信息,请参阅 已验证的身份。Amazon SES 中的电子邮件地址区分大小写。请确保您输入的地址与经验证的地址完全相同。

      • recipient@example.com – 替换为收件人的地址。如果您的账户仍处于沙盒中,您还必须验证此地址,然后才能使用它。有关更多信息,请参阅 请求生产访问权限(从 Amazon SES 沙盒中移出)。请确保您输入的地址与经验证的地址完全相同。

      • (可选)us-west-2 - 如果您要在美国西部(俄勒冈州)以外的区域中使用 Amazon SES,请将它替换为您要使用的区域。有关已推出 Amazon SES 的区域的列表,请参阅《AWS 一般参考》中的 Amazon Simple Email Service(Amazon SES)

    3. 保存 amazon-ses-sample.py

    4. 要运行程序,请在 amazon-ses-sample.py 所在的目录中打开命令提示符,然后键入 python amazon-ses-sample.py

    5. 检查输出。如果已成功发送电子邮件,则控制台会显示“Email sent!”,否则将显示一条错误消息。

    6. 登录收件人地址的电子邮件客户端。您将看到已发送的电子邮件。