1、首先在pom文件开启mail依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
2、utils
@Autowired
private JavaMailSenderImpl mailSender;
/**
* 复杂的邮件
*
* @param multipart :是否支持多部件
* @param subject :主题
* @param text :文本内容
* @param html :是否支持html编码格式
* @param attachmentFilename :附件名
* @param file :附件file位置
* @param from :发送人
* @param to :接收人
* @throws MessagingException
* @Author K
*/
protected void sendMail(Boolean multipart, String subject, String text, Boolean html,
String attachmentFilename, File file,
String from, String to) throws MessagingException {
MimeMessage mimeMessage = mailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,multipart);
helper.setSubject(subject);
helper.setText(text,html);
helper.addAttachment(attachmentFilename,file);
helper.setFrom(from);
helper.setTo(to);
mailSender.send(mimeMessage);
}