SpringBoot集成QQ/网易/Gmail邮箱发送邮件
2023-02-18 16:44:29 时间
- 添加依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
- application.yml配置文件新增
spring:
mail:
# qq
host: smtp.qq.com #发送邮件服务器
username: xx@qq.com #QQ邮箱
password: xxxxxxxxxxx #客户端授权码
protocol: smtp #发送邮件协议
properties.mail.smtp.auth: true
properties.mail.smtp.port: 465 #端口号465或587
properties.mail.smtp.starttls.enable: true
properties.mail.smtp.starttls.required: true
properties.mail.smtp.ssl.enable: true
default-encoding: utf-8
from: xx@qq.com #与上面的username保持一致
#网易(126/163/yeah)
#126邮箱SMTP服务器地址:smtp.126.com,端口号:465或者994
#163邮箱SMTP服务器地址:smtp.163.com,端口号:465或者994
#yeah邮箱SMTP服务器地址:smtp.yeah.net,端口号:465或者994
host: smtp.126.com
username: xx@126.com
password: xxxxxxxx
protocol: smtp
properties.mail.smtp.auth: true
properties.mail.smtp.port: 994 #465或者994
properties.mail.smtp.starttls.enable: true
properties.mail.smtp.starttls.required: true
properties.mail.smtp.ssl.enable: true
default-encoding: utf-8
from: xx@126.com
# 谷歌邮箱
host: smtp.gmail.com
username: xxx@gmail.com
password: xxxxx #Gmail账号密码
protocol: smtp
properties.mail.smtp.auth: true
properties.mail.smtp.port: 465
properties.mail.smtp.starttls.enable: true
properties.mail.smtp.starttls.required: true
properties.mail.smtp.ssl.enable: true
from: xxx@gmail.com
default-encoding: utf-8
- service封装
/**
* 发送文本邮件
* @param to 发给谁
* @param subject 主题
* @param content 内容
*/
public void sendSimpleMail(String to, String subject, String content);
/**
* 发送HTML邮件
* @param to 发给谁
* @param subject 主题
* @param content 内容
* @throws MessagingException
*/
public void sendHtmlMail(String to, String subject, String content) throws MessagingException;
package com.ruoyi.common.utils.mail;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
@Service("mailService")
public class MailServiceImpl implements MailService {
@Autowired
private JavaMailSender mailSender;
@Value("${spring.mail.from}")
private String from;
/**
* 发送文本邮件
* @param to
* @param subject
* @param content
*/
@Override
@Async("threadPoolTaskExecutor")
public void sendSimpleMail(String to, String subject, String content) {
SimpleMailMessage message = new SimpleMailMessage();
message.setFrom(from);
message.setTo(to);
message.setSubject(subject);
message.setText(content);
mailSender.send(message);
}
/**
* 发送HTML邮件
* @param to
* @param subject
* @param content
*/
@Override
@Async
public void sendHtmlMail(String to, String subject, String content) throws MessagingException {
MimeMessage message = mailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(message, true);
helper.setFrom(from);
helper.setTo(to);
helper.setSubject(subject);
helper.setText(content, true);
mailSender.send(message);
}
}
相关文章
- 腾讯云 x Elasticsearch 携手三周年有奖征文大赛
- 微服务架构下路由、多活、灰度、限流的探索与挑战
- CODING X 小鹅通|助力知识教育行业开启 DevOps 数字化转型新篇章
- 如何做好组装式应用?
- 干货 | 疫情背景下的中长期月度预测方案
- 干货 | 携程酒店慢查询治理之路
- 开源 | 携程度假零成本微前端框架-零界
- RCTF-Web
- 干货 | 记一次跨域配置引发的思考
- RSS Can:将网站信息流转换为 RSS 订阅源(三)
- photoshop2023软件安装包64位最新版下载
- DNSPod十问陈迪菲:从C到B,鹅厂设计师的中场战事
- 【ES三周年】+es快速入门&基础概念讲解
- 行业方案 | 新规落地,企业集团财务公司如何构建数智财务体系?
- TAPD思享汇|高科制造企业如何软硬协同、敏捷提效?
- 瑞幸瞄准蜜雪冰城?
- 干货 | 深度学习在携程搜索词义解析中的应用
- 干货 | Islands Architecture(孤岛架构)在携程新版首页的实践
- 干货 | 提前在开发阶段暴露代码问题,携程Alchemy代码质量平台
- 干货 | 浅谈携程大住宿研发效能提升实践