javascript
Spring Boot 之发送邮件
?
目錄
Spring Boot 之發送郵件簡介
API
配置
實戰
完整示例
引申和引用
Spring Boot 之發送郵件
- 簡介
- API
- 配置
- 實戰
- 引入依賴
- 配置郵件屬性
- Java 代碼
- 完整示例
- 引申和引用
簡介
Spring Boot 收發郵件最簡便方式是通過?spring-boot-starter-mail。
<dependency><groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId> </dependency>spring-boot-starter-mail 本質上是使用 JavaMail(javax.mail)。如果想對 JavaMail 有進一步了解,可以參考:JavaMail 使用小結
API
Spring Framework 提供了一個使用?JavaMailSender?接口發送電子郵件的簡單抽象,這是發送郵件的核心 API。
JavaMailSender?接口提供的 API 如下:
配置
Spring Boot 為?JavaMailSender?提供了自動配置以及啟動器模塊。
如果?spring.mail.host?和相關庫(由 spring-boot-starter-mail 定義)可用,則 Spring Boot 會創建默認?JavaMailSender(如果不存在)。可以通過?spring.mail?命名空間中的配置項進一步自定義發件人。
特別是,某些默認超時值是無限的,您可能希望更改它以避免線程被無響應的郵件服務器阻塞,如以下示例所示:
也可以使用 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> <groupId>com.github.dozermapper</groupId> <artifactId>dozer-spring-boot-starter</artifactId> <version>6.4.0</version> </dependency> </dependencies>配置郵件屬性
在?src/main/resources?目錄下添加?application-163.properties?配置文件,內容如下:
spring.mail.host = smtp.163.com spring.mail.username = xxxxxx spring.mail.password = xxxxxx spring.mail.properties.mail.smtp.auth = true spring.mail.properties.mail.smtp.starttls.enable = true spring.mail.properties.mail.smtp.starttls.required = true spring.mail.default-encoding = UTF-8 mail.domain = 163.com mail.from = ${spring.mail.username}@${mail.domain}注:需替換有效的?spring.mail.username、spring.mail.password。
application-163.properties?配置文件表示使用 163 郵箱時的配置,為了使之生效,需要通過?spring.profiles.active = 163?來激活它。
在?src/main/resources?目錄下添加?application.properties?配置文件,內容如下:
spring.profiles.active = 163Java 代碼
首先,需要讀取部分配置屬性,方法如下:
import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component; import org.springframework.validation.annotation.Validated;接著,定義一個郵件參數實體類(使用 lombok 簡化了 getter、setter):
import lombok.Data; import java.util.Date;接著,實現發送郵件的功能接口:
import com.github.dozermapper.core.Mapper; import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.mail.SimpleMailMessage; import org.springframework.mail.javamail.JavaMailSender; import org.springframework.mail.javamail.MimeMessageHelper; import org.springframework.stereotype.Service; import javax.mail.MessagingException; import javax.mail.internet.MimeBodyPart; import javax.mail.internet.MimeMessage; import javax.mail.internet.MimeMultipart; import java.io.IOException;完整示例
完整示例:源碼
使用方法:
引申和引用
引申
- Spring Boot 教程
參考
- Spring Boot 官方文檔之 Sending Email
轉載于:https://www.cnblogs.com/williamjie/p/10250710.html
總結
以上是生活随笔為你收集整理的Spring Boot 之发送邮件的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 下列关于葡萄糖酸钙锌口服溶液,表述错误的
- 下一篇: SpringBoot基础系列-Sprin