SpringBoot集成spring-boot-starter-mail发送邮件

本文介绍如何在SpringBoot中使用spring-boot-starter-mail模块集成JavaMailSender接口进行邮件发送。详细解释了配置项如spring.mail.host的作用,以及如何通过spring.mail.properties设置邮件发送的超时时间,避免线程被不响应的邮件服务器阻塞。同时,文章还提到了如何使用JNDI配置JavaMailSender。

SpringBoot集成spring-boot-starter-mail发送邮件

37. Sending Email

The Spring Framework provides an easy abstraction for sending email by using the JavaMailSender interface, and Spring Boot provides auto-configuration for it as well as a starter module.

[Tip]

See the reference documentation for a detailed explanation of how you can use JavaMailSender.

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. The sender can be further customized by configuration items from the spring.mail namespace. See MailProperties for more details.

In particular, certain default timeout values are infinite, and you may want to change that to avoid having a thread blocked by an unresponsive mail server, as shown in the following example:

spring.mail.properties.mail.smtp.connectiontimeout=5000
spring.mail.properties.mail.smtp.timeout=3000
spring.mail.properties.mail.smtp.writetimeout=5000

It is also possible to configure a JavaMailSender with an existing Session from JNDI:

spring.mail.jndi-name=mail/Session

When a jndi-name is set, it takes precedence over all other Session-related settings.

转载于:https://my.oschina.net/moyesen/blog/3001836

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值