话不多说上干货
首先我们要去亚马逊申请到access_key_id、secret_key_id,如果不知道怎么操作的朋友可以去看我之前写的一篇文章集成S3,这里就不做描述了,言归正传,开始集成was ses邮件服务。
首先pom需引入相关依赖
<!-- 集成亚马逊邮件-->
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-ses</artifactId>
<version>1.12.648</version>
</dependency>
这里有个问题需要进行描述,集成依赖后执行代码发现总会出现这么一行错误信息
com.amazonaws.util.Base64 - JAXB is unavailable. Will fallback to SDK implementation which may be less performant.If you are using Java 9+, you will need to include javax.xml.bind:jaxb-api as a dependency.
跟距错误描述意思是 com.amazonaws.util.Base64 - JAXB 不可用。将回退到性能可能较低的 SDK 实现。如果您使用的是 Java 9+,则需要包含 javax.xml.bind:jaxb-api 作为依赖项。关于这个问题在网上找了很多办法但都没有起到作用,虽然不影响代码的执行,但对于我这种有强迫症的同学,总是想处理掉,担心后面会引发更大的问题,最后想着升级依赖处理,并成功解决了改问题。
继续引入依赖
<!-- 亚马逊jar中依赖包-->
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.3.1</version>
</dependency>
开始编写代码流程
import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.regions.Regions;
import com.amazonaws.services.simpleemail.AmazonSimpleEmailService;
import com.amazonaws.services.simpleemail.AmazonSimpleEmailServiceClientBuilder;
import com.amazonaws.services.simpleemail.model.RawMessage;
import com.amazonaws.services.simpleemail.model.SendRawEmailRequest;
public void amazonSESSample() throws Exception {
//证书
String access_key_id = "你申请的证书";
String secret_key_id = "你申请的证书";
//发件人邮箱
String SENDER = "xxx";
// 收件人地址
String RECIPIENT = "xxxxx";
// 邮件主题
String SUBJECT = "这是一个测试邮件";
// 邮件内容
String BODY_TEXT = "这是一个测试邮件";
Session session = Session.getDefaultInstance(new Properties());
// 创建一个 MimeMessage 对象
MimeMessage message = new MimeMessage(session);
// 添加主题.
message.setSubject(SUBJECT, "UTF-8");
message.setFrom(new InternetAddress(SENDER));
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse(RECIPIENT));
// 创建邮件容器
MimeMultipart msg_body = new MimeMultipart("alternative");
// 将文本信息添加到邮件中
MimeBodyPart textPart = new MimeBodyPart();
textPart.setContent(BODY_TEXT, "text/plain; charset=UTF-8");
msg_body.addBodyPart(textPart);
// 创建容器包装器
MimeBodyPart wrap = new MimeBodyPart();
// 将容器添加到包装器中
wrap.setContent(msg_body);
// 创建多部分/混合父容器。
MimeMultipart msg = new MimeMultipart("mixed");
// 将父容器添加到消息中
message.setContent(msg);
// 将包装器存入邮件消息中
msg.addBodyPart(wrap);
//开始发送邮件
try {
System.err.println("开始发送邮件");
//通过sdk创建与was邮箱的链接
BasicAWSCredentials awsCredentials = new BasicAWSCredentials(access_key_id, secret_key_id);
// 实例化 Amazon SES 客户端,该客户端将创建服务,使用提供的 AWS 凭证进行调用。
AmazonSimpleEmailService client =
AmazonSimpleEmailServiceClientBuilder.standard()
// 将 US_WEST_2 替换为您使用的 AWS 区域 Amazon SES.
.withRegion(Regions.US_WEST_2)
.withCredentials(new AWSStaticCredentialsProvider(awsCredentials)).build();
// 在控制台上打印原始电子邮件内容
PrintStream out = System.out;
message.writeTo(out);
// 发送电子邮件。
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
message.writeTo(outputStream);
RawMessage rawMessage =
new RawMessage(ByteBuffer.wrap(outputStream.toByteArray()));
SendRawEmailRequest rawEmailRequest =
new SendRawEmailRequest(rawMessage);
client.sendRawEmail(rawEmailRequest);
System.out.println("Email sent!");
} catch (Exception ex) {
System.out.println("Email Failed");
System.err.println("Error message: " + ex.getMessage());
ex.printStackTrace();
}
}
大功告成。执行代码 邮件发送成功。