Springboot中使用mail邮件发送
1、配置邮箱的POP3/SMTP服务和IMAP/SMTP服务
这里使用的是QQ邮箱,进入设置-账户,开启下服务。
开启后获取授权码,保存自己客户端授权码。
2、导入依赖和一些默认#配置新的
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
注意,password不是邮箱的密码,而是授权码。
spring.mail.host=smtp.qq.com
spring.mail.port=587
spring.mail.username=xxxxx@qq.com
spring.mail.password=xxxxxxxxxxx
以上配置好了,我们可以写一些demo。
3、发送邮件
- 简单消息
@RunWith(SpringRunner.class)
@SpringBootTest
public class MailTest {
@Autowired
private JavaMailSender mailSender;
@Test
public void sendMail(){
SimpleMailMessage message = new SimpleMailMessage();
message.setFrom("xxxxxxxx@qq.com");
message.setTo("xxxxxxxx@qq.com");
message.setSubject("主题");
message.setText("文本内容");
mailSender.send(message);
}
}
- 复杂消息
对于复杂消息,我们可以编辑html消息模板,以及实现动态解析的功能。另外还能够携带多附件。
public void sendComplexMsg() throws MessagingException {
MimeMessage mimeMessage = mailSender.createMimeMessage();
//多文件上传
MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
helper.setFrom("xxxxxxxx@qq.com");
helper.setTo("xxxxxxxx@qq.com");
helper.setSubject("面试通知");
//发送html格式
helper.setText(parse(getMod(),getParams()),true);
helper.addAttachment("xxxx公司简章.txt", new File("附件.txt"));
helper.addAttachment("xxxx公司入职须知.txt", new File("附件.txt"));
mailSender.send(mimeMessage);
}
动态解析模板
private String getMod() {
return "<div>\n" +
" 尊敬的#{username}先生(女士),欢迎您应聘我公司#{postion}职位,您的学识、经历给我们留下了良好的印象。为了彼此进一步的了解,特邀请你在#{joinTime}参加面试。收到请回复,谢谢!\n" +
"电话:#{compTel},公司地址:#{compAddr},收到请回复,谢谢!\n" +
"</div>\n" +
"<br>\n" +
"<div style=\"float:right\">\n" +
"\t<div>#{dept}</div>\n" +
"\t<div>#{sendTime}</div>\n" +
"</div>";
}
private String parse(String mod, Map<String, String> params) {
int length = mod.length();
int left = 0;
while ((left = mod.indexOf("#")) != -1 && left < length) {
for (int right = left; right < length; right++) {
if (mod.charAt(right) == '}') {
String key = mod.substring(left + 2, right);
mod = mod.substring(0, left) + params.get(key) + mod.substring(right + 1);
length = mod.length();
left += key.length() + 1;
break;
}
}
}
return mod;
}
public Map<String, String> getParams() {
HashMap<String, String> map = new HashMap<>();
map.put("username", "xxxx");
map.put("postion", "Java开发");
map.put("joinTime", "2023年5月26日下午3:00");
map.put("compTel", "xxxxxxx");
map.put("compAddr", "xxxxxx");
map.put("dept", "xxxxxxxx");
SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");
map.put("sendTime", sdf.format(new Date()));
return map;
}
4、整合工具类
对于以上的内容,我们需要来把它整合成一个工具来使用。
/**
* mail工具类
*/
@Component
public class MailUtil {
@Autowired
private JavaMailSender mailSender;
@Value("${spring.mail.username}")
private String clientMail;
/**
* @param subject 主题
* @param content 内容
* @param fileList 文件
* @param to 接收方
* @throws MessagingException
*/
private void sendMail(String subject, String content, List<File> fileList, String to,boolean isHtml) throws MessagingException {
MimeMessage mimeMessage = mailSender.createMimeMessage();
//是否为多文件上传
boolean mutipart = !CollectionUtils.isEmpty(fileList);
MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, mutipart);
helper.setFrom(clientMail);
helper.setTo(to);
helper.setSubject(subject);
//发送html格式
helper.setText(content, isHtml);
//附件
for (File file : fileList) {
helper.addAttachment(file.getName(), file);
}
mailSender.send(mimeMessage);
}
/**
* 发送Html邮件
* @param subject
* @param mod
* @param params
* @param to
* @param fileList
* @throws MessagingException
*/
public void sendHtmlMsg(String subject, String mod, Map<String, String> params, String to, List<File> fileList) throws MessagingException {
sendMail(subject, parse(mod, params), fileList, to,true);
}
/**
* 发送简单消息邮件
* @param subject
* @param msg
* @param to
* @param fileList
* @throws MessagingException
*/
public void sendSimpleMsg(String subject, String msg, String to, List<File> fileList) throws MessagingException {
sendMail(subject,msg,fileList,to,false);
}
private String parse(String mod, Map<String, String> params) {
int length = mod.length();
int left = 0;
while ((left = mod.indexOf("#")) != -1 && left < length) {
for (int right = left; right < length; right++) {
if (mod.charAt(right) == '}') {
String key = mod.substring(left + 2, right);
mod = mod.substring(0, left) + params.get(key) + mod.substring(right + 1);
length = mod.length();
left += key.length() + 1;
break;
}
}
}
return mod;
}
}
- 编写测试类来测试工具类
@Test
public void testMailUtil() throws MessagingException {
String subject = "面试通知";
String to = "xxxxxxx@qq.com";
List<File> fileList = new ArrayList<>();
fileList.add(new File("附件.txt"));
fileList.add(new File("附件.txt"));
mailUtil.sendHtmlMsg(subject,getMod(),getPart
效果图如下