AWS SES SDK V2 API 邮件发送
2024-11-08 15:27:31 6
记录一下
@Bean
public SesClient sesClient(EmailAwsConfig config) {
AwsBasicCredentials build = AwsBasicCredentials.builder()
.accountId(config.getAccountId())
.accessKeyId(config.getAccessKey())
.secretAccessKey(config.getAccessKeySecret())
.validateCredentials(true).build();
return SesClient.builder()
.region(Region.of(config.getRegion()))
.credentialsProvider(StaticCredentialsProvider.create(build)).build();
}
@Autowired
SesClient sesClient;
public Result awsSendEmail(SendDto dto) {
try {
SendEmailRequest request = SendEmailRequest.builder()
.destination(Destination.builder().toAddresses(dto.getReceiveEmail()).build())
.message(Message.builder()
.subject(Content.builder().data(dto.getTitle()).charset("UTF-8").build())
.body(Body.builder().html(Content.builder()
.data(dto.getContent()).charset("UTF-8").build()).build())
.build())
// 显示名称以字符串拼接的方式传递
.source("22xcode.com <" + dto.getAccountName() + ">")
.build();
SendEmailResponse response = sesClient.sendEmail(request);
log.info("requestId: {}, msgId: {}",
response.responseMetadata().requestId(), response.messageId());
return Result.success();
} catch (Exception ex) {
log.error("error, dto: {}", dto, ex);
return Result.failure();
}
}
ref:
https://github.com/aws/aws-sdk-java-v2