依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<scope>provided</scope>
</dependency>
配置文件
spring:
mail:
host: smtp.qq.com
username: xxxxxxxx@qq.com
password: axloxxxxxxfqbdgc
port: 587
default-encoding: UTF-8
properties:
mail:
smtp:
ssl:
enable: false
required: false
debug: false
mail工具类
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import javax.mail.MessagingException;
import java.io.File;
import java.util.Date;
@Service
@Slf4j
public class MailService {
@Resource
private JavaMailSenderImpl javaMailSender;
@Value("${spring.mail.username}")
private String sendMailer;
private void checkMail(String to,String subject,String text){
if(StringUtils.isEmpty(to)){
throw new RuntimeException("邮件收信人不能为空");
}
if(StringUtils.isEmpty(subject)){
throw new RuntimeException("邮件主题不能为空");
}
if(StringUtils.isEmpty(text)){
throw new RuntimeException("邮件内容不能为空");
}
}
public void sendTextMailMessage(String to,String subject,String text){
try {
MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(javaMailSender.createMimeMessage(),true);
mimeMessageHelper.setFrom(sendMailer);
mimeMessageHelper.setTo(to.split(","));
mimeMessageHelper.setSubject(subject);
mimeMessageHelper.setText(text);
mimeMessageHelper.setSentDate(new Date());
javaMailSender.send(mimeMessageHelper.getMimeMessage());
log.info("发送邮件成功: {} -> {}", sendMailer, to);
} catch (MessagingException e) {
log.warn("发送邮件失败: {}", e.getMessage());
}
}
public void sendHtmlMailMessage(String to,String subject,String content){
content="<!DOCTYPE html>\n" +
"<html>\n" +
"<head>\n" +
"<meta charset=\"utf-8\">\n" +
"<title>邮件</title>\n" +
"</head>\n" +
"<body>\n" +
"\t<h3>这是一封HTML邮件!</h3>\n" +
"</body>\n" +
"</html>";
try {
MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(javaMailSender.createMimeMessage(),true);
mimeMessageHelper.setFrom(sendMailer);
mimeMessageHelper.setTo(to.split(","));
mimeMessageHelper.setSubject(subject);
mimeMessageHelper.setText(content,true);
mimeMessageHelper.setSentDate(new Date());
javaMailSender.send(mimeMessageHelper.getMimeMessage());
log.info("发送邮件成功: {} -> {}", sendMailer, to);
} catch (MessagingException e) {
log.warn("发送邮件失败: {}", e.getMessage());
}
}
public void sendAttachmentMailMessage(String to,String subject,String content,String filePath){
try {
MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(javaMailSender.createMimeMessage(),true);
mimeMessageHelper.setFrom(sendMailer);
mimeMessageHelper.setTo(to.split(","));
mimeMessageHelper.setSubject(subject);
mimeMessageHelper.setText(content,true);
mimeMessageHelper.setSentDate(new Date());
FileSystemResource file = new FileSystemResource(new File(filePath));
String fileName = file.getFilename();
mimeMessageHelper.addAttachment(fileName, file);
javaMailSender.send(mimeMessageHelper.getMimeMessage());
log.info("发送邮件成功: {} -> {}", sendMailer, to);
} catch (MessagingException e) {
log.warn("发送邮件失败: {}", e.getMessage());
}
}
}
测试类
@Resource
private MailService mailService;
@Test
void test08() {
mailService.sendTextMailMessage("xxxxx@qq.com", "测试标题", "用来测试");
}