SpringBoot 2.x ——使用 mail 实现邮件发送
创始人
2024-05-27 05:14:20
0

文章目录

  • 前言
  • 环境、版本等
  • pom依赖引入
  • springboot项目配置文件
    • 获取邮箱授权码
    • 配置properties文件
  • 定义接口信息接收类
  • 编写邮件发送服务类
  • 编写接口
  • swagger测试
    • 1、简单邮件发送
    • 2、html格式发送(支持附件)

前言

最近再看xxl-job的源码,其中在邮件告警通知中使用到了告警信息邮件通知的方式,挺有意思的,特写一篇文章进行简单的配置和使用。

环境、版本等

  • springboot 2.1.4.RELEASE
  • jdk 1.8

pom依赖引入

springboot的版本就已经对mail组件进行了控制,只需要引入对应的依赖即可,无需单独设置版本。(也可以设定指定的版本号)

org.springframework.bootspring-boot-starter-mail

springboot项目配置文件

由于加入了spring-boot-starter-mail依赖组件,此时如果需要使用mail功能,还需要进行下面的几项配置。

获取邮箱授权码

进入QQ邮箱的设置,找到账户
在这里插入图片描述
账户项中下滑至POP3/IMAP/SMTP/Exchange/CardDAV/CalDAV服务
在这里插入图片描述

选择生成授权码。需要发送确认短信信息,当发送成功后,将会获得当前邮箱的授权码信息
将授权码信息复制粘贴到spring.mail.password中即可!

配置properties文件

创建application.properties文件,并在其中配置如下信息:

server.port=80spring.mail.host=smtp.qq.com
spring.mail.port=25
spring.mail.username=302592372@qq.com
spring.mail.from=302592372@qq.com    # 邮件发送者
spring.mail.password=邮箱授权码
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true
spring.mail.properties.mail.smtp.starttls.required=true
spring.mail.properties.mail.smtp.socketFactory.class=javax.net.ssl.SSLSocketFactory

定义接口信息接收类

主要是接口传递参数使用,如下结构:

import lombok.Data;
import java.io.Serializable;@Data
public class MailRequest implements Serializable {/*** 接收人*/private String sendTo;/*** 邮件主题*/private String subject;/*** 邮件内容*/private String text;/*** 附件路径*/private String filePath;}

编写邮件发送服务类

编写邮件发送操作的服务类,使用两种方式:简单邮件内容发送html邮件内容发送

import cn.xj.emails.uo.MailRequest;
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.stereotype.Service;
import org.springframework.util.StringUtils;import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;
import java.util.Date;@Slf4j
@Service
public class SendMailService {@Autowiredprivate JavaMailSender javaMailSender;@Value("${spring.mail.from}")private String sendMailer;/*** 简单邮件内容发送* @param mailRequest*/public void sendSimpleMail(MailRequest mailRequest) {SimpleMailMessage message = new SimpleMailMessage();//邮件发件人message.setFrom(sendMailer);//邮件收件人 1或多个message.setTo(mailRequest.getSendTo().split(","));//邮件主题message.setSubject(mailRequest.getSubject());//邮件内容message.setText(mailRequest.getText());//邮件发送时间message.setSentDate(new Date());javaMailSender.send(message);log.info("发送邮件成功:{}->{}",sendMailer,mailRequest.getSendTo());}/*** Html格式邮件,可带附件* @param mailRequest*/public void sendHtmlMail(MailRequest mailRequest) {MimeMessage message = javaMailSender.createMimeMessage();try {MimeMessageHelper helper = new MimeMessageHelper(message,true);//邮件发件人helper.setFrom(sendMailer);//邮件收件人 1或多个helper.setTo(mailRequest.getSendTo().split(","));//邮件主题helper.setSubject(mailRequest.getSubject());//邮件内容helper.setText(mailRequest.getText(),true);//邮件发送时间helper.setSentDate(new Date());String filePath = mailRequest.getFilePath();if (StringUtils.hasText(filePath)) {FileSystemResource file = new FileSystemResource(new File(filePath));String fileName = filePath.substring(filePath.lastIndexOf(File.separator));helper.addAttachment(fileName,file);}javaMailSender.send(message);log.info("发送邮件成功:{}->{}",sendMailer,mailRequest.getSendTo());} catch (MessagingException e) {log.error("发送邮件时发生异常!",e);}}
}

编写接口

制定一个测试 controller,进行简单的接口开发。

import cn.xj.emails.service.SendMailService;
import cn.xj.emails.uo.MailRequest;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;@RestController
@RequestMapping("/email")
@Api(value = "发送邮件接口",tags = {"发送邮件接口"})
public class TestController {@Autowiredprivate SendMailService sendMailService;@PostMapping("/simple")public void SendSimpleMessage(@RequestBody MailRequest mailRequest) {sendMailService.sendSimpleMail(mailRequest);}@PostMapping("/html")public void SendHtmlMessage(@RequestBody MailRequest mailRequest) { sendMailService.sendHtmlMail(mailRequest);}
}

swagger测试

为了测试的方便,项目中整合了swagger2进行接口测试,当然也可以使用postman等工具。

1、简单邮件发送

/email/simple

在这里插入图片描述
收到邮件如下:
在这里插入图片描述

2、html格式发送(支持附件)

/email/html

在这里插入图片描述
收到的邮件如下所示:
在这里插入图片描述

相关内容

热门资讯

中证A500ETF摩根(560... 8月22日,截止午间收盘,中证A500ETF摩根(560530)涨1.19%,报1.106元,成交额...
A500ETF易方达(1593... 8月22日,截止午间收盘,A500ETF易方达(159361)涨1.28%,报1.104元,成交额1...
何小鹏斥资约2.5亿港元增持小... 每经记者|孙磊    每经编辑|裴健如 8月21日晚间,小鹏汽车发布公告称,公司联...
中证500ETF基金(1593... 8月22日,截止午间收盘,中证500ETF基金(159337)涨0.94%,报1.509元,成交额2...
中证A500ETF华安(159... 8月22日,截止午间收盘,中证A500ETF华安(159359)涨1.15%,报1.139元,成交额...
科创AIETF(588790)... 8月22日,截止午间收盘,科创AIETF(588790)涨4.83%,报0.760元,成交额6.98...
创业板50ETF嘉实(1593... 8月22日,截止午间收盘,创业板50ETF嘉实(159373)涨2.61%,报1.296元,成交额1...
港股异动丨航空股大幅走低 中国... 港股航空股大幅下跌,其中,中国国航跌近7%表现最弱,中国东方航空跌近5%,中国南方航空跌超3%,美兰...
电网设备ETF(159326)... 8月22日,截止午间收盘,电网设备ETF(159326)跌0.25%,报1.198元,成交额409....
红利ETF国企(530880)... 8月22日,截止午间收盘,红利ETF国企(530880)跌0.67%,报1.034元,成交额29.0...