You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用AWS SES SMTP发送邮件遇AuthenticationFailedException:220 Ready to start TLS

问题

按照AWS SES官方文档指引创建了SMTP凭证并下载了凭证.csv文件,但使用Open JDK 1.8的Java代码通过SMTP发送邮件时触发认证异常。

依赖配置如下:

<dependency>
    <groupId>javax.mail</groupId>
    <artifactId>mail</artifactId>
    <version>1.4.7</version>
</dependency>

Java代码:

private static final String HOST = "email-smtp.us-west-2.amazonaws.com";
private static final String PORT = "587";
private static final String USER_NAME = "SMTP user name in credentials.csv";
private static final String PASSWORD = "SMTP password in credentials.csv";

public static void main(String[] args) throws UnsupportedEncodingException, MessagingException {
    Properties props = new Properties();
    props.put("mail.smtp.host", HOST);
    props.put("mail.smtp.port", PORT);
    props.put("mail.smtp.auth", "true");
    Session session = Session.getInstance(props, new Authenticator() {
        @Override
        protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication(USER_NAME, PASSWORD);
        }
    });

    MimeMessage message = new MimeMessage(session);
    message.setRecipients(Message.RecipientType.TO, InternetAddress.parse("faxon.xing@gmail.com"));
    message.setSubject("AWS SES SMTP Email");
    message.setContent("<p>Hello, AWS SES SMTP<a href=\"https://www.google.com\">click btn</a></p>", "text/html;charset=UTF-8");

    Transport.send(message);
}

异常日志:

Exception in thread "main" javax.mail.AuthenticationFailedException: 220 Ready to start TLS

解决方法

  • 启用STARTTLS加密
    AWS SES的587端口要求先启动TLS加密再进行认证,原代码缺少该配置,需在Properties中添加:

    props.put("mail.smtp.starttls.enable", "true");
    props.put("mail.smtp.starttls.required", "true");
    

    这两项配置会强制客户端在发起认证前建立TLS连接,匹配服务器的安全要求。

  • 升级javax.mail依赖版本
    1.4.7版本属于老旧版本,存在兼容性问题,建议升级到稳定兼容版本(如1.6.2),调整依赖为:

    <dependency>
        <groupId>javax.mail</groupId>
        <artifactId>javax.mail-api</artifactId>
        <version>1.6.2</version>
    </dependency>
    <dependency>
        <groupId>com.sun.mail</groupId>
        <artifactId>javax.mail</artifactId>
        <version>1.6.2</version>
    </dependency>
    
  • 验证凭证与区域一致性
    确认使用的SMTP用户名、密码是AWS SES控制台生成的有效凭证,且SMTP服务器地址与你的SES账户所在区域完全匹配,避免因区域不匹配导致的认证失败。

内容的提问来源于stack exchange,提问作者Xon Fa

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.07 15:57:53