实体类vo:
package com.cxqy.activity.dto;
import lombok.Data;
import org.codehaus.jackson.annotate.JsonIgnore;
import org.codehaus.jackson.annotate.JsonIgnoreProperties;
import org.springframework.web.multipart.MultipartFile;
import javax.validation.constraints.NotBlank;
import java.util.Date;
/**
* @Author yjl
* @Date 2022/1/21 14:33
* @Version 1.0
*/
@Data
@JsonIgnoreProperties(value = { "multipartFiles"})
public class MailVo {
private String id;
private String from;
@NotBlank(message = "收件人不能为空")
private String to;
@NotBlank(message = "邮件主题不能为空")
private String subject;
@NotBlank(message = "邮件内容不能为空")
private String text;
private Date sentDate;
private String cc;
private String bcc;
private String status;
private String error;
private MultipartFile[] multipartFiles;
}
-----------------------------
package com.cxqy.activity.controller;
import com.cxqy.activity.dto.MailVo;
import com.cxqy.activity.service.MailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;
import javax.validation.Valid;
/**
* @Author yjl
* @Date 2022/1/21 14:47
* @Version 1.0
*/
@RestController
@RequestMapping("/mails")
public class MailController {
@Autowired
private MailService mailService;
@GetMapping("/")
public ModelAndView index() {
ModelAndView mv = new ModelAndView("mail/sendMail");
mv.addObject("from", mailService.getMailSendFrom());
return mv;
}
@PostMapping("/send")
public MailVo sendMail(@Valid MailVo mailVo, MultipartFile[] files) {
mailVo.setMultipartFiles(files);
return mailService.sendMail(mailVo);
}
}
--------------------
package com.cxqy.activity.service;
import com.cxqy.activity.dto.MailVo;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import java.util.Date;
/**
* @Author yjl
* @Date 2022/1/21 14:37
* @Version 1.0
*/
public interface MailService {
MailVo sendMail(MailVo mailVo);
String getMailSendFrom();
}
-----------------------
package com.cxqy.activity.service.impl;
import com.cxqy.activity.dto.MailVo;
import com.cxqy.activity.service.MailService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import java.util.Date;
/**
* @Author yjl
* @Date 2022/1/21 15:11
* @Version 1.0
*/
@Slf4j
@Service
public class MailServiceImpl implements MailService {
private Logger logger = LoggerFactory.getLogger(getClass());
@Autowired
private JavaMailSenderImpl mailSender;
@Override
public MailVo sendMail(MailVo mailVo) {
try {
sendMimeMail(mailVo);
return saveMail(mailVo);
} catch (Exception e) {
logger.error("发送邮件失败:", e);
mailVo.setStatus("fail");
mailVo.setError(e.getMessage());
return mailVo;
}
}
@Override
public String getMailSendFrom() {
return mailSender.getJavaMailProperties().getProperty("from");
}
private void sendMimeMail(MailVo mailVo) {
try {
MimeMessageHelper messageHelper = new MimeMessageHelper(mailSender.createMimeMessage(), true);
mailVo.setFrom(getMailSendFrom());
messageHelper.setFrom(mailVo.getFrom());
messageHelper.setTo(mailVo.getTo().split(","));
messageHelper.setSubject(mailVo.getSubject());
messageHelper.setText(mailVo.getText());
if (!StringUtils.isEmpty(mailVo.getCc())) {
messageHelper.setCc(mailVo.getCc().split(","));
}
if (!StringUtils.isEmpty(mailVo.getBcc())) {
messageHelper.setCc(mailVo.getBcc().split(","));
}
if (mailVo.getMultipartFiles() != null) {
for (MultipartFile multipartFile : mailVo.getMultipartFiles()) {
messageHelper.addAttachment(multipartFile.getOriginalFilename(), multipartFile);
}
}
if (mailVo.getSentDate() ==null) {
mailVo.setSentDate(new Date());
messageHelper.setSentDate(mailVo.getSentDate());
}
mailSender.send(messageHelper.getMimeMessage());
mailVo.setStatus("ok");
logger.info("发送邮件成功:{}->{}", mailVo.getFrom(), mailVo.getTo());
} catch (Exception e) {
throw new RuntimeException(e);
}
}
private MailVo saveMail(MailVo mailVo) {
return null;
}
}
--------------yml配置
spring:
application:
name: @project.artifactId@
profiles:
active: local
flyway:
baseline-on-migrate: true
servlet:
multipart:
max-file-size: 200MB
max-request-size: 300MB
jackson:
date-format: yyyy-MM-dd HH:mm:ss
time-zone: UTC
server:
port: 8610
spring.mail:
host: smtp.163.com #SMTP服务器地址
username: xxxxx #登陆账号
password: xxxxx #登陆密码(或授权码)
default-encoding: utf-8
properties:
from: xxx@163.com #邮件发信人(即真实邮箱)
thymeleaf:
cache: false
prefix: classpath:/views/
servlet:
multipart:
max-file-size: 10MB #限制单个文件大小
max-request-size: 50MB #限制请求总量
pagehelper:
helper-dialect: mysql
reasonable: false
support-methods-arguments: true
params: count=countSql
------------------------------------------------------pom依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>