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

Spring Boot用Gmail SMTP发邮件遇Connection reset异常排查

Spring Boot 结合 Gmail SMTP 发送邮件遇 Connection reset 问题

异常信息

org.springframework.mail.MailSendException: Mail server connection failed.
Failed messages: jakarta.mail.MessagingException: Exception reading response;
nested exception is:
java.net.SocketException: Connection reset

application.yml 配置

spring:
  mail:
    host: smtp.gmail.com
    port: 587
    username: your_email@gmail.com
    password: your_app_password

    properties:
      mail:
        smtp:
          auth: true
          starttls:
            enable: true
            required: true

          connectiontimeout: 5000
          timeout: 5000
          writetimeout: 5000
          debug: true

邮件服务代码

private final JavaMailSender javaMailSender;

@Value("${app.mail.from}")
private String fromEmail;

public void sendMail(EmailRequest request) {
    try {
        System.out.println("From mail spotted -> " + fromEmail);

        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom(fromEmail);
        message.setTo(request.getTo().toArray(new String[0]));

        if (request.getCc() != null && !request.getCc().isEmpty()) {
            message.setCc(request.getCc().toArray(new String[0]));
        }

        if (request.getBcc() != null && !request.getBcc().isEmpty()) {
            message.setBcc(request.getBcc().toArray(new String[0]));
        }

        message.setSubject(request.getSubject());
        message.setText(request.getBody());

        javaMailSender.send(message);

        log.info("Simple email sent successfully to: {}", request.getTo());

    } catch (Exception e) {
        log.error("error exception while sendmail", e);
        throw new RuntimeException(e);
    }
}

完整错误日志

2026-05-18T10:07:26.234+05:30 ERROR 2684 --- [SERVICEB]
[nio-9898-exec-4] c.t.s.modules.mail.service.MailService :
error exception while sendmail

org.springframework.mail.MailSendException:
Mail server connection failed.

Failed messages:
jakarta.mail.MessagingException: Exception reading response;

nested exception is:
java.net.SocketException: Connection reset

已检查项

  • SMTP主机为smtp.gmail.com
  • 端口为587
  • STARTTLS已启用
  • 网络连接正常

提问

为何会出现java.net.SocketException: Connection reset错误?是否需要使用Gmail应用密码或修改SMTP配置?


解答

核心原因及解决方案

  1. 必须使用Gmail应用密码
    Gmail从2022年起已关闭普通密码的SMTP支持,仅保留两种验证方式:

    • 开启账号两步验证,生成应用密码(专门用于第三方邮件服务),替换配置中的password字段
    • 若未开启两步验证,需开启「不太安全的应用访问」(该选项已逐步淘汰,不推荐)
  2. 网络拦截问题

    • 部分内网(公司/校园网)会拦截587端口请求,可切换到465端口并启用SSL:
      修改application.yml配置:
      spring:
        mail:
          port: 465
          properties:
            mail:
              smtp:
                ssl:
                  enable: true
                starttls:
                  enable: false
                  required: false
      
    • 检查本地防火墙、杀毒软件是否拦截了Java程序的网络连接
  3. 调试日志排查
    确保spring.mail.properties.mail.smtp.debug=true已开启,启动程序后查看详细SMTP交互日志,定位连接中断的具体环节


内容的提问来源于stack exchange,提问作者Omkar Thakur

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.02 02:24:51