LoginSignup
18

More than 5 years have passed since last update.

Amazon SES を利用してメールを送信する

Posted at

Amazon SES の設定

2015/2/15 現在、Tokyo リージョンでは利用できないようなので、リージョンは US East を選択する。

FROM メールアドレスの登録

  • AWS コンソールから、Services > SES > Email Addresses ページにアクセスする
  • 「Verify a New Email Address」ボタンから、使用するメールアドレスを登録する
  • 確認メールが飛んでくるので、メールに記載されているリンクをクリックする
  • 登録したメールアドレスの Status が verified となればOK

メール送信用の IAM ユーザーの登録

  • Services > SES > SMTP Settings ページにアクセスする
  • 「Create My SMTP Credentials」ボタンから、IAM ユーザーを登録する
  • Services > IAM > Users ページにアクセスし、追加した IAM ユーザーを選択する
  • 「Manage Access Keys」ボタンから、アクセスキーを新規に登録する (後ほどコード上で指定するキーとなる)
  • 「Edit Policy」リンクから、ポリシーに ses:SendEmail を追加する
    • IAM 新規登録時は ses:SendRawEmail のみが登録されているため、ポリシーを追加する必要がある
{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Action": "ses:SendRawEmail",
      "Action": "ses:SendEmail",
      "Resource": "*"
    }
  ]
}

Java コードサンプル

aws-java-sdk-ses 1.9.19 を利用したサンプルコード。

    Destination destination = new Destination().withToAddresses(new String[]{"TO メールアドレス"});

    Body body = new Body().withText(new Content().withData("本文"));
    Message message = new Message()
        .withSubject(new Content().withData("件名"))
        .withBody(body);

    SendEmailRequest request = new SendEmailRequest()
        .withSource("FROM メールアドレス")
        .withDestination(destination)
        .withMessage(message);

    AmazonSimpleEmailServiceClient client = new AmazonSimpleEmailServiceClient(new  BasicAWSCredentials("アクセスキー", "シークレットアクセスキー"));
    client.setRegion(Region.US_EAST_1);
    client.sendEmail(request);

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
18