Java Spring Boot 1.2.5.RELEASE - 通过 Gmail SMTP 发送电子邮件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31721298/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 11:30:44  来源:igfitidea点击:

Spring Boot 1.2.5.RELEASE - Sending E-mail via Gmail SMTP

javaspringemailspring-bootjavamail

提问by InsFi

Firstly, I need to say that sending email with 1.2.0.RELEASE works fine

首先,我需要说用 1.2.0.RELEASE 发送电子邮件工作正常

application.properties:

应用程序属性:

spring.mail.host = smtp.gmail.com
spring.mail.username = *****@gmail.com
spring.mail.password = ****
spring.mail.properties.mail.smtp.auth = true
spring.mail.properties.mail.smtp.socketFactory.port = 465
spring.mail.properties.mail.smtp.socketFactory.class = javax.net.ssl.SSLSocketFactory
spring.mail.properties.mail.smtp.socketFactory.fallback = false

pox.xml

痘痘.xml

<parent>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-parent</artifactId>
     <version>1.2.0.RELEASE</version>
     <relativePath/>
</parent>

.......

......

<dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-mail</artifactId>
</dependency>



After changing parent version to 1.2.5.RELEASE email sending hasn't worked

将父版本更改为 1.2.5.RELEASE 后,电子邮件发送无效

Docs says:If spring.mail.host and the relevant libraries (as defined by spring-boot-starter-mail) are available, a default JavaMailSender is created if none exists.

文档说:如果 spring.mail.host 和相关库(由 spring-boot-starter-mail 定义)可用,如果不存在,则会创建默认的 JavaMailSender。


So i've added


所以我添加了

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

It hasn't helped and then i've replaced it to

它没有帮助,然后我将其替换为

<dependency>
    <groupId>com.sun.mail</groupId>
    <artifactId>javax.mail</artifactId>
    <version>1.5.4</version>
</dependency>

Also i've tried

我也试过

spring.mail.host = smtp.gmail.com
spring.mail.username = *****@gmail.com
spring.mail.password = ****
spring.mail.port = 465

Result the same.

It's not a problem to create and configure @Bean manually. But I want to use all beauty of Spring Boot.
Please point me to my mistakes.

结果一样。

手动创建和配置@Bean 不是问题。但我想使用 Spring Boot 的所有美。
请指出我的错误。

Thanks in advance

提前致谢

采纳答案by Andy Wilkinson

It looks like there's a regression/behaviour change in Java Mail. The changeis in both 1.5.3 and 1.5.4. Your app works with Boot 1.2.0 as it uses Java Mail 1.5.2. It fails with Boot 1.2.5 as it uses Java Mail 1.5.4.

看起来 Java Mail 中存在回归/行为变化。更改在 1.5.3 和 1.5.4 中都有。您的应用程序使用 Boot 1.2.0,因为它使用 Java Mail 1.5.2。它在 Boot 1.2.5 中失败,因为它使用 Java Mail 1.5.4。

The problem in 1.5.3+ appears to be that the SMTP transport connects on port 465 and GMail expects an SSL handshake. Java Mail incorrectly thinks it's not using SSL so it never initiates the handshake and the connection attempt (eventually) times out. You can convince Java Mail to do the right thing by being explicit about the use of SSL. Add the following to application.properties:

1.5.3+ 中的问题似乎是 SMTP 传输在端口 465 上连接,而 GMail 需要 SSL 握手。Java Mail 错误地认为它没有使用 SSL,因此它永远不会启动握手并且连接尝试(最终)超时。您可以通过明确说明 SSL 的使用来说服 Java Mail 做正确的事情。将以下内容添加到application.properties

spring.mail.properties.mail.smtp.ssl.enable = true

回答by Stephane Nicoll

It looks like it is a regression. I have created #3624to investigate the issue. Thanks for the sample project!

看起来这是一个回归。我创建了#3624来调查这个问题。感谢您的示例项目!