Spring Boot中发送邮件步骤
Spring Boot中发送邮件具体的使用步骤如下
1、添加Starter模块依赖 2、添加Spring Boot配置(QQ/网易系/Gmail) 3、调用JavaMailSender接口发送邮件
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
在application.yml中添加邮件相关的配置,这里分别罗列几个常用邮件的配置比如QQ邮箱、网易系邮箱、Gmail邮箱。
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.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邮件服务器
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.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 发送邮件服务器为: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.smtp.starttls.enable: true
properties.mail.smtp.starttls.required: true
properties.mail.smtp.ssl.enable: true
from: xxx@gmail.com
default-encoding: utf-8
package cn.qiucode.service;
import javax.mail.MessagingException;
/**
* Created by wuming on 2019/6/1.
*/
public interface MailService {
/**
* 发送文本邮件
* @param to
* @param subject
* @param content
*/
public void sendSimpleMail(String to, String subject, String content);
/**
* 发送HTML邮件
* @param to
* @param subject
* @param content
* @throws MessagingException
*/
public void sendHtmlMail(String to, String subject, String content) throws MessagingException;
}
实现类:
package cn.qiucode.service.impl;
import cn.qiucode.service.MailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;
/**
* Created by wuming on 2019/6/1.
*/
@Service("mailService")
public class MailServiceImpl implements MailService {
@Autowired
private JavaMailSender mailSender;
@Value("${spring.mail.from}")
private String from;
/**
* 发送文本邮件
* @param to
* @param subject
* @param content
*/
@Override
public void sendSimpleMail(String to, String subject, String content) {
SimpleMailMessage message = new SimpleMailMessage();
message.setFrom(from);
message.setTo(to);
message.setSubject(subject);
message.setText(content);
mailSender.send(message);
}
/**
* 发送HTML邮件
* @param to
* @param subject
* @param content
*/
@Override
public void sendHtmlMail(String to, String subject, String content) throws MessagingException {
MimeMessage message = mailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(message, true);
helper.setFrom(from);
helper.setTo(to);
helper.setSubject(subject);
helper.setText(content, true);
mailSender.send(message);
}
}
@Autowired
private MailService mailService;
//在添加评论/留言的方法里调用service
//发送邮箱通知
asyncSendMail(messageParam.getArticleId(),messageParam.getContent());
//异步处理线程
public void asyncSendMail(final Integer articleId,final String content){
ThreadUtil.execute(new Runnable() {
@Override
public void run() {
try{
String subject=null==articleId ? "有新游客留言了!" : "有游客评论了ID为【"+articleId+"】这篇文章";
String msgTitle=null==articleId ? "留言内容如下:" : "评论内容如下:";
mailService.sendHtmlMail("2834491983@qq.com",subject,"<body style=\"text-align: center;margin-left: auto;margin-right: auto;\">\n"
+ " <div id=\"welcome\" style=\"text-align: center;position: absolute;\" >\n"
+" <h3>"+msgTitle+"</h3>\n"
+" <span>"+content+"</span>"
+ " </div>\n" + "</body>");
}catch(Exception e){
e.printStackTrace();
}
}
});
}
最终效果如下: