使用 AWS SES 发送邮件时,出现了以下错误信息:
javax.mail.AuthenticationFailedException: Authentication failed
这个问题通常是由于没有正确配置 AWS SES 密钥和 IAM 用户权限而引起的。
要解决这个问题,需要按照以下步骤进行操作:
aws ses create-access-key --user-name
aws iam create-user --user-name
aws iam put-user-policy \
--user-name \
--policy-name SESPolicy \
--policy-document file://ses-policy.json
其中,ses-policy.json
的内容如下:
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "VisualEditor0",
"Effect": "Allow",
"Action": "ses:*",
"Resource": "*"
}
]
}
AWSCredentials credentials = new BasicAWSCredentials(
"", "");
AmazonSES client = AmazonSESClientBuilder.standard()
.withCredentials(new AWSStaticCredentialsProvider(credentials))
.withRegion("")
.build();
Properties props = new Properties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", "email-smtp.us-west-2.amazonaws.com");
props.put("mail.smtp.port", "587");
Session session = Session.getInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("", "");
}
});
通过以上步骤进行操作,就可以成功发送邮件了。