简单谈一下
关于AWS的短信和邮件服务,国内外都没有相关参考,只能通读亚马逊官方文档踩坑。亚马逊短信服务考虑到需要申请短代码价格高昂所以换用Twillio短信服务,邮箱服务依然采用亚马逊SES服务。
文档地址:AWS开发文档
使用代码发送邮件,官方给了两种方式,一种是基于SMTP端口,另一种使用API发送。两种我都尝试了,我选第二种。
参照文档开始第一步
最开始需要你做三件事:
。 注册AWS账号
。验证邮箱或者域
。移除沙盒
注册账号就不说了,从验证邮箱开始:
从service搜索进入到SES控制台,这里仍然是老控制台。选择address,然后选择验证新邮箱,输入需要验证的邮箱点击验证。
然后会看到如下:
登录你的邮箱查看信息:
点击该链接即验证成功。
继续下一步脱离沙盒环境:
根据文档描述,如果你不脱离沙盒,那么你将会受到这些限制:
。你仅仅可以发送邮件到你刚刚验证过的邮箱地址;
。你只能通过验证的邮箱地址发送邮件
。你每天只能发送200条短信
。你每秒只能发送一条数据
这几条约束加起来等于你什么都不能做。
所以要参照文档发送请求脱离沙盒。
首先在当前SES控制台界面选择sending statistics,然后看到如下:
点击编辑,填写表单申请移除沙盒,举例:
这样很难说一次过审核,反正最后我发了一份正式邮件,亚马逊就帮助我们移除沙盒:
然后我们可以登录SES控制台查看状态:
现在已经移除沙盒,并且每天可以发送五万条邮件,每秒可以发送14条。
到此最基础的三步已经完成了。
继续跟着文档走
还是三步走:
。导入Java相关依赖包
。创建AWS IAM账号并授予SES权限
。创建credentials file
导入Java相关依赖包,这个很简单:
我直接贴出来pom:
<!--亚马逊邮件服务-->
<!-- https://mvnrepository.com/artifact/com.amazonaws/aws-java-sdk-ses -->
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-ses</artifactId>
<version>1.11.888</version>
</dependency>
<dependency>
<groupId>com.sun.mail</groupId>
<artifactId>javax.mail</artifactId>
<version>1.6.0</version>
</dependency>
第二步,创建IAM,并授权:
首先service 搜索IAM进入控制台
接下来创建IAM账户并授权
最重要的就是勾选这个编程访问,然后下一步选择附加策略,添加这一条即可:
成功后如下:
记得下载或者复制,此页面关闭后永远消失,访问秘钥和私有访问秘钥是编程发送邮件的必要条件。
接下来第三步,创建credentials file,这里存的就是上面保存下来的秘钥和私钥。这个东西是放在操作系统读取的,我也懒得研究如何放在程序里了。
意思就是,创建一个文本文件,名字就叫credentials 不带任何后缀,window放在.aws文件夹下,linux也一样。文件credentials粘贴放这三行文字,要把YOUR_AWS_ACCESS_KEY_ID替换成你
刚下载的秘钥,把YOUR_AWS_SECRET_ACCESS_KEY替换成你下载的私钥,也就是刚刚保存的csv文件里的内容。
万事俱备,最后一步,贴代码,代码是文档里的原封复制,只不过我把configset注释了,感觉对我没什么用。
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;
/**
* @author: flitsneak
* @date: 2021/2/23
*/
public class AWSSES {
// Replace sender@example.com with your "From" address.
// This address must be verified with Amazon SES.
static final String FROM = "发邮件邮箱地址,认证过的邮箱地址";
// 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 = "想要发送到的邮箱地址";
// 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_EAST_1).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());
}
}
}
测试: