邮件发送时一直报错问题排查

今天需要用Spring Boot发送邮件,使用的腾讯企业邮箱(QQ邮箱),yaml配置如下:

spring:
  mail:
    host: smtp.exmail.qq.com
    port: 465
    username: biz-noreply@example.com
    password: password-example
    properties:
      mail:
        smtp:
          auth: true
          starttls:
            enable: true
            required: true

Spring Boot引入了mail包之后,使用自动注入的BeanJavaMailSender发送邮件,代码如下:

SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
simpleMailMessage.setFrom("biz-noreply@example.com");
simpleMailMessage.setTo(emails);
simpleMailMessage.setSubject(title);
simpleMailMessage.setText(content);
javaMailSender.send(simpleMailMessage);

结果每次在发送时,始终都会报Could not connect to SMTP host: smtp.exmail.qq.com, port: 465, response: -1的错误,怎么试都是这个结果,错误详情如下:

javax.mail.MessagingException: Could not connect to SMTP host: smtp.exmail.qq.com, port: 465, response: -1
    at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:2197)
    at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:740)
    at javax.mail.Service.connect(Service.java:366)
    at org.springframework.mail.javamail.JavaMailSenderImpl.connectTransport(JavaMailSenderImpl.java:515)
    at org.springframework.mail.javamail.JavaMailSenderImpl.testConnection(JavaMailSenderImpl.java:396)
    at org.springframework.boot.actuate.mail.MailHealthIndicator.doHealthCheck(MailHealthIndicator.java:43)
    at org.springframework.boot.actuate.health.AbstractHealthIndicator.health(AbstractHealthIndicator.java:84)
    at org.springframework.boot.actuate.health.CompositeHealthIndicator.health(CompositeHealthIndicator.java:68)
    at org.springframework.boot.actuate.health.HealthEndpoint.health(HealthEndpoint.java:47)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)

后来,把端口从465改为587就好了,就是这么神奇。

而且,腾讯企业邮的帮助文档里,压根就没提到587这个端口(http://service.exmail.qq.com/cgi-bin/help?subtype=1&&id=28&&no=1000585 )!我还是在QQ邮箱的帮助页面找到的这个端口…