SpringBoot集成QQ/网易/Gmail邮箱发送邮件
2023-06-13 09:16:15 时间
- 添加依赖
<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);
}
}
相关文章
- SpringBoot的启动流程_springboot启动卡住了
- java山寨qq账号密码验证_Java实战-山寨QQ
- qq怎样防盗 qq密码如何防盗[通俗易懂]
- QQ农场怎么开挂_qq农场矿山辅助
- 这几天用QQ音乐听歌,我发现了一个不,不得了的事情!!
- 通过Wireshark监听流量,查找对方微信、QQ、钉钉的IP地址 – 学金融的文史哲小生
- QQ 浏览器搜索相关性实践
- QQ音乐可以在Linux平台上欣赏(qq音乐linux)
- Linux下搭乐:QQ给您无限乐趣(linux版的qq)
- 奇瑞QQ冰淇凌最新预告图公布 竞争五菱宏光MINI EV
- 版本QQ邮箱开放Linux版本下载(qq邮箱linux)
- QQ浏览器 for Mac 3.3版诚邀体验
- Linux下完美安装QQ的简易教程(linux安装qq教程)
- 轻松开启Linux的社交时代:安装Linux版QQ(linux版qq)
- 使用Linux版QQ,享受更丰富的功能(linux版本qq)
- 行完成在Linux系统下安装QQ:仅需一行命令!(linux安装qq命令)
- 游戏人间: 在 Linux 上玩 QQ 游戏(qq游戏linux)
- QQ网页版轻松实现Linux平台即时通讯(qq网页版linux)
- 在Linux系统中安装QQ,轻松给你的生活带来更多乐趣(qq安装linux)
- 腾讯详解 QQ-AR 开放平台,让全社会参与进来
- php仿QQ验证码的实例分析