jar包
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
spring.mail.host=smtp.qq.com
spring.mail.username=2856357380@qq.com
spring.mail.nickname=发件人
spring.mail.password=wdwfcftliomddcjb
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true
spring.mail.properties.mail.smtp.starttls.required=true
工具类
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import javax.mail.MessagingException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.io.File;
import java.io.UnsupportedEncodingException;
@Service
@Slf4j
public class MailServiceImpl {
public static final String DEFAULT_ENCODING = "UTF-8";
@Value("${spring.mail.username}")
private String userName;
@Value("${spring.mail.nickname}")
private String nickname;
@Autowired(required = false)
private JavaMailSender mailSender;
@Async("getAsyncExecutor")
public void sendSimpleTextMailActual(String subject,String content,String[] toWho,String[] ccPeoples,String[] bccPeoples,String[] attachments) throws UnsupportedEncodingException {
if(subject == null||toWho == null||toWho.length == 0||content == null){
log.error("邮件-> {} 无法继续执行,因为缺少基本的参数:邮件主题、收件人、邮件内容",subject);
throw new RuntimeException("模板邮件无法继续发送,因为缺少必要的参数!");
}
log.info("开始发送简单文本邮件:主题->{},收件人->{},抄送人->{},密送人->{},附件->{}",subject,toWho,ccPeoples,bccPeoples,attachments);
if(attachments != null&&attachments.length > 0){
try{
MimeMessage mimeMessage = mailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true,DEFAULT_ENCODING);
boolean continueProcess = handleBasicInfo(helper,subject,content,toWho,ccPeoples,bccPeoples,false);
if(!continueProcess){
log.error("邮件基本信息出错: 主题->{}",subject);
return;
}
handleAttachment(helper,subject,attachments);
mailSender.send(mimeMessage);
log.info("发送邮件成功: 主题->{}",subject);
} catch (MessagingException e) {
e.printStackTrace();
log.error("发送邮件失败: 主题->{}",subject);
}
}else{
SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
handleBasicInfo(simpleMailMessage,subject,content,toWho,ccPeoples,bccPeoples);
mailSender.send(simpleMailMessage);
log.info("发送邮件成功: 主题->{}",subject,toWho,ccPeoples,bccPeoples,attachments);
}
}
@Async("getAsyncExecutor")
public void sendHtmlMail(String subject, String content, String[] toWho) {
if(subject == null||toWho == null||toWho.length == 0||content == null){
log.error("邮件-> {} 无法继续执行,因为缺少基本的参数:邮件主题、收件人、邮件内容",subject);
throw new RuntimeException("模板邮件无法继续发送,因为缺少必要的参数!");
}
log.info("开始发送Html邮件:主题->{},收件人->{}",subject,toWho);
MimeMessage mimeMessage = mailSender.createMimeMessage();
try {
MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true,DEFAULT_ENCODING);
handleBasicInfo(helper,subject,content,toWho);
mailSender.send(mimeMessage);
log.info("html邮件发送成功");
} catch (MessagingException e) {
log.error("发送邮件出错->{}",subject);
}
log.info("发送邮件成功: 主题->{}",subject,toWho);
}
public boolean handleBasicInfo(MimeMessageHelper mimeMessageHelper,String subject,String content,String[] toWho,String[] ccPeoples,String[] bccPeoples,boolean isHtml){
try{
InternetAddress address = new InternetAddress(userName, nickname, "gb2312");
mimeMessageHelper.setFrom(address);
mimeMessageHelper.setSubject(subject);
mimeMessageHelper.setText(content,isHtml);
mimeMessageHelper.setTo(toWho);
if(ccPeoples != null)
mimeMessageHelper.setCc(ccPeoples);
if(bccPeoples != null)
mimeMessageHelper.setBcc(bccPeoples);
return true;
}catch(MessagingException e){
e.printStackTrace();
log.error("邮件基本信息出错->{}",subject);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return false;
}
public void handleBasicInfo(SimpleMailMessage simpleMailMessage,String subject,String content,String[] toWho,String[] ccPeoples,String[] bccPeoples) throws UnsupportedEncodingException {
InternetAddress address = new InternetAddress(userName, nickname, "gb2312");
simpleMailMessage.setSubject(subject);
simpleMailMessage.setText(content);
simpleMailMessage.setTo(toWho);
simpleMailMessage.setCc(ccPeoples);
simpleMailMessage.setBcc(bccPeoples);
}
public void handleBasicInfo(MimeMessageHelper mimeMessageHelper,String subject,String content,String[] toWho){
try {
InternetAddress address = new InternetAddress(userName, nickname, "gb2312");
mimeMessageHelper.setFrom(address);
mimeMessageHelper.setSubject(subject);
mimeMessageHelper.setText(content,true);
mimeMessageHelper.setTo(toWho);
} catch (MessagingException e) {
log.error("html邮件基本信息出错->{}",subject);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
public void handleAttachment(MimeMessageHelper mimeMessageHelper,String subject,String[] attachmentFilePaths){
if(attachmentFilePaths != null&&attachmentFilePaths.length > 0) {
FileSystemResource resource;
String fileName;
for (String attachmentFilePath : attachmentFilePaths) {
resource = new FileSystemResource(new File(attachmentFilePath));
if (!resource.exists()) {
log.warn("邮件->{} 的附件->{} 不存在!", subject, attachmentFilePath);
continue;
}
fileName = resource.getFilename();
try {
mimeMessageHelper.addAttachment(fileName, resource);
} catch (MessagingException e) {
e.printStackTrace();
log.error("邮件->{} 添加附件->{} 出现异常->{}", subject, attachmentFilePath, e.getMessage());
}
}
}
}
}
使用
mailService.sendHtmlMail("点名时间用户注册",
"点击下面链接激活账号" +
"<a href='http://localhost:8080/user/active?jwt="+JwtUtil.sign(map)+"'>点击激活</a>",
new String[]{userParam.getUserEmail()});