AWS SDK を使用して Amazon SES 経由で E メールを送信する - Amazon Simple Email Service

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

AWS SDK を使用して Amazon SES 経由で E メールを送信する

AWS SDK を使用して、Amazon SES 経由で E メールを送信できます。 AWS SDKsは、いくつかのプログラミング言語で使用できます。詳細については、Tools for Amazon Web Services を参照してください。

前提条件

次のセクションのコードサンプルを完了するには、次の前提条件を完了する必要があります。

  • まだ行っていない場合は、「Amazon Simple Email Service を設定する」の作業を完了してください。

  • Amazon SES で E メールアドレスを検証する- Amazon SES で E メールを送信するには、送信者の E メールアドレスを所有していることを検証する必要があります。アカウントが Amazon SES サンドボックスにまだある場合は、受信者の E メールアドレスも検証する必要があります。E メールアドレスを検証するには、Amazon SES コンソールを使用することをお勧めします。詳細については、「Eメールアドレス ID の作成」を参照してください。

  • AWS 認証情報の取得 — SDK を使用して Amazon SES にアクセスするには、 AWS アクセスキー ID と AWS シークレットアクセスキーが必要です。 Amazon SES 認証情報を取得するするには、 AWS Management Consoleの「セキュリティの認証情報」のページを参照してください。認証情報の詳細については、「Amazon SES 認証情報の種類」を参照してください。

  • 共有認証情報ファイルの作成 — このセクションのサンプルコードが正常に機能するためには、共有認証情報ファイルを作成する必要があります。詳細については、「AWS SDK を使用して Amazon SES 経由で E メールを送信するときに使用する共有認証情報ファイルの作成」を参照してください。

コードサンプル

重要

次のチュートリアルでは、受信を確認できるように自分宛に E メールを送信します。さらに詳しい実験や負荷テストには、Amazon SES メールボックスシミュレーターを使用してください。メールボックスシミュレーターに送信される E メールは、送信クォータに加算されず、バウンス率や苦情率の計算にも含まれません。詳細については、手動でメールボックスシミュレーターを使用するを参照ください。

プログラミング言語を選択して、その言語の例を表示します。
    .NET

    以下の手順は、Visual Studioと AWS SDK for .NETを使用して、Amazon SES 経由で E メールを送信する方法を示しています。

    このソリューションは次のコンポーネントを使用してテスト済みです。

    • Microsoft Visual Studio コミュニティ 2017、バージョン 15.4.0。

    • Microsoft .NET Framework バージョン 4.6.1.

    • を使用してインストールされた AWSSDK.Core パッケージ (バージョン 3.3.19) NuGet。

    • . AWSSDKSimpleEmail package (バージョン 3.3.6.1)。 を使用してインストールされます NuGet。

    開始する前に、次のタスクを実行します。
    を使用して E メールを送信するには AWS SDK for .NET
    1. 以下のステップを実行して、新しいプロジェクトを作成します。

      1. Visual Studio を起動します。

      2. [ファイル] メニューで [New]、[Project] の順に選択します。

      3. [New Project] ウィンドウの左側のパネルで、[Installed]、[Visual C#] の順に展開します。

      4. 右側のパネルで、[Console App (.NET Framework)] を選択します。

      5. 名前AmazonSESSample と入力し、OK を選択します。

    2. を使用して NuGet 、次のステップを完了して Amazon SES パッケージをソリューションに含めます。

      1. Solution Explorer ペインでプロジェクトを右クリックし、 NuGet パッケージの管理を選択します。

      2. NuGet: AmazonSESSample タブで、「参照」を選択します。

      3. 検索ボックスに [AWSSDK.SimpleEmail] と入力します

      4. AWSSDKパッケージSimpleEmailを選択し、インストールを選択します。

      5. 変更のプレビューウィンドウで、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 (AWS 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 を "差出人:" の E メールアドレスに置き換えます。このアドレスは確認する必要があります。詳細については、「Amazon SES の検証済みID」を参照してください。

      • recipient@example.com を "宛先:" のアドレスに置き換えます。アカウントがサンドボックスにまだある場合は、このアドレスも確認する必要があります。

      • を、この E メールを送信するときに使用する設定セットの名前ConfigSetに置き換えます。

      • USWest2 を、Amazon SES を使用して E メールを送信するために使用する AWS リージョン エンドポイントの名前に置き換えます。Amazon SES を使用できるリージョンのリストについては、「AWS 全般のリファレンス」の「Amazon Simple Email Service (Amazon SES)」を参照してください。

      終了したら、Program.csを保存します。

    5. 次の手順に従ってアプリケーションをビルドおよび実行します。

      1. [Build] メニューの [Build Solution] を選択します。

      2. [Debug] メニューの [Start Debugging] を選択します。コンソールウィンドウが表示されます。

    6. コンソールの出力を確認します。E メールが正常に送信されると、コンソールに "The email was sent successfully." と表示されます

    7. E メールが正常に送信されたら、受信者アドレスの E メールクライアントにサインインします。送信した E メールメッセージを確認します。

    Java

    次の手順では、Eclipse IDE for Java EE Developers と を使用して AWS SDK プロジェクトAWS Toolkit for Eclipseを作成し、Java コードを変更して Amazon SES 経由で E メールを送信する方法を示します。

    開始する前に、次のタスクを実行します。
    • Eclipse のインストール - Eclipse はhttps://www.eclipse.org/downloadsからダウンロードできます。このチュートリアルのコードは、バージョン 1.8 の Java Runtime Environment を実行する 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でテスト済みです。

    を使用して E メールを送信するには 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 サンプル で、Amazon Simple Email Service JavaMail サンプル を選択します。

        3. [Finish] を選択します。

    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で、以下を独自の値に置き換えます。

      重要

      E メールアドレスでは、大文字と小文字は区別されます。検証したアドレスと完全に一致することを確認してください。

      • SENDER@EXAMPLE.COM - 「From」E メールアドレスに置き換えます。このアドレスを確認してから、プログラムを実行してください。詳細については、「Amazon SES の検証済みID」を参照してください。

      • RECIPIENT@EXAMPLE.COM - 「To」E メールアドレスに置き換えます。アカウントがサンドボックスにまだある場合は、このアドレスを使用前に確認する必要があります。詳細については、「Amazon SES サンドボックス外への移動」を参照してください。

      • (オプション)us-west-2 — 米国西部(オレゴン)以外の地域で Amazon SES を使用する場合は、これを使用する地域に置き換えます。Amazon SES を使用できるリージョンのリストについては、「AWS 全般のリファレンス」の「Amazon Simple Email Service (Amazon SES)」を参照してください。

    6. AmazonSESSample.java を保存します。

    7. プロジェクトを構築します。[Project]、[Build Project] の順に選択します。

      注記

      このオプションが無効の場合、自動構築が有効になっている可能性があります。その場合は、このステップをスキップします。

    8. プログラムを開始して E メールを送信します。[Run] を選択した後、もう一度 [Run] を選択します。

    9. Eclipse でコンソールペインの出力を確認します。E メールが正常に送信されると、コンソールに "Email sent!" が表示されます。送信に失敗すると、エラーメッセージが表示されます。

    10. E メールが正常に送信されたら、受信者アドレスの E メールクライアントにサインインします。送信した E メールメッセージを確認します。

    PHP

    このトピックでは、AWS SDK for PHP を使用して Amazon SES 経由で E メールを送信する方法を示します。

    開始する前に、次のタスクを実行します。
    • PHP をインストールする — PHP は、http://php.net/downloads.php から入手できます。このチュートリアルでは、バージョン 5.5 以上の PHP が必要です。PHP をインストールした後、コマンドプロンプトから PHP を実行できるように環境変数に PHP のパスを追加します。このチュートリアルのコードは PHP 7.2.7 でテスト済みです。

    • AWS SDK for PHP バージョン 3 のインストール - ダウンロードとインストールの手順については、 AWS SDK for PHP のドキュメントを参照してください。このチュートリアルのコードは SDK バージョン 3.64.13 でテスト済みです。

    を使用して Amazon SES から E メールを送信するには AWS SDK for PHP
    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 (AWS SDK for PHP)'; $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/">'. 'AWS SDK for PHP</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— プログラム AWS SDK for PHP に を含めるために必要なパスに置き換えます。詳細については、AWS SDK for PHP ドキュメントを参照してください。

      • sender@example.com — Amazon SES で検証した E メールアドレスに置き換えます。詳細については、「検証済みID」を参照してください。Amazon SES では、E メールアドレスの大文字と小文字が区別されます。検証したアドレスと完全に一致するアドレスを入力してください。

      • recipient1@example.comrecipient2@example.com - 受信者のアドレスに置き換えます。アカウントがサンドボックスにまだある場合は、受取人のアドレスも確認済みである必要があります。詳細については、「Amazon SES サンドボックス外への移動」を参照してください。検証したアドレスと完全に一致するアドレスを入力してください。

      • (オプション)ConfigSet — この E メールを送信する際に設定セットを使用する場合、この値を設定セットの名前で置き換えます。設定セットの詳細については、「Amazon 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. 出力を確認します。E メールが正常に送信されると、コンソールに "Email sent!" が表示されます。送信に失敗すると、エラーメッセージが表示されます。

      注記

      プログラムの実行時に "cURL error 60: SSL certificate problem" エラーが発生した場合は、AWS SDK for PHP のドキュメントに従って、最新の CA バンドルをダウンロードしてください。次に、amazon-ses-sample.php で、SesClient::factory 配列に以下の行を追加し、ダウンロードした CA バンドルのパスで path_of_certsを置き換えて、プログラムを再実行します。

      'http' => [ 'verify' => 'path_of_certs\ca-bundle.crt' ]
    6. 受信者のアドレスの E メールクライアントにサインインします。送信した メッセージを確認します。

    Ruby

    このトピックでは、AWS SDK for Ruby を使用して Amazon SES 経由で E メールを送信する方法を示します。

    開始する前に、次のタスクを実行します。
    • Ruby のインストール - Ruby は https://www.ruby-lang.org/en/downloads/からダウンロードできます。このチュートリアルのコードは Ruby 1.9.3 でテスト済みです。Ruby をインストールした後、コマンドプロンプトから Ruby を実行できるように環境変数に Ruby のパスを追加します。

    • のインストール AWS SDK for Ruby — ダウンロードとインストールの手順については、「 AWS SDK for RubyAWS SDK for Ruby デベロッパーガイド」の「 のインストール」を参照してください。このチュートリアルのサンプルコードは AWS SDK for Rubyバージョン 2.9.36 でテスト済みです。

    • 共有認証情報ファイルの作成 — このセクションのサンプルコードが正常に機能するためには、共有認証情報ファイルを作成する必要があります。詳細については、「AWS SDK を使用して Amazon SES 経由で E メールを送信するときに使用する共有認証情報ファイルの作成」を参照してください。

    を使用して Amazon SES から E メールを送信するには AWS SDK for Ruby
    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 (AWS SDK for Ruby)" # The HTML body of the email. htmlbody = '<h1>Amazon SES test (AWS SDK for Ruby)</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/">'\ 'AWS SDK for Ruby</a>.' # The email body for recipients with non-HTML email clients. textbody = "This email was sent with Amazon SES using the AWS SDK for Ruby." # 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 で検証した E メールアドレスに置き換えます。詳細については、「検証済みID」を参照してください。Amazon SES では、E メールアドレスの大文字と小文字が区別されます。検証したアドレスと完全に一致するアドレスを入力してください。

      • 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. 出力を確認します。E メールが正常に送信されると、コンソールに "Email sent!" が表示されます。送信に失敗すると、エラーメッセージが表示されます。

    6. 受信者のアドレスの E メールクライアントにサインインします。送信した E メールメッセージを確認します。

    Python

    このトピックでは、AWS SDK for Python (Boto)を使用して Amazon SES 経由で E メールを送信する方法を示します。

    開始する前に、次のタスクを実行します。
    • Amazon SES で E メールアドレスを検証する - Amazon SES で E メールを送信するには、送信者の E メールアドレスを所有していることを検証する必要があります。アカウントが Amazon SES サンドボックスにまだある場合は、受信者の E メールアドレスも検証する必要があります。E メールアドレスを検証するには、Amazon SES コンソールを使用することをお勧めします。詳細については、「Eメールアドレス ID の作成」を参照してください。

    • 認証情報の取得 AWS — SDK を使用して Amazon SES にアクセスするには、 AWS アクセスキー ID と AWS シークレットアクセスキーが必要です。 Amazon SES 認証情報を取得するには、 AWS Management Consoleの「セキュリティの認証情報」のページを参照してください。認証情報の詳細については、「Amazon SES 認証情報の種類」を参照してください。

    • Python のインストール - Python はhttps://www.python.org/downloads/からダウンロードできます。このチュートリアルのコードは Python 2.7.6 および Python 3.6.1 でテスト済みです。Python をインストールした後、コマンドプロンプトから Python を実行できるように環境変数に Python のパスを追加します。

    • のインストール AWS SDK for Python (Boto) — ダウンロードとインストールの手順については、 AWS SDK for Python (Boto) のドキュメントを参照してください。このチュートリアルのサンプルコードは SDK for Python のバージョン 1.4.4 でテスト済みです。

    SDK for Python を使用して Amazon SES 経由で E メールを送信するには
    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 " "AWS SDK for Python (Boto)." ) # 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/'> AWS SDK for Python (Boto)</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 で検証した E メールアドレスに置き換えます。詳細については、「検証済みID」を参照してください。Amazon SES では、E メールアドレスの大文字と小文字が区別されます。検証したアドレスと完全に一致するアドレスを入力してください。

      • 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. 出力を確認します。E メールが正常に送信されると、コンソールに "Email sent!" が表示されます。送信に失敗すると、エラーメッセージが表示されます。

    6. 受信者のアドレスの E メールクライアントにサインインします。送信した E メールメッセージを確認します。