公告:欢迎访问,查看更多资源请点我^.^!            点我关闭广告
SpringBoot_I~电子邮件
2018-06-01 09:47:07
800人阅读
评论(1)
分类:Java

一 引入依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency> 二 application.properties配置
spring.mail.host=smtp.163.com
spring.mail.username=xxx@163.com
#一般密码为授权码
spring.mail.password=
spring.mail.default-encoding=UTF-8

mail.fromMail.addr=xxx@163.com 三 编写发送邮件(忽略MailService接口)
package com.study.springboot.SpringBoot_A.mail.impl;
import com.study.springboot.SpringBoot_A.mail.MailService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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.Component;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;

/**
* @author 赵超
* @Description
* @create 2018-03-29 14:43
**/
@Component
public class MailServiceImpl implements MailService {
private final Logger logger = LoggerFactory.getLogger(this.getClass());
@Autowired
private JavaMailSender javaMailSender;
@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 {
javaMailSender.send(message);
logger.info("success");
} catch (Exception e) {
logger.error("failed", e);
}
}
/**
* 发送html邮件
*
* @param tos
* @param subject
* @param content
*/
@Override
public void sendHtmlMail(String[] tos, String subject, String content) {
MimeMessage message = javaMailSender.createMimeMessage(); try {
MimeMessageHelper helper = new MimeMessageHelper(message, true);
helper.setFrom(from);
helper.setTo(tos);
helper.setSubject(subject);
helper.setText(content, true);
javaMailSender.send(message);
logger.info("html success");
} catch (MessagingException e) {
logger.error("发送html邮件时发生异常!", e);
}
}
/**
* 发送带有附件的邮件
*
* @param tos
* @param subject
* @param content
* @param failPath 批量时候可优化为List<String>
*/
@Override
public void sendAttachmentsMail(String[] tos, String subject, String content, String failPath) {
MimeMessage message = javaMailSender.createMimeMessage(); try {
MimeMessageHelper helper = new MimeMessageHelper(message, true);
helper.setFrom(from);
helper.setTo(tos);
helper.setSubject(subject);
helper.setText(content, true);
FileSystemResource file = new FileSystemResource(new File(failPath));
String fileName = failPath.substring(failPath.lastIndexOf(File.separator));
helper.addAttachment(fileName, file);
javaMailSender.send(message);
logger.info("attachment success");
} catch (MessagingException e) {
logger.error("发送带附件的邮件时发生异常!", e);
}
}
/**
* 发送含有静态资源的文件,譬如html内容中含有本地图片等
*
* @param tos
* @param subject
* @param content
* @param rscPath 批量时候可优化为List<Map<K, V>>的形式[{rscId, rscPath}, {rscId, rscPath}, ...]
* @param rscId
*/
@Override
public void sendInlineResourceMail(String[] tos, String subject, String content, String rscPath, String rscId) {
MimeMessage message = javaMailSender.createMimeMessage();
try {
MimeMessageHelper helper = new MimeMessageHelper(message, true);
helper.setFrom(from);
helper.setTo(tos);
helper.setSubject(subject);
helper.setText(content, true);

FileSystemResource res = new FileSystemResource(new File(rscPath)); //添加多个条件可以使用多条helper.addInline(fileName, file); helper.addInline(rscId, res);

javaMailSender.send(message);
logger.info("嵌入静态资源的邮件已经发送。");
} catch (MessagingException e) {
logger.error("发送嵌入静态资源的邮件时发生异常!", e);
}
}
} 四 测试
package com.study.springboot.SpringBoot_A.mail;
import com.study.springboot.SpringBoot_A.SpringBootAApplication;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/**
* @author 赵超
* @Description
* @create 2018-03-29 14:51
**/
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(SpringBootAApplication.class)
public class MailTest {
@Autowired
private MailService mailService;
@Test
public void send() {
String to = "773134958@qq.com";
String subject = "主题";
String content = "我是邮件内容!!";
mailService.sendSimpleMail(to, subject, content);
}
@Test
public void testHtmlMail() throws Exception {
String content="<html>\n" +
"<body>\n" +
" <h3>hello world ! 这是一封Html邮件!</h3>\n" +
"</body>\n" +
"</html>";
String[] tos = {"773134958@qq.com"};
mailService.sendHtmlMail(tos,"html邮件主题", content);
}
@Test
public void testAttachmentMail() throws Exception {
String content=" pdf ";
String[] tos = {"773134958@qq.com"};
String filePath = "C:\\Users\\emiancang\\Desktop\\app-api.xlsx";
mailService.sendAttachmentsMail(tos,"附件邮件主题", content, filePath);
}
@Test
public void testInlineResourceMail() throws Exception {
String rscId = "neo006";
String content="<html>\n" +
" <body>\n" +
" <h3>hello world ! 这是一封静态资源邮件!</h3>\n" +
" <img src=\'cid:" + rscId + "\'/>\n" +
" </body>\n" +
"</html>";
String path = "C:\\Users\\emiancang\\Desktop\\travel.png";
String[] tos = {"773134958@qq.com"};
mailService.sendInlineResourceMail(tos,"静态邮件主题", content, path, rscId);
}
/**
* 测试html模版解析发送邮件
*/
@Autowired
TemplateEngine templateEngine;
@Test
public void testTemplateMail() throws Exception {
Context context = new Context();//org.thymeleaf.context.Context
context.setVariable("username", "赵超");
context.setVariable("id", "105"); //templateEngine.process()方法第一个参数为templates文件夹中模版的名称,本例中模版页面为mail.html
String emailContent = templateEngine.process("mail", context);
String[] tos = {"773134958@qq.com"};
mailService.sendHtmlMail(tos, "Html模版邮件", emailContent);
}
} 五 html模版
<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8"/>
<title>Title</title>
</head>
<body>
尊敬的用户<span th:text="${username}">用户名</span>,
您好,这是验证邮件,请完成,
<a href="#" th:href="@{'https://www.zhaochao.top/iBlog/article.html?id=' + ${id}}">
呵呵哒
</a> </body> </html>

分享一下:
赞一下(1)
博主资料
博主头像
zc521106
文章:34
浏览:19899
文章分类
Java(12)
NoSql(112)
数据库(12)
前端(12)
阅读排行
Java基础知识
(12)
NoSql应用
(112)
数据库Oracle语法
(12)
前端常用工具类
(12)
java web项目
(12)
linux安装mysql
(12)
评论区
这篇文章怎么样?写点评论吧!
姓名:
邮箱:
有回复时通知我:
发表
回复【10.20.30.32】:
ssl加密方式发送邮件请给我留言获取哦!
2018-06-08 18:02:25
* 以上用户言论只代表其个人观点,不代表iBlog网站的观点或立场,如有任何疑问请随时联系管理员...