> For the complete documentation index, see [llms.txt](https://jack80342.gitbook.io/spring-boot/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://jack80342.gitbook.io/spring-boot/iv.-spring-boot-features/36.-sending-email.md).

# 36. 发送邮件

Spring框架通过`JavaMailSender`接口为发送邮件提供了一个简单的抽象，并且Spring Boot也为它提供了自动配置和一个starter模块。 具体查看[`JavaMailSender`参考文档](https://docs.spring.io/spring/docs/5.0.4.RELEASE/spring-framework-reference/integration.html#mail)。

如果`spring.mail.host`和相关的libraries（通过`spring-boot-starter-mail`定义的）都可用，Spring Boot将创建一个默认的`JavaMailSender`，该sender可以通过`spring.mail`命名空间下的配置项进一步自定义，具体参考[MailProperties](https://github.com/spring-projects/spring-boot/tree/v2.0.0.RELEASE/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/mail/MailProperties.java)。

特别地，某些默认的超时值是无限的。你可能想改变它，以避免由无响应的邮件服务器引起的线程阻塞：

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