Spring Boot中发送邮件步骤
Spring Boot中发送邮件具体的使用步骤如下
- 1、添加Starter模块依赖
- 2、添加Spring Boot配置(QQ/网易系/Gmail)
- 3、调用JavaMailSender接口发送邮件
添加Starter模块依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId> </dependency>
添加Spring Boot配置
在application.yml中添加邮件相关的配置,这里分别罗列几个常用邮件的配置比如QQ邮箱、网易系邮箱、Gmail邮箱。
QQ邮箱配置
详细的配置如下:
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.display.sendmail: Javen #可以任意 properties.mail.display.sendname: Spring Boot Guide Email #可以任意 from: xx@qq.com #与上面的username保持一致
说明:开启SSL时使用587端口时无法连接QQ邮件服务器
网易系(126/163/yeah)邮箱配置
详细的配置如下:
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 from: xx@126.com#与上面的username保持一致
特别说明:
- 126邮箱SMTP服务器地址:smtp.126.com,端口号:465或者994
- 163邮箱SMTP服务器地址:smtp.163.com,端口号:465或者994
- yeah邮箱SMTP服务器地址:smtp.yeah.net,端口号:465或者994
调用JavaMailSender接口发送邮件
常用几种邮件形式接口的封装
import javax.mail.MessagingException;
public interface IMailService {
/**
* 发送文本邮件
* @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;
/**
* 发送带附件的邮件
* @param to
* @param subject
* @param content
* @param filePath
* @throws MessagingException
*/
public void sendAttachmentsMail(String to, String subject, String content, String filePath) throws MessagingException;
再写一个组件实现上面的接口并注入JavaMailSender
@Component
public class IMailServiceImpl implements IMailService {
@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邮件
/**
* 发送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);
}
发送带附件的邮件
/**
* 发送带附件的邮件
* @param to
* @param subject
* @param content
* @param filePath
*/
public void sendAttachmentsMail(String to, String subject, String content, String filePath) 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);
FileSystemResource file = new FileSystemResource(new File(filePath));
String fileName = filePath.substring(filePath.lastIndexOf(File.separator));
helper.addAttachment(fileName, file);
mailSender.send(message);
}
发送正文中有静态资源的邮件
/**
* 发送正文中有静态资源的邮件
* @param to
* @param subject
* @param content
* @param rscPath
* @param rscId
*/
public void sendResourceMail(String to, String subject, String content, String rscPath, String rscId) 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);
FileSystemResource res = new FileSystemResource(new File(rscPath));
helper.addInline(rscId, res);
mailSender.send(message);
}
测试
package com.javen.controller;
import com.javen.email.impl.IMailServiceImpl;
import com.javen.vo.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;
@RestController
@RequestMapping("/email")
public class EmailController {
@Autowired
private IMailServiceImpl mailService;//注入发送邮件的各种实现方法
@Autowired
private TemplateEngine templateEngine;//注入模板引擎
/**
* 发送普通的邮件
*/
@RequestMapping
public JsonResult index(){
try {
mailService.sendSimpleMail("xxx@126.com","SpringBoot Email","这是一封普通的SpringBoot测试邮件");
}catch (Exception ex){
ex.printStackTrace();
return new JsonResult(-1,"邮件发送失败!!");
}
return new JsonResult();
}
/**
* 发送html的邮件
*/
@RequestMapping("/htmlEmail")
public JsonResult htmlEmail(){
try {
mailService.sendHtmlMail(""xxx@126.com","触手可及","<body style=\"text-align: center;margin-left: auto;margin-right: auto;\">\n"
+ " <div id=\"welcome\" style=\"text-align: center;position: absolute;\" >\n"
+" <h3>欢迎使用IJPay -By Javen</h3>\n"
+" <span>https://github.com/Javen205/IJPay</span>"
+ " <div\n"
+ " style=\"text-align: center; padding: 10px\"><a style=\"text-decoration: none;\" href=\"https://github.com/Javen205/IJPay\" target=\"_bank\" ><strong>IJPay 让支付触手可及,欢迎Start支持项目发展:)</strong></a></div>\n"
+ " <div\n" + " style=\"text-align: center; padding: 4px\">如果对你有帮助,请任意打赏</div>\n"
+ " <img width=\"180px\" height=\"180px\"\n"
+ " src=\"https://javen205.gitbooks.io/ijpay/content/assets/wxpay.png\">\n"
+ " </div>\n" + "</body>");
}catch (Exception ex){
ex.printStackTrace();
return new JsonResult(-1,"邮件发送失败!!");
}
return new JsonResult();
}
/**
* 发送附件的邮件
*/
@RequestMapping("/attachmentsMail")
public JsonResult attachmentsMail(){
try {
String filePath = "/Users/Javen/Desktop/IJPay.png";
mailService.sendAttachmentsMail("xxx@126.com", "这是一封带附件的邮件", "邮件中有附件,请注意查收!", filePath);
}catch (Exception ex){
ex.printStackTrace();
return new JsonResult(-1,"邮件发送失败!!");
}
return new JsonResult();
}
}
使用 Spring Boot 发送邮件到这里就介绍完了。个人能力有限如有错误欢迎指正。你有更好的解决方案或者建议欢迎一起交流讨论,如有疑问欢迎留言。

本文暂时没有评论,来添加一个吧(●'◡'●)