1,导包:
<!-- mail发送 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
2,配置properties文件:
spring.mail.host=smtp.qq.com
spring.mail.username=******@qq.com
#密码指得是动态密码,qq邮箱的话在设置里打开,会有个动态smtp码.
spring.mail.password=******
spring.mail.protocol=smtps
spring.mail.test-connection=false
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true
spring.mail.properties.mail.smtp.starttls.required=true
# ssl 配置 (非ssl发送一般是25端口,linux服务器一般都是禁用的,所以要切换465)
spring.mail.port=465
spring.mail.default-encoding=UTF-8
spring.mail.properties.mail.smtp.ssl.enable=true
spring.mail.properties.mail.imap.ssl.socketFactory.fallback=false
spring.mail.properties.mail.smtp.ssl.socketFactory.class=com.fintech.modules.base.util.mail.MailSSLSocketFactory
3.构建个公共发送类
/**
* // 发送邮件是耗时任务,要异步发送邮箱
*/
@Override
@Async
public boolean sendMail(Mail mail) {
SimpleMailMessage message = new SimpleMailMessage();
message.setFrom(mail.getSender());
message.setTo(mail.getReceiver());
message.setSubject(mail.getSubject());
message.setText(mail.getText());
mailSender.send(message);
return true;
}
版权声明:本文为weixin_42533856原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。