1. 导入依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
2. application.yaml配置
spring:
mail:
default-encoding: UTF-8
host: smtp.qq.com
username: xxxxx@qq.com
password: xxxxxxxxx
3.如何获取password
百度百科的获取步骤
4. 制作发送工具类
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Component;
@Component
public class EmailUtil {
@Autowired
private JavaMailSender mailSender;
private static final String senderEmail = "xxxxx@qq.com";
public void sendEmail(String title,String content,String... emails) {
SimpleMailMessage mailMessage = new SimpleMailMessage();
mailMessage.setSubject(title);
mailMessage.setText(content);
mailMessage.setTo(emails);
mailMessage.setFrom(senderEmail);
mailSender.send(mailMessage);
}
}
最后测试
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
public class EmailTest {
@Autowired
private EmailUtil emailUtil;
@Test
public void test01() {
emailUtil.sendEmail("测试","做个测试","??????@qq.com");
}
}