代码其实非常简单。只需要简单的配置就可以实现发送发邮件的功能。
1:添加依赖配置:
<!--发送邮件--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId> </dependency>
2:配置邮件服务器信息
spring:
mail:
host: smtp.qq.com
username: [email protected]
password: iipjqncncrgvcbcf
default-encoding: UTF-8
# port: 587
to: [email protected]
subject: 提醒邮件
mailTemplate: message.ftl
ccList: [email protected],[email protected]
# properties:
# mail:
# smtp:
# auth: true
# starttls:
# enable: true
# required: true
#socketFactory:
# port : 587
#class : javax.net.ssl.SSLSocketFactory
#fallback : false
需要注意的是:加井号的这些配置。在本地是不需要配置的。可以直接发送邮件。但是如果打包服务器。这个时候默认采用的是
25端口。而25这个端口一般是禁用的。所以会报一个错误:connent refuse port:xxxx.xxx.xx,25 refuse(-1).
默认有两个端口是开放的。一个465 一个587.但是阿里云我用的465一直就还是无法连接。telnet也可以通。然后改成587就可以了。其他服务器465端口应该是可以的。
发送邮件的代码就非常简单:
省略