Spring Boot 发送邮件
Spring Boot 发送邮件
配置
- pom
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
- application
spring.mail.host=smtp.163.com #发送邮件的协议(此处是SMTP协议,163邮箱)
spring.mail.username=xxoo@xxoo.com #用户名
spring.mail.password=xxoo #密码(移动端,客户授权码,不是页面登陆码)
spring.mail.default-encoding=UTF-8 #编码
mail.fromMail.addr=xxoo@xxoo.com #发送人
1.简单文本邮件
步骤
- 引入相关Jar包
- 配置邮箱参数
- 封装SimpleMailMessage
- JavaMailSender进行发送
@Autowired
private JavaMailSender mailSender;
@Value("${mail.fromMail.addr}")
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);//内容
try {
mailSender.send(message);//发送邮件
logger.info("简单邮件已经发送。");
} catch (Exception e) {
logger.error("发送简单邮件时发生异常!", e);
}
}
- 测试
@Test
public void testSimpleMail() throws Exception {
mailService.sendSimpleMail("ityouknow@126.com","test simple mail"," hello this is simple mail");
}
2.HTML邮件
/**
* 发送html邮件
* @param to
* @param subject
* @param content
*/
@Override
public void sendHtmlMail(String to, String subject, String content) {
MimeMessage message = mailSender.createMimeMessage();
try {
//true表示需要创建一个multipart message
MimeMessageHelper helper = new MimeMessageHelper(message, true);
helper.setFrom(from);
helper.setTo(to);
helper.setSubject(subject);
helper.setText(content, true);
mailSender.send(message);
logger.info("html邮件发送成功");
} catch (MessagingException e) {
logger.error("发送html邮件时发生异常!", e);
}
}
- 测试
@Test
public void testHtmlMail() throws Exception {
String content="<html>\n" +
"<body>\n" +
" <h3>hello world ! 这是一封html邮件!</h3>\n" +
"</body>\n" +
"</html>";
mailService.sendHtmlMail("ityouknow@126.com","test simple mail",content);
}
3.附件邮件
/**
* 发送带附件的邮件
* @param to
* @param subject
* @param content
* @param filePath
*/
public void sendAttachmentsMail(String to, String subject, String content, String filePath){
MimeMessage message = mailSender.createMimeMessage();
try {
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);//发送一个附件addAttachment
//helper.addAttachment("test"+fileName, file);//发送多个附件设置一样
mailSender.send(message);
logger.info("带附件的邮件已经发送。");
} catch (MessagingException e) {
logger.error("发送带附件的邮件时发生异常!", e);
}
}
- 测试
@Test
public void sendAttachmentsMail() {
String filePath="e:\\tmp\\application.log";
mailService.sendAttachmentsMail("ityouknow@126.com", "主题:带附件的邮件", "有附件,请查收!", filePath);
}
4-图片邮件
/**
* 发送正文中有静态资源(图片)的邮件
* @param to
* @param subject
* @param content
* @param rscPath
* @param rscId
*/
public void sendInlineResourceMail(String to, String subject, String content, String rscPath, String rscId){
MimeMessage message = mailSender.createMimeMessage();
try {
MimeMessageHelper helper = new MimeMessageHelper(message, true);
helper.setFrom(from);
helper.setTo(to);
helper.setSubject(subject);
helper.setText(content, true);
//设置图片addInline
FileSystemResource res = new FileSystemResource(new File(rscPath));
helper.addInline(rscId, res);
mailSender.send(message);
logger.info("嵌入静态资源的邮件已经发送。");
} catch (MessagingException e) {
logger.error("发送嵌入静态资源的邮件时发生异常!", e);
}
}
- 测试
@Test
public void sendInlineResourceMail() {
String rscId = "neo006";//图片名
String content="<html><body>这是有图片的邮件:<img src=\'cid:" + rscId + "\' ></body></html>";//调用显示图片
String imgPath = "C:\\Users\\summer\\Pictures\\favicon.png";//图片地址
mailService.sendInlineResourceMail("ityouknow@126.com", "主题:这是有图片的邮件", content, imgPath, rscId);
}
5.模板邮件
使用模板引擎,此处使用的是thymeleaf
<!-- 模板引擎 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
- 模板配置
在resources/templates中创建页面emailTemplate.html
<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org"><!--模板声明-->
<head>
<meta charset="UTF-8"/>
<title>Title</title>
</head>
<body>
您好,这是验证邮件,请点击下面的链接完成验证,<br/>
<a href="#" th:href="@{ http://www.ityouknow.com/neo/{id}(id=${id}) }">激活账号</a><!--配置接收-->
</body>
</html>
- 测试
@Autowired
private TemplateEngine templateEngine;
@Test
public void sendTemplateMail() {
//创建邮件正文
Context context = new Context();
context.setVariable("id", "006");
String emailContent = templateEngine.process("emailTemplate", context);
mailService.sendHtmlMail("ityouknow@126.com","主题:这是模板邮件",emailContent);//sendHtmlMail走的是HTML方法
}
6.常见错误
- 421HL:ICC该IP同时并发连接数过大
- 451 Requested mail action not taken:too much fail...登录失败次数过多,被临时禁止登录。
- 553 authentication is required 认证失败
参考地址:http://help.163.com/09/1224/17/5RAJ4LMH00753VB8.html