# 11.4. 发送电子邮件

Spring 框架提供了使用`JavaMailSender`接口发送电子邮件的抽象，Spring Boot 为其提供了自动配置以及启动模块。

> 有关如何使用 的详细说明， 请参阅[`JavaMailSender`参考文档](https://docs.spring.io/spring-framework/reference/6.1/integration/email.html)。

如果`spring.mail.host`和相关库（由`spring-boot-starter-mail` 定义）可用，则创建默认`JavaMailSender`（如果不存在）。发送者可以通过`spring.mail`命名空间中的配置项进一步定制。请参阅[`MailProperties`](https://github.com/spring-projects/spring-boot/tree/v3.2.0/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
```

还可以使用 JNDI 中的现有`Session`配置进行`JavaMailSender`配置：

```
spring.mail.jndi-name=mail/Session
```

设置`jndi-name` 后，它优先于所有其他与会话相关的设置。


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://doc.shiker.tech/spring-boot-can-kao-wen-dang/11.-io/11.4.-fa-song-dian-zi-you-jian.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
