1. 邮箱发送
2. 准备工作
3. 整合SpringBoot
3.1 配置
依赖引入
<!-- 邮件服务-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
application.yml
spring:
mail:
host: smtp.qq.com
port: 587
username: QQ邮箱
password: 授权码
default-encoding: UTF-8
properties:
mail:
debug: true
smtp:
sockFactory:
class: javax.net.ssl.SSLSocketFactory
其他
邮件对象
package com.ruoyi.common.vo;
import lombok.Data;
/**
* 邮件对象
*/
@Data
public class MailMessage {
//发送者
private String from;
//接受者
private String to;
//抄送人
private String cc;
//主题
private String subject;
//内容
private String text;
//附件
private MultipartFile file;
}
3.2 发送简单邮件
创建邮箱组件
package com.ruoyi.common.component;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.ruoyi.common.vo.MailMessage;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
/**
* qq邮箱
*/
@Component
public class MailService {
@Value("${spring.mail.username}")
private String mailFrom;
@Resource
private JavaMailSender javaMailSender;
/**
* 发送邮件
*
* @param from 发送者
* @param to 收件人
* @param cc 抄送人
* @param subject 主题
* @param content 内容
*/
public void sendSimpleMail(String from, String to, String cc, String subject, String content) {
SimpleMailMessage mailMessage = new SimpleMailMessage();
mailMessage.setFrom(mailFrom);
mailMessage.setTo(to);
mailMessage.setCc(cc);
mailMessage.setSubject(subject);
mailMessage.setText(content);
javaMailSender.send(mailMessage);
}
public void sendSimpleMail(MailMessage message) {
SimpleMailMessage mailMessage = new SimpleMailMessage();
mailMessage.setFrom(mailFrom);
String[] tos = StrUtil.splitToArray(message.getTo(), ";");
mailMessage.setTo(tos);
String[] ccs = StrUtil.splitToArray(message.getCc(), ";");
mailMessage.setCc(ccs);
mailMessage.setSubject(message.getSubject());
mailMessage.setText(message.getText());
javaMailSender.send(mailMessage);
}
}
测试
控制层
@Resource
private MailService mailService;
/**
* 发送邮件
*
* @param user
* @return
*/
@GetMapping("/mail")
public AjaxResult mail(MailMessage mailMessage) {
mailService.sendSimpleMail(mailMessage);
return AjaxResult.success();
}
3.3 发送带附件的邮件
邮箱组件
package com.ruoyi.common.component;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.ruoyi.common.vo.MailMessage;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;
/**
* qq邮箱
*/
@Component
public class MailService {
@Value("${spring.mail.username}")
private String mailFrom;
@Resource
private JavaMailSender javaMailSender;
/**
* 发送带附件的邮件
*
* @param message
* @param file
*/
public void sendAttachFileMail(MailMessage message, File file) {
try {
MimeMessage mimeMessage = javaMailSender.createMimeMessage();
MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage, true);
mimeMessageHelper.setFrom(mailFrom);
String[] tos = StrUtil.splitToArray(message.getTo(), ";");
mimeMessageHelper.setTo(tos);
String[] ccs = StrUtil.splitToArray(message.getCc(), ";");
mimeMessageHelper.setCc(ccs);
mimeMessageHelper.setSubject(message.getSubject());
mimeMessageHelper.setText(message.getText());
mimeMessageHelper.addAttachment(file.getName(), file);
javaMailSender.send(mimeMessage);
} catch (MessagingException e) {
throw new RuntimeException(e);
}
}
}
使用及测试
@Resource
private MailService mailService;
/**
* 发送邮件带附件
*
* @param mailMessage
* @return
*/
@PostMapping("/mailFile")
public AjaxResult mailFile(MailMessage mailMessage, MultipartFile multipartFile) {
try {
// 获取文件名
String fileName = multipartFile.getOriginalFilename();
// 获取文件后缀(.xml)
String suffix = fileName.substring(fileName.lastIndexOf("."));
File file = File.createTempFile(fileName.substring(0, fileName.lastIndexOf(".")), suffix);
multipartFile.transferTo(file);
mailService.sendAttachFileMail(mailMessage, file);
} catch (IOException e) {
throw new RuntimeException(e);
}
return AjaxResult.success();
}
3.4 发送带图片资源的邮件
组件
package com.ruoyi.common.component;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.vo.MailMessage;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
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.stereotype.Component;
import org.springframework.web.util.HtmlUtils;
import javax.annotation.Resource;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;
/**
* qq邮箱
*/
@Component
public class MailService {
@Value("${spring.mail.username}")
private String mailFrom;
@Resource
private JavaMailSender javaMailSender;
/**
* 发送正文带图片的邮件
*
* @param message
*/
public void sendMailWithirng(MailMessage message) {
String[] srcPath = message.getSrcPath().split(",");
String[] resIds = message.getResIds().split(",");
if (srcPath.length != resIds.length) {
throw new ServiceException("图片信息不全,发送失败!");
}
try {
MimeMessage mimeMessage = javaMailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
helper.setFrom(mailFrom);
String[] tos = StrUtil.splitToArray(message.getTo(), ";");
helper.setTo(tos);
String[] ccs = StrUtil.splitToArray(message.getCc(), ";");
helper.setCc(ccs);
helper.setSubject(message.getSubject());
helper.setText(message.getText(), true);
for (int i = 0; i < srcPath.length; i++) {
FileSystemResource fileSystemResource = new FileSystemResource(new File(srcPath[i]));
helper.addInline(resIds[i], fileSystemResource);
}
javaMailSender.send(mimeMessage);
} catch (MessagingException e) {
throw new RuntimeException(e);
}
}
}
测试
@Resource
private MailService mailService;
/**
* 发送正文带图片的邮件
*
* @param mailMessage
* @return
*/
@PostMapping("/sendMailWithirng")
public AjaxResult sendMailWithirng(@Validated @RequestBody MailMessage mailMessage) {
mailService.sendMailWithirng(mailMessage);
return AjaxResult.success();
}
3.5 使用 FreeMarker 构建邮件模报
依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
组件
package com.ruoyi.common.component;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.vo.MailMessage;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
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.stereotype.Component;
import org.springframework.web.util.HtmlUtils;
import javax.annotation.Resource;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;
/**
* qq邮箱
*/
@Component
public class MailService {
@Value("${spring.mail.username}")
private String mailFrom;
@Resource
private JavaMailSender javaMailSender;
/**
* 使用 FreeMarker 构建邮件模报
*
* @param message
*/
public void sendHtmlMail(MailMessage message) {
try {
MimeMessage mimeMessage = javaMailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
helper.setFrom(mailFrom);
String[] tos = StrUtil.splitToArray(message.getTo(), ";");
helper.setTo(tos);
String[] ccs = StrUtil.splitToArray(message.getCc(), ";");
helper.setCc(ccs);
helper.setSubject(message.getSubject());
helper.setText(message.getText(), true);
javaMailSender.send(mimeMessage);
} catch (MessagingException e) {
throw new RuntimeException(e);
}
}
}
邮件模板
3.6 使用 Thymeleaf 构建邮件模板