目录

使用 AWS SDK for Java 发送电子邮件

最近使用了AWS S3,便想着再试试SES

我的域名邮箱一直是用的Mail.Ru,如果收发量不大其实还行,就是境内访问很慢,而且之前由于贴吧云签到服务验证用户一下发的有点频繁,还被ban了个子用户,所以一直就用的比较少了.

AWS SES说明

注册AWS账号需要双币信用卡, 正常的免费套餐就足够用了

https://img.pulnd.com/post/ses-aws-sdk-for-java/J8TL5D18C7.png

移除沙盒之后,满足日常服务使用没问题

https://img.pulnd.com/post/ses-aws-sdk-for-java/71by1DAwu0.png

开始使用 AWS SES

直接在搜索框输入SES, 选择 Amazon Simple Email Service

https://img.pulnd.com/post/ses-aws-sdk-for-java/2vl0S2l6P4.png

验证域名和电子邮件地址

先要确认您拥有发件人的电子邮件地址,然后才能使用 Amazon SES 发送电子邮件。如果您的账户仍处于 Amazon SES 沙盒中,您还必须验证收件人的电子邮件地址

https://img.pulnd.com/post/ses-aws-sdk-for-java/bGUwP7Kbo8.png

先分别验证一下域名和电子邮件地址

https://img.pulnd.com/post/ses-aws-sdk-for-java/N6j7x4pq15.png

设置 MAIL FROM 域

https://img.pulnd.com/post/ses-aws-sdk-for-java/U4E1Y1w325.png

https://img.pulnd.com/post/ses-aws-sdk-for-java/w44AF8IyjE.png

全部状态ok就可以了

https://img.pulnd.com/post/ses-aws-sdk-for-java/G7X2b1Oqh8.png

获取AWS凭证

通过 AWS 管理控制台的安全凭证页面来生成您的凭证并保存

https://img.pulnd.com/post/ses-aws-sdk-for-java/lK1L4i4P4o.png

安装JDK并配置环境变量

下载 JDK ,嫌官网下载麻烦的可以到清华大学开源软件镜像站下载,下载完后安装

配置环境变量步骤:

打开我的电脑-属性-高级-高级系统设置-系统变量

1、新增 JAVA_HOME 属性值:填写你JDK的根目录

2、修改原来 Path 属性值:新增一个 %JAVA_HOME%\bin

安装Eclipse

下载 Eclipse

安装完毕后打开Eclipse,菜单栏选择 Window - Preferences - Java - Installed JREs

如果列表里已经显示有JDK了就ok了,如果没有点add添加一下

https://img.pulnd.com/post/ses-aws-sdk-for-java/p32jBtM07c.png

https://img.pulnd.com/post/ses-aws-sdk-for-java/x1C3hqCXW0.png

选取JDK安装目录

https://img.pulnd.com/post/ses-aws-sdk-for-java/vDTqX6fe46.png

最后面再换到 Compiler 标签看一下Eclipse的编译版本

https://img.pulnd.com/post/ses-aws-sdk-for-java/OG3m8Td3qU.png

安装 AWS Toolkit for Eclipse

下载工具包

安装工具包

  1. 打开“Help”->“Install New Software…”。

  2. 在对话框顶部标有“Work with”的文本框中,输入 https://aws.amazon.com/eclipse

  3. 从下面的列表中选择所需的“AWS Core Management Tools”和其他可选项。

https://img.pulnd.com/post/ses-aws-sdk-for-java/7r1pC22fsy.png

  1. 单击“Next”,Eclipse 将引导您完成剩余的安装步骤。

选择 Install anyway

https://img.pulnd.com/post/ses-aws-sdk-for-java/RMIBwpX4pi.png

重启一下

https://img.pulnd.com/post/ses-aws-sdk-for-java/gE16i76Bmn.png

请注意:该工具包需要 Eclipse 4.4 (Luna) 或更高版本。

配置共享凭证

如果有这个界面直接填入你的 AWS 访问密钥信息就可以了

https://img.pulnd.com/post/ses-aws-sdk-for-java/YL17P582i2.png

如果没有可以直接创建一个 credentials 无后缀名文件

1
2
3
[default]
aws_access_key_id = YOUR_AWS_ACCESS_KEY_ID
aws_secret_access_key = YOUR_AWS_SECRET_ACCESS_KEY

YOUR_AWS_ACCESS_KEY 替换为您的唯一 AWS 访问密钥 ID,并将 YOUR_AWS_SECRET_ACCESS_KEY 替换为您的唯一 AWS 秘密访问密钥

保存此文件将文件另存为对应的位置

  • Windows 目录为
1
C:\Users\<yourUserName>\.aws\credentials
  • Linux, macOS, or Unix 目录为
1
~/.aws/credentials

使用 AWS SDK for Java 发送电子邮件

通过执行以下步骤在 Eclipse 中创建 AWS Java 项目:

启动 Eclipse,在 File 菜单上,选择 New,然后选择 Other。在 New 窗口上,展开 AWS 文件夹,然后选择 AWS Java Project

New AWS Java Project 对话框中,执行以下操作:

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

AWS SDK for Java Samples 下,选择 Amazon Simple Email Service JavaMail Sample

https://img.pulnd.com/post/ses-aws-sdk-for-java/W62PUrM11l.png

选择 Finish。

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

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

https://img.pulnd.com/post/ses-aws-sdk-for-java/xmKa1x86wS.png

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

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
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());
    }
  }
}

在 AmazonSESSample.java 中,将以下内容替换为您自己的值:

https://img.pulnd.com/post/ses-aws-sdk-for-java/J5OTp06X75.png

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

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

  • RECIPIENT@EXAMPLE.COM 替换为您的“To”电子邮件地址。如果您的账户仍处于沙盒中,您还必须验证此地址,然后才能使用它。有关更多信息,请参阅 脱离 Amazon SES 沙盒。)

  • US_WEST_2 请将该区域替换自己账号对应的使用区域。

区域名称Code
美国东部 (俄亥俄)us-east-2
美国东部 (弗吉尼亚北部)us-east-1
美国西部 (加利福尼亚北部)us-west-1
美国西部 (俄勒冈)us-west-2
非洲(开普敦)af-south-1
亚太区域 (香港)ap-east-1
亚太区域 (孟买)ap-south-1
亚太地区(大阪)ap-northeast-3
亚太区域 (首尔)ap-northeast-2
亚太地区 (新加坡)ap-southeast-1
亚太地区 (悉尼)ap-southeast-2
亚太地区 (东京)ap-northeast-1
加拿大(中部)ca-central-1
中国 (北京)cn-north-1
中国(宁夏)cn-northwest-1
歐洲 (法蘭克福)eu-central-1
歐洲 (愛爾蘭)eu-west-1
歐洲 (倫敦)eu-west-2
「歐洲」(米蘭)eu-south-1
歐洲 (巴黎)eu-west-3
歐洲 (斯德哥爾摩)eu-north-1
中东 (巴林)me-south-1
南美洲 (圣保罗)sa-east-1

全部修改完成后保存一下,Run运行

如果控制台显示 Email sent! ,表示已成功发送电子邮件。

https://img.pulnd.com/post/ses-aws-sdk-for-java/GnGesWi5Km.png

进行邮件评分

使用mail-tester进行评分

https://img.pulnd.com/post/ses-aws-sdk-for-java/REw5WiS787.png

生成 SMTP 凭证

https://img.pulnd.com/post/ses-aws-sdk-for-java/GyD0mM2EG8.png

现在可以开始使用了

参考文章

[1]. 使用 AWS SDK for Java 发送电子邮件 - Amazon Simple Email Service