翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。
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 シークレットアクセスキーが必要です。認証情報を取得するするには、 AWS Management Consoleの「セキュリティの認証情報
」のページを参照してください。認証情報の詳細については、「Amazon SES 認証情報の種類」を参照してください。 -
共有認証情報ファイルの作成 — このセクションのサンプルコードが正常に機能するためには、共有認証情報ファイルを作成する必要があります。詳細については、「AWS SDK を使用して Amazon SES 経由で E メールを送信するときに使用する共有認証情報ファイルの作成」を参照してください。
コードの例
重要
次のチュートリアルでは、受信を確認できるように自分宛に E メールを送信します。さらに詳しい実験や負荷テストには、Amazon SES メールボックスシミュレーターを使用してください。メールボックスシミュレーターに送信される E メールは、送信クォータに加算されず、バウンス率や苦情率の計算にも含まれません。詳細については、手動でメールボックスシミュレーターを使用するを参照ください。
プログラミング言語を選択して、その言語の例を表示します。
以下の手順は、Visual Studio
このソリューションは次のコンポーネントを使用してテスト済みです。
-
Microsoft Visual Studio コミュニティ 2017、バージョン 15.4.0。
-
Microsoft .NET Framework バージョン 4.6.1.
-
NuGet を使用してインストールされた AWSSDK.Core パッケージ (バージョン 3.3.19)。
-
NuGet を使用してインストールされた AWSSDK.SimpleEmail パッケージ (バージョン 3.3.6.1)。
開始する前に、次のタスクを実行します。
-
Visual Studio のインストール - Visual Studio は https://www.visualstudio.com/
から入手可能です。
を使用して E メールを送信するには AWS SDK for .NET
-
以下のステップを実行して、新しいプロジェクトを作成します。
-
Visual Studio を起動します。
-
[ファイル] メニューで [New]、[Project] の順に選択します。
-
[New Project] ウィンドウの左側のパネルで、[Installed]、[Visual C#] の順に展開します。
-
右側のパネルで、[Console App (.NET Framework)] を選択します。
-
名前に
AmazonSESSample
と入力し、OK を選択します。
-
-
次のステップを実行して、NuGet を使用して Amazon SES パッケージをソリューションに含めます。
-
ソリューションエクスプローラーペインで、プロジェクトを右クリックして、コンテキストメニューの [NuGet パッケージの管理] を選択します。
-
[NuGet: AmazonSESSample] タブで、[参照] を選択します。
-
検索ボックスに [
AWSSDK.SimpleEmail
] と入力します -
[AWSSDK.SimpleEmail] パッケージを選択し、[インストール] を選択します。
-
変更のプレビューウィンドウで、OK を選択します。
-
-
[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(); } } } -
コードエディタで、以下の作業を行います。
-
sender@example.com
を "差出人:" の E メールアドレスに置き換えます。このアドレスは確認する必要があります。詳細については、「Amazon SES の検証済みID」を参照してください。 -
recipient@example.com
を "宛先:" のアドレスに置き換えます。アカウントがサンドボックスにまだある場合は、このアドレスも確認する必要があります。 -
ConfigSet
を、この E メールを送信するときに使用する設定セットの名前に置き換えます。 -
USWest2
を、Amazon SES を使用して E メールを送信するために使用する AWS リージョン エンドポイントの名前に置き換えます。Amazon SES を使用できるリージョンのリストについては、「AWS 全般のリファレンス」の「Amazon Simple Email Service (Amazon SES)」を参照してください。
終了したら、
Program.cs
を保存します。 -
-
次の手順に従ってアプリケーションをビルドおよび実行します。
-
[Build] メニューの [Build Solution] を選択します。
-
[Debug] メニューの [Start Debugging] を選択します。コンソールウィンドウが表示されます。
-
-
コンソールの出力を確認します。E メールが正常に送信されると、コンソールに "
The email was sent successfully.
" と表示されます -
E メールが正常に送信されたら、受信者アドレスの E メールクライアントにサインインします。送信した E メールメッセージを確認します。