Java发送邮件 SpringBoot
配置和目录结构
配置预览
spring.application.name
:设置应用程序的名称为"demo-mail"。spring.mail.host
:设置SMTP服务器的地址为"smtp.qq.com",这是QQ邮箱的SMTP服务器地址。spring.mail.port
:设置SMTP服务器的端口号为465,这是QQ邮箱的SMTP服务器端口号。spring.mail.username
:设置发送邮件的邮箱地址为"3324855376@qq.com",这是发送邮件的QQ邮箱地址。spring.mail.password
:设置发送邮件的邮箱密码为"axyqbvfuxkdqdaai",这是发送邮件的QQ邮箱密码。注意,如果密码是纯数字,需要将其用引号括起来。spring.mail.default-encoding
:设置邮件的默认编码格式为UTF-8。spring.mail.properties.mail.debug
:设置是否开启debug模式发送邮件,这里设置为true,表示开启debug模式。spring.mail.properties.smtp.connectionTimeout
:设置连接延迟时间为5000毫秒。spring.mail.properties.smtp.timeout
:设置延迟时间为5000毫秒。spring.mail.properties.smtp.writeTimeout
:设置写入邮箱延迟时间为5000毫秒。spring.mail.properties.smtp.allow8BitMime
:设置是否允许8位编码的MIME。spring.mail.properties.smtp.sendPartial
:设置是否发送部分内容。spring.mail.properties.smtp.ssl.enabled
:设置是否开启SSL连接,这里设置为true,表示开启SSL连接。spring.mail.properties.smtp.socketFactory.class
:设置SSL连接时的SocketFactory类为"javax.net.ssl.SSLSocketFactory"。
spring:
application:
name: demo-mail
mail:
host: smtp.qq.com # 邮箱地址
port: 465 # 邮箱端口号
username: 3324855376@qq.com # 设置发送邮箱
password: axyqbvfuxkdqdaai # 如果是纯数字要加引号
default-encoding: UTF-8 # 设置编码格式
properties:
mail:
debug: true # 是否开启debug模式发送邮件
smtp:
connectionTimeout: 5000 # 设置连接延迟
timeout: 5000 # 延迟时间
writeTimeout: 5000 # 写入邮箱延迟
allow8BitMime: true
sendPartial: true
ssl:
enabled: true # 是否开启SSL连接
socketFactory:
class: javax.net.ssl.SSLSocketFactory # 必要设置!!!
可以设置发送邮件debug模式这样在发送时可以在控制台中输出相关信息。
目录结构
需要导入的包
SpringBoot版本为3.2.3
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.2.3</version>
<relativePath/>
</parent>
邮件配置说明
<dependencies>
<!-- 邮箱 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
<!-- knife4j -->
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>knife4j-openapi3-jakarta-spring-boot-starter</artifactId>
<version>4.4.0</version>
</dependency>
<