【问题标题】:Spring Boot 1.2.5.RELEASE - Sending E-mail via Gmail SMTPSpring Boot 1.2.5.RELEASE - 通过 Gmail SMTP 发送电子邮件
【发布时间】:2017-04-05 02:53:00
【问题描述】:

首先,我需要说明的是,使用 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

<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>


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

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


所以我添加了

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

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

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

我也试过

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

结果相同。

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

提前致谢

【问题讨论】:

  • 那么错误信息是什么?
  • @dunni,没有错误信息。发件人线程只是冻结
  • 所以你有一个使用 Spring Boot 1.2.0 的示例项目并且它可以工作。然后你只是换成spring boot 1.2.5然后它就坏了?据我所知,这两个版本之间没有任何变化。可以分享一下项目吗?
  • @StéphaneNic​​oll , link 我已经用 *** 替换了我的凭据。现在是 1.2.0.RELEASE

标签: java spring email spring-boot jakarta-mail


【解决方案1】:

Java Mail 中似乎存在回归/行为变化。 The change 在 1.5.3 和 1.5.4 中。您的应用程序与 Boot 1.2.0 一起使用,因为它使用 Java Mail 1.5.2。 Boot 1.2.5 失败,因为它使用 Java Mail 1.5.4。

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

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

【讨论】:

  • 也为我工作。由于我的防病毒软件,我遇到了另一个错误。一旦我禁用了邮件保护,邮件就成功发送了
  • 这个答案非常有用。我使用它,以及使用 spring-boot 的“spring.mail.test-connection=true”,来快速解决我的问题。
【解决方案2】:

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

【讨论】:

    猜你喜欢
    • 2017-10-15
    • 2017-05-12
    • 2020-02-21
    • 1970-01-01
    • 2014-01-04
    • 2016-01-16
    • 2015-10-09
    • 2011-08-11
    • 2022-08-14
    相关资源
    最近更新 更多