springboot java mail sender邮件发送配置

转:

springboot java mail sender邮件发送超时

springboot java mail sender邮件发送超时
西门吹雪碧 2020-10-10 11:18:55 751 收藏
版权

正常来说,发送邮件在本地发送走25端口都不会有问题,但是部署到linux环境就失败?看过很多博主写的,大多相互抄袭,不妨看看下面的解决方案吧。
1. 有关 SpringBoot 邮件服务

Spring Framework 自己有一套基于 JavaMail 的邮件服务包 org.springframework.mail,并通过 JavaMailSender 接口提供了一种简易的发送邮件的方式。这样,开发人员就可以不用操心底层的邮件系统,使用 Spring 提供的接口即可方便地使用邮件服务。官方文档:https://docs.spring.io/spring/docs/5.0.10.RELEASE/spring-framework-reference/integration.html#mail

而在 SpringBoot 中,提供了更为简便的自动配置,又进一步简化了开发人员工作。官方文档:https://docs.spring.io/spring-boot/docs/2.0.6.RELEASE/reference/htmlsingle/#boot-features-email 。下面介绍一下 SpringBoot 中配置邮件服务的具体方法。
2. 具体操作
2.1 添加 Maven 依赖

想要使用 SpringBoot 的邮件服务,需要依赖 spring-boot-starter-mail。所以在 pom 中添加依赖。

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

2.2 配置 yml


一定要注意的点是,25接口有可能被运营商封了,为了验证,可以使用命令 telnet smtp.163.com 25,测试接口是否是可联通的。

如果不行,我们考虑使用465接口,该接口需要ssl证书验证,因此需要做一些权限相关的配置。具体为:starttls、socketFactory下的配置。

    host: 为邮件服务的地址。一般在邮箱设置中可以看到。
    port: 端口号。不同的服务商端口号可能不同,这个也需要自行查看。
    username: 为登录邮箱的用户名。
    password: 这个不是邮箱登录密码,而是第三方授权码。在邮箱设置中或者帮助文档中会有介绍,需要开通第三方授权。
    properties: 其它参数。如果需要使用 SSL 方式,需要配置 mail.smtp.starttls.enable 为 true。有关详细的参数说明见文档https://javaee.github.io/javamail/#Development_Releases

有关 SpringBoot 中 auto-configuration 的参数见源码:
————————————————
版权声明:本文为CSDN博主「西门吹雪碧」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/sduliding/article/details/108993306

转:

JavaMail 发送邮件阻塞问题解决——设置 smtp 超时时间

背景

最近发现项目中有关发送邮件的模块偶尔会阻塞住,导致整个线程阻塞。诡异的是没有捕获到任何异常日志,程序莫名其妙就卡在了 sendMail 上。

后来想到发送邮件的内容过大,可能由于这个原因导致,所以找了一下有关 JavaMail 超时设置的资料。现做整理,顺便聊聊一些小坑。

JavaMail smtp 超时参数

参数类型描述
mail.smtp.connectiontimeout int Socket connection timeout value in milliseconds. This timeout is implemented by java.net.Socket. Default is infinite timeout.
mail.smtp.timeout int Socket read timeout value in milliseconds. This timeout is implemented by java.net.Socket. Default is infinite timeout.
mail.smtp.writetimeout int Socket write timeout value in milliseconds. This timeout is implemented by using a java.util.concurrent.ScheduledExecutorService per connection that schedules a thread to close the socket if the timeout expires. Thus, the overhead of using this timeout is one thread per connection. Default is infinite timeout.

源自 JavaMail API,文末有链接。

参数简介

  • mail.smtp.connectiontimeout:连接时间限制,单位毫秒。是关于与邮件服务器建立连接的时间长短的。默认是无限制。
  • mail.smtp.timeout:邮件接收时间限制,单位毫秒。这个是有关邮件接收时间长短。默认是无限制。
  • mail.smtp.writetimeout:邮件发送时间限制,单位毫秒。有关发送邮件时内容上传的时间长短。默认同样是无限制。

大部分博客、资料都提到了前两个属性,而容易忽略最后一个。因为我是用来发送邮件的,所以上传邮件的内容过大是导致发送模块阻塞的原因。而设置 writetimeout 时间后,再超时时就会报异常,捕获处理下就可以了。

在 SpringBoot 中的配置

我所用的是 SpringBoot 工程,之前也发表过一篇有关 SpringBoot 配置邮件的博客:SpringBoot 配置邮件服务

只需要在原有配置的基础上,加上如下设置即可。

 
spring:
mail:
properties:
mail:
smtp:
timeout: 10000
connectiontimeout: 10000
writetimeout: 10000

站在前人的肩膀上前行,感谢以下资料的支持。

原文地址:https://www.cnblogs.com/libin6505/p/14890010.html