SpringBoot 2.x 集成QQ邮箱、网易系邮箱、Gmail邮箱发送邮件

在Spring中提供了非常好用的 JavaMailSender接口实现邮件发送,在SpringBoot的Starter模块中也为此提供了自动化配置。

几个名词解释

什么是POP3、SMTP和IMAP?
详细介绍-请移步至网易帮助文档

IMAP和POP3有什么区别?

详细介绍-请移步至网易帮助文档

什么是免费邮箱客户端授权码功能?

详细介绍-请移步至网易帮助文档

Spring Boot中发送邮件步骤

Spring Boot中发送邮件具体的使用步骤如下

1、添加Starter模块依赖

2、添加Spring Boot配置(QQ/网易系/Gmail)

3、调用JavaMailSender接口发送邮件

添加Starter模块依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId> </dependency> 添加Spring Boot配置

application.yml中添加邮件相关的配置,这里分别罗列几个常用邮件的配置比如QQ邮箱、网易系邮箱、Gmail邮箱。

QQ邮箱配置

官方配置说明:参考官方帮助中心

获取客户端授权码:参考官方帮助中心

详细的配置如下:

spring: mail: 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.display.sendmail: Javen #可以任意 properties.mail.display.sendname: Spring Boot Guide Email #可以任意 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保持一致

说明:开启SSL时使用587端口时无法连接QQ邮件服务器

网易系(126/163/yeah)邮箱配置

网易邮箱客户端授码:参考官方帮助中心

客户端端口配置说明:

详细的配置如下:

spring: mail: 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.display.sendmail: Javen properties.mail.display.sendname: Spring Boot Guide Email 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

特别说明:

126邮箱SMTP服务器地址:smtp.126.com,端口号:465或者994

163邮箱SMTP服务器地址:smtp.163.com,端口号:465或者994

yeah邮箱SMTP服务器地址:smtp.yeah.net,端口号:465或者994

Gmail邮箱配置

Gmail 客户端设置说明:参考官方Gmail帮助

以上链接需要自行搭梯子,这里截几张图参考下

image


image


image

总结:
Gmail 发送邮件服务器为:smtp.gmail.com,端口号:465。客户端授权码为Gmail账号的密码,必须使用使用SSL。

还需要开启允许不够安全的应用 ,不然会出现Authentication failed的异常
选择登录与安全滑到底部有个允许不够安全的应用开启即可

详细的配置如下:

spring: mail: 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.display.sendmail: Javen properties.mail.display.sendname: Spring Boot Guide Email 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 调用JavaMailSender接口发送邮件

常用几种邮件形式接口的封装

import javax.mail.MessagingException; public interface IMailService { /** * 发送文本邮件 * @param to * @param subject * @param content */ public void sendSimpleMail(String to, String subject, String content); public void sendSimpleMail(String to, String subject, String content, String... cc); /** * 发送HTML邮件 * @param to * @param subject * @param content * @throws MessagingException */ public void sendHtmlMail(String to, String subject, String content) throws MessagingException; public void sendHtmlMail(String to, String subject, String content, String... cc); /** * 发送带附件的邮件 * @param to * @param subject * @param content * @param filePath * @throws MessagingException */ public void sendAttachmentsMail(String to, String subject, String content, String filePath) throws MessagingException; public void sendAttachmentsMail(String to, String subject, String content, String filePath, String... cc); /** * 发送正文中有静态资源的邮件 * @param to * @param subject * @param content * @param rscPath * @param rscId * @throws MessagingException */ public void sendResourceMail(String to, String subject, String content, String rscPath, String rscId) throws MessagingException; public void sendResourceMail(String to, String subject, String content, String rscPath, String rscId, String... cc); }

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wpfdww.html