配置
Spring Boot 为
JavaMailSender提供了自动配置以及启动器模块。如果
spring.mail.host和相关库(由 spring-boot-starter-mail 定义)可用,则 Spring Boot 会创建默认JavaMailSender(如果不存在)。可以通过spring.mail命名空间中的配置项进一步自定义发件人。
特别是,某些默认超时值是无限的,您可能希望更改它以避免线程被无响应的邮件服务器阻塞,如以下示例所示:spring.mail.properties.mail.smtp.connectiontimeout=5000 spring.mail.properties.mail.smtp.timeout=3000 spring.mail.properties.mail.smtp.writetimeout=5000也可以使用 JNDI 中的现有会话配置
JavaMailSender:spring.mail.jndi-name=mail/Session以下为 Spring Boot 关于 Mail 的配置:
有关更多详细信息,请参阅
MailProperties。# Email (MailProperties) spring.mail.default-encoding=UTF-8 # Default MimeMessage encoding. spring.mail.host= # SMTP server host. For instance, `smtp.example.com`. spring.mail.jndi-name= # Session JNDI name. When set, takes precedence over other Session settings. spring.mail.password= # Login password of the SMTP server. spring.mail.port= # SMTP server port. spring.mail.properties.*= # Additional JavaMail Session properties. spring.mail.protocol=smtp # Protocol used by the SMTP server. spring.mail.test-connection=false # Whether to test that the mail server is available on startup. spring.mail.username= # Login user of the SMTP server.实战
引入依赖
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency> <dependency>
