一、创建一个普通的SpringBoot工程(使用maven构建)
二、在pom.xml下添加依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
三、在resources目录下的application.properties添加下述配置
spring.mail.host = smtp.163.com
spring.mail.username = xxxxxxx@163.com
spring.mail.password =
spring.mail.properties.mail.smtp.auth = true
spring.mail.properties..mail.smtp.timeout = 25000
四、创建一个EmailConfig类,从application.properties中获取发件人邮箱(后续需要用到)
@Component
public class EmailConfig {
@Value("${spring.mail.username}")
private String emailFrom;
public String getEmailFrom() {
return emailFrom;
}
public void setEmailFrom(String emailFrom) {
this.emailFrom = emailFrom;
}
}
五、创建一个发送邮件接口EmailService
public interface EmailService {
/**
* 发送简单邮件
* @param sendTo 收件人
* @param title 邮件标题
* @param content 邮件内容
*/
void sendSimpleMail(String sendTo, String title, String content);
/**
* 发送简单邮件(带附件)
* @param sendTo 收件人
* @param titel 邮件标题
* @param content 邮件内容
* @param attachments <文件名,附件> 附件列表
*/
void sendAttachmentsMail(String sendTo, String titel, String content, List<Pair<String, File>> attachments);
六、实现EmailService接口
@Service
public class EmailServiceImpl implements EmailService{
@Autowired
private EmailConfig emailConfig;
@Autowired
private JavaMailSender mailSender;
/**
* 发送简单邮件
* @param sendTo 收件人
* @param title 邮件标题
* @param content 邮件内容
*/
@Override
public void sendSimpleMail(String sendTo, String title, String content) {
SimpleMailMessage message = new SimpleMailMessage();
message.setFrom(emailConfig.getEmailFrom());
message.setTo(sendTo);
message.setSubject(title);
message.setText(content);
mailSender.send(message);
System.out.println("邮件已发送");
}
/**
* 发送简单邮件(带附件)
* @param sendTo 收件人
* @param titel 邮件标题
* @param content 邮件内容
* @param attachments <文件名,附件> 附件列表
*/
@Override
public void sendAttachmentsMail(String sendTo, String titel, String content, List<Pair<String, File>> attachments) {
MimeMessage mimeMessage = mailSender.createMimeMessage();
try {
MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
helper.setFrom(emailConfig.getEmailFrom());
helper.setTo(sendTo);
helper.setSubject(titel);
helper.setText(content);
for (Pair<String, File> pair : attachments) {
helper.addAttachment(pair.getKey(),new FileSystemResource(pair.getValue()));
}
} catch (MessagingException e) {
e.printStackTrace();
}
mailSender.send(mimeMessage);
}
七、创建一个Controller类用于测试
@RestController
public class EmailController {
@Autowired
private EmailServiceImpl emailService;
//发送简单邮件
@GetMapping("/email")
public String simpleEmail(){
String sendTo = "xxxx@qq.com";
String title = "测试";
String content = "测试成功";
emailService.sendSimpleMail(sendTo,title,content);
return "邮件已发送,请查收";
}
//发送简单邮件(带附件)
@GetMapping("/attemail")
public String attachmentsMail(){
String sendTo = "xxx@qq.com";
String title = "测试";
String content = "测试成功";
List<Pair<String,File>> attachments = new ArrayList<>();
attachments.add(new Pair<String, File>("1.png",new File("D:\\devsoft\\截图\\1.png")));
emailService.sendAttachmentsMail(sendTo,title,content,attachments);
return "邮件已发送,请查收";
}
八、访问localhost:8080/email 或 localhost:8080/attemail(注意更改File路径),结果查看邮箱即可