当使用AWS SES的SMTP接口发送电子邮件时,可能会遇到“AuthenticationFailedException: 220 Ready to start TLS”错误。此错误通常表示在与SMTP服务器建立连接并尝试启用TLS时出现身份验证问题。
以下是一个可能的解决方案,其中包含Java代码示例:
import javax.mail.*;
import javax.mail.internet.*;
import java.util.Properties;
public class Main {
public static void main(String[] args) {
String from = "sender@example.com";
String to = "recipient@example.com";
String host = "email-smtp.us-west-2.amazonaws.com";
String username = "YOUR_SMTP_USERNAME";
String password = "YOUR_SMTP_PASSWORD";
Properties props = new Properties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", host);
props.put("mail.smtp.port", "587");
Session session = Session.getInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});
try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse(to));
message.setSubject("Hello AWS SES SMTP");
message.setText("This is a test email from AWS SES SMTP");
Transport.send(message);
System.out.println("Email sent successfully.");
} catch (MessagingException e) {
e.printStackTrace();
}
}
}
确保替换以下变量的占位符:
from
:发件人的电子邮件地址to
:收件人的电子邮件地址host
:AWS SES的SMTP主机地址(根据您的区域和设置进行更改)username
:AWS SES的SMTP用户名password
:AWS SES的SMTP密码如果您仍然遇到身份验证问题,请确保您的SMTP用户名和密码正确,并且已正确配置AWS SES的SMTP凭证。您可以在AWS控制台中的“SMTP Settings”部分找到这些凭证。
上一篇:AWS SES 审计