博主介绍:✌全网粉丝3W+,全栈开发工程师,从事多年软件开发,在大厂呆过。持有软件中级、六级等证书。可提供微服务项目搭建与毕业项目实战,博主也曾写过优秀论文,查重率极低,在这方面有丰富的经验✌
博主作品:《Java项目案例》主要基于SpringBoot+MyBatis/MyBatis-plus+MySQL+Vue等前后端分离项目,可以在左边的分类专栏找到更多项目。《Uniapp项目案例》有几个有uniapp教程,企业实战开发。《微服务实战》专栏是本人的实战经验总结,《Spring家族及微服务系列》专注Spring、SpringMVC、SpringBoot、SpringCloud系列、Nacos等源码解读、热门面试题、架构设计等。除此之外还有不少文章等你来细细品味,更多惊喜等着你哦
🍅开源项目免费哦(有vue2与vue3版本): 点击这里克隆或者下载 🍅
🍅文末获取联系🍅精彩专栏推荐订阅👇🏻👇🏻 不然下次找不到哟
Java项目案例《100套》
https://blog.csdn.net/qq_57756904/category_12173599.html
uniapp小程序《100套》https://blog.csdn.net/qq_57756904/category_12199600.html
目录
编辑
一、获取授权码
1、以QQ邮箱为例,页面首部找到设置
二、SpringBoot集成邮件发送
1、环境配置
2、代码编写
三、测试邮件发送
一、获取授权码
1、以QQ邮箱为例,页面首部找到设置
2、开启POP3/SMTP服务
如果服务不是开启状态需要开启,服务开启状态可以点击管理服务去获取授权码。
获取授权码这次本人需要绑定手机号,绑定手机后就可以点击生成授权码
二、SpringBoot集成邮件发送
1、环境配置
- 添加依赖
<!-- springboot 邮件mail --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-mail</artifactId><version>${springboot.version}</version></dependency>
- application.properties
# 邮件
spring.mail.default-encoding= utf-8
# 配置 SMTP 服务器地址
spring.mail.host= smtp.qq.com
#发送方邮件名
spring.mail.username= xxxx@qq.com
#授权码
spring.mail.password= xxxxxx
- application.yml(与application.properties任选其一既可)
spring:#邮箱基本配置mail:#配置smtp服务主机地址host: smtp.qq.com#发送者邮箱username: xxxxxxx@qq.com#配置密码,注意不是真正的密码,而是刚刚申请到的授权码password: xxxxxxxxxxx#端口号465或587port: 587#默认的邮件编码为UTF-8default-encoding: UTF-8#其他参数properties:mail:# 配置SSL 加密工厂smtp:ssl:# 本地测试,先放开sslenable: falserequired: falsesocketFactory:class: javax.net.ssl.SSLSocketFactory# 开启debug模式,这样邮件发送过程的日志会在控制台打印出来,方便排查错误debug: true
2、代码编写
- MailRequest实体
package com.ceam.exam.dto;import java.io.Serializable;/*** @author CeaM* 2023/7/17*/
public class MailRequest implements Serializable {/*** 接收人*/private String sendTo;/*** 邮件主题*/private String subject;/*** 邮件内容*/private String text;/*** 附件路径*/private String filePath;public String getSendTo() {return sendTo;}public void setSendTo(String sendTo) {this.sendTo = sendTo;}public String getSubject() {return subject;}public void setSubject(String subject) {this.subject = subject;}public String getText() {return text;}public void setText(String text) {this.text = text;}public String getFilePath() {return filePath;}public void setFilePath(String filePath) {this.filePath = filePath;}
}
- SendMailService
package com.ceam.web.components.service;import com.ceam.exam.dto.MailRequest;/*** @author CeaM* 2023/7/17*/
public interface SendMailService {/*** 简单文本邮件** @param mailRequest* @return*/void sendSimpleMail(MailRequest mailRequest);/*** Html格式邮件,可带附件** @param mailRequest* @return*/void sendHtmlMail(MailRequest mailRequest);
}
- SendMailServiceImpl
package com.ceam.web.components.service.impl;import com.ceam.common.utils.AssertUtil;
import com.ceam.exam.dto.MailRequest;
import com.ceam.web.components.service.SendMailService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;import javax.annotation.Resource;
import java.util.Date;/*** @author CeaM* 2023/7/17*/
@Slf4j
@Service
public class SendMailServiceImpl implements SendMailService {// 注入邮件工具类@Resourceprivate JavaMailSender javaMailSender;@Value("${spring.mail.username}")private String sendMailer;public void checkMail(MailRequest mailRequest) {AssertUtil.isNotEmpty(mailRequest,"邮件请求不能为空");AssertUtil.isNotBlank(mailRequest.getSendTo(), "邮件收件人不能为空");AssertUtil.isNotBlank(mailRequest.getSubject(), "邮件主题不能为空");AssertUtil.isNotBlank(mailRequest.getText(), "邮件收件人不能为空");}@Overridepublic void sendSimpleMail(MailRequest mailRequest) {SimpleMailMessage message = new SimpleMailMessage();checkMail(mailRequest);// 邮件发件人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());}@Overridepublic void sendHtmlMail(MailRequest mailRequest) {
// MimeMessage message = javaMailSender.createMimeMessage();
// checkMail(mailRequest);
// 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);
// }}
}
- SendMailController
@RestController
@RequestMapping("/send-mail")
@Api(value = "发送邮件接口",tags = {"发送邮件接口"})
public class SendMailController {@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);}
}
三、测试邮件发送