1. 簡介
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 使用指南
2. API
Spring Framework 提供了一個使用 JavaMailSender
接口發送電子郵件的簡單抽象,這是發送郵件的核心 API。
JavaMailSender
接口提供的 API 如下:
3. 配置
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.
4. 實戰
4.1. 引入依賴
<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>
4.2. 配置郵件屬性
在 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 = 163
4.3. Java 代碼
首先,需要讀取部分配置屬性,方法如下:
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import org.springframework.validation.annotation.Validated;
@Validated
@Component
@ConfigurationProperties(prefix = "mail")
public class MailProperties {
private String domain;
private String from;
public String getDomain() {
return domain;
}
public void setDomain(String domain) {
this.domain = domain;
}
public String getFrom() {
return from;
}
public void setFrom(String from) {
this.from = from;
}
}
接着,定義一個郵件參數實體類(使用 lombok 簡化了 getter、setter):
import lombok.Data;
import java.util.Date;
@Data
public class MailDTO {
private String from;
private String replyTo;
private String[] to;
private String[] cc;
private String[] bcc;
private Date sentDate;
private String subject;
private String text;
private String[] filenames;
}
接着,實現發送郵件的功能接口:
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;
@Service
public class MailService {
private final Logger log = LoggerFactory.getLogger(this.getClass());
@Autowired
private MailProperties mailProperties;
@Autowired
private JavaMailSender javaMailSender;
@Autowired
private Mapper mapper;
public void sendSimpleMailMessage(MailDTO mailDTO) {
SimpleMailMessage simpleMailMessage = mapper.map(mailDTO, SimpleMailMessage.class);
if (StringUtils.isEmpty(mailDTO.getFrom())) {
mailDTO.setFrom(mailProperties.getFrom());
}
javaMailSender.send(simpleMailMessage);
}
public void sendMimeMessage(MailDTO mailDTO) {
MimeMessage mimeMessage = javaMailSender.createMimeMessage();
MimeMessageHelper messageHelper;
try {
messageHelper = new MimeMessageHelper(mimeMessage, true);
if (StringUtils.isEmpty(mailDTO.getFrom())) {
messageHelper.setFrom(mailProperties.getFrom());
}
messageHelper.setTo(mailDTO.getTo());
messageHelper.setSubject(mailDTO.getSubject());
mimeMessage = messageHelper.getMimeMessage();
MimeBodyPart mimeBodyPart = new MimeBodyPart();
mimeBodyPart.setContent(mailDTO.getText(), "text/html;charset=UTF-8");
// 描述數據關系
MimeMultipart mm = new MimeMultipart();
mm.setSubType("related");
mm.addBodyPart(mimeBodyPart);
// 添加郵件附件
for (String filename : mailDTO.getFilenames()) {
MimeBodyPart attachPart = new MimeBodyPart();
try {
attachPart.attachFile(filename);
} catch (IOException e) {
e.printStackTrace();
}
mm.addBodyPart(attachPart);
}
mimeMessage.setContent(mm);
mimeMessage.saveChanges();
} catch (MessagingException e) {
e.printStackTrace();
}
javaMailSender.send(mimeMessage);
}
}
5. 示例源碼
示例源碼:spring-boot-mail