java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > Spring Boot发送邮件

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

作者:bug 搬运工

SpringBoot发送邮件的详细步骤包括添加Starter依赖、配置邮件服务器(QQ/网易/yeah邮箱)和调用JavaMailSender接口,本文详细介绍了如何在SpringBoot项目中集成邮件发送功能,并提供了具体的配置示例和代码实现,感兴趣的朋友一起看看吧

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

  1. 添加 Starter 模块依赖
  2. 添加 Spring Boot 配置(QQ/网易系/Gmail)
  3. 调用 JavaMailSender 接口发送邮件

开始编码

创建 Spring Boot 项目,添加依赖。

项目结构

1. 添加依赖

在 Maven pom.xml 配置文件中加入 spring-boot-starter-mail 依赖。

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-mail</artifactId>
</dependency>

2. 添加配置参数

然后在 application.yml 文件中加入以下配置。

application.yml 配置

QQ 邮箱配置

spring:
  mail:
    host: smtp.qq.com # 发送邮件服务器
    username: 1016767658@qq.com # 发送邮件的邮箱地址
    password: ivhkrc*****kbdcf # 客户端授权码,不是邮箱密码,这个在 QQ 邮箱设置里面自动生成的
    properties.mail.smtp.port: 465 # 端口号 465 或 587
    from: 1016767658@qq.com # 发送邮件的地址,和上面 username 一致
    properties.mail.smtp.starttls.enable: true
    properties.mail.smtp.starttls.required: true
    properties.mail.smtp.ssl.enable: true
    default-encoding: utf-8

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

spring:
  mail:
    host: smtp.126.com # 发送邮件服务器
    username: xx@126.com # 发送邮件的邮箱地址
    password: xxxxxxx # 客户端授权码,不是邮箱密码,网易的是自己设置的
    properties.mail.smtp.port: 994 # 465 或者 994
    from: xxx@126.com # 发送邮件的地址,和上面 username 一致
    properties.mail.smtp.starttls.enable: true
    properties.mail.smtp.starttls.required: true
    properties.mail.smtp.ssl.enable: true
    default-encoding: utf-8

注意:

封装邮件接口,方便调用发送邮件

IMailService 接口

package com.jiangfeixiang.sendemail;
/**
 * @Author: 姜飞祥
 * @Description: 封装一个发邮件的接口,后边直接调用即可
 * @Date: Create in 2019/1/28/0028 21:57
 */
public interface IMailService {
    /**
     * 发送文本邮件
     * @param to 收件人
     * @param subject 主题
     * @param content 内容
     */
    void sendSimpleMail(String to, String subject, String content);
    /**
     * 发送 HTML 邮件
     * @param to 收件人
     * @param subject 主题
     * @param content 内容
     */
    public void 

到此这篇关于Spring Boot中发送邮件的具体使用步骤的文章就介绍到这了,更多相关Spring Boot发送邮件内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文