spring-boot-mail
1. 功能
- 发送普通邮件
- 发送htm邮件
- 发送带附件的邮件
- 发送带静态资源的邮件
2. 实现
类结构图

3. 实现
接口
package com.jihite.service;
public interface MailService {
void sendSimpleMail(String to, String subject, String content);
void sendHtmlMail(String to, String subject, String content);
void sendAttachMail(String to, String subject, String content, String filePath);
void sendInlineResourceMail(String to, String subject, String content, String rscPath, String rscId);
}
实现
package com.jihite.service.impl; import com.jihite.service.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; @Component
public class MailServiceImpl implements MailService{
private final Logger logger = LoggerFactory.getLogger(this.getClass());
@Autowired
private JavaMailSender mailSender; @Value("${mail.fromMail.addr}")
private String from; @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.info("简易邮件发送失败", e);
} } @Override
public void sendHtmlMail(String to, String subject, String content) {
MimeMessage message = mailSender.createMimeMessage();
try {
MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(message, true);
mimeMessageHelper.setFrom(from);
mimeMessageHelper.setTo(to);
mimeMessageHelper.setSubject(subject);
mimeMessageHelper.setText(content, true);
mailSender.send(message);
} catch (MessagingException e) {
logger.error("发送html邮件时发生异常", e); }
} @Override
public void sendAttachMail(String to, String subject, String content, String filePath) {
MimeMessage mimeMessage = mailSender.createMimeMessage();
try {
MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage, true);
mimeMessageHelper.setFrom(from);
mimeMessageHelper.setTo(to);
mimeMessageHelper.setSubject(subject);
mimeMessageHelper.setText(content, true); FileSystemResource fileSystemResource = new FileSystemResource(new File(filePath));
String fileName = "附件";
mimeMessageHelper.addAttachment(fileName, fileSystemResource); mailSender.send(mimeMessage);
logger.info("带附件的邮件发送成功!");
} catch (MessagingException e) {
logger.error("发送带附件失败", e);
}
} @Override
public void sendInlineResourceMail(String to, String subject, String content, String rscPath, String rscId) {
MimeMessage mimeMessage = mailSender.createMimeMessage();
try {
MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, 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(mimeMessage);
logger.info("发送嵌入静态资源的邮件成功"); } catch (MessagingException e) {
logger.error("发送嵌入静态资源的邮件失败", e);
} }
}
resources/application.properties
spring.application.name=spirng-boot-mail spring.mail.host=smtp.163.com
spring.mail.username=xxoo@xxoo.com
spring.mail.password=xxoo
spring.mail.default-encoding=UTF-8 mail.fromMail.addr=xxoo@xxoo.com
测试
package com.jihite.service; import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner; @RunWith(SpringRunner.class)
@SpringBootTest
public class MailServiceTest { @Autowired
private MailService mailService; private String to = "jihite@126.com"; @Test
public void testSimpleMail() {
String subject = "测试普通邮件";
String content = "Hello, This is a simple mail";
mailService.sendSimpleMail(to, subject, content);
} @Test
public void testHtmlMail() {
String subject = "测试待Html邮件";
String content="<html>\n" +
"<body>\n" +
" <h3>hello world ! 这是一封html邮件!</h3>\n" +
"</body>\n" +
"</html>";
mailService.sendHtmlMail(to, subject, content);
} @Test
public void testAttachMail() {
String subject = "测试带附件邮件";
String content = "请注意查收附件哦";
String filePath = "/Users/jihite/Del/testMail";
mailService.sendAttachMail(to, subject, content, filePath);
} @Test
public void testInlineResourceMail() {
String subject = "带图片的邮件";
String rscId = "neo006";
String content="<html><body>这是有图片的邮件:<img src=\'cid:" + rscId + "\' ></body></html>";
String imgPath = "/Users/jihite/Downloads/abc.png";
mailService.sendInlineResourceMail(to, subject, content, imgPath, rscId);
} }
4. 代码
spring-boot-mail的更多相关文章
- Spring Boot Mail 实现邮件发送
此 demo 主要演示了 Spring Boot 如何整合邮件功能,包括发送简单文本邮件. 邮件服务在开发中非常常见,比如用邮件注册账号.邮件作为找回密码的途径.用于订阅内容定期邮件推送等等,下面就简 ...
- Spring Boot Mail通过QQ邮箱发送邮件
本文将介绍如何在Spring Boot工程完成QQ邮箱配置,实现邮件发送功能. 一.在pom文件中添加依赖 <dependency> <groupId>org.springfr ...
- Spring Boot 揭秘与实战(七) 实用技术篇 - Java Mail 发送邮件
文章目录 1. Spring Boot 集成 Java Mail 2. 单元测试 3. 源代码 Spring 对 Java Mail 有很好的支持.因此,Spring Boot 也提供了自动配置的支持 ...
- Spring Boot 2.0(六):使用 Docker 部署 Spring Boot 开源软件云收藏
云收藏项目已经开源2年多了,作为当初刚开始学习 Spring Boot 的练手项目,使用了很多当时很新的技术,现在看来其实很多新技术是没有必要使用的,但做为学习案例来讲确实是一个绝佳的 Spring ...
- (转)Spring Boot 2 (六):使用 Docker 部署 Spring Boot 开源软件云收藏
http://www.ityouknow.com/springboot/2018/04/02/docker-favorites.html 云收藏项目已经开源2年多了,作为当初刚开始学习 Spring ...
- Spring Boot 2 (六):使用 Docker 部署 Spring Boot 开源软件云收藏
Spring Boot 2 (六):使用 Docker 部署 Spring Boot 开源软件云收藏 云收藏项目已经开源3年多了,作为当初刚开始学习 Spring Boot 的练手项目,使用了很多当时 ...
- Spring Boot 邮件配置
######################################################## spring boot mail tls ###################### ...
- 值得使用的Spring Boot
2013年12月12日,Spring发布了4.0版本.这个本来只是作为Java平台上的控制反转容器的库,经过将近10年的发展已经成为了一个巨无霸产品.不过其依靠良好的分层设计,每个功能模块都能保持较好 ...
- 一键式Spring集成工具 Spring Boot
最近公司使用Spring boot进行开发,稍微了解一下,不过自我感觉把集中式配置applicate.properties搞明白,注解用过Spring MVC的boot绝对没问题的 比如拦截器:@As ...
- Spring Boot Admin 的使用 2
http://blog.csdn.net/kinginblue/article/details/52132113 ******************************************* ...
随机推荐
- jvm虚拟机--堆内存
reserved 保留区域 堆 所有对象实例都在这里分配内存. 是垃圾收集的主要区域("GC 堆").现代的垃圾收集器基本都是采用分代收集算法,主要思想是针对不同的对象采取不同的垃 ...
- hive 桶表
转自:https://blog.csdn.net/csdnliuxin123524/article/details/81052974 桶表(bucket table): 原理: 分区表是按照经常查询的 ...
- 保存到Excel文件中
OLEObject ole_object , ole_workbooks ole_object = CREATE OLEObjectIF ole_object.ConnectToNewObject(& ...
- Opencv打开摄像头,读不到图像,一般来说先读取第一帧,舍弃,然后就正常了
舍弃第一帧的程序: cap >> img; cv::waitKey(100); if (cvWaitKey(5) == 27) break; cap >> img;
- 7.Git与项目
Git简介 Git是目前世界上最先进的分布式版本控制系统 安装 sudo apt-get install git 安装成功后,运行如下命令 git 产生 Linus在1991年创建了开源的Linux, ...
- delphi 小数点四舍五入问题
function ARoundN(v: Double; n: Integer): Double; var I:Integer; begin result:=v; do begin result:=re ...
- Android-WebView与本地HTML (Java调用--->HTML的方法)
上一篇博客 Android-WebView与本地HTML (HTML调用-->Java的方法) 介绍了 JavaScript 调用--> Java中的方法,而此篇博客是介绍 Java 调用 ...
- Windows的cmd窗口显示utf8字符
用XeLaTeX的时候,查字体需要用fc-list命令,XeLaTeX用的都是utf编码,所以fc-list输出的字体信息也是utf编码.因此需要把cmd窗口也改成utf8编码才能看到这些字体信息.U ...
- 手机app有了短信验证码还有没必要有图片验证码?
当然有必要,这里我们来聊一个恶意短信验证的案例,通过这个案例我们就能更好理解短信验证码和图片验证码这两者的关系了. 讨论防止恶意短信验证之前,我们先来看看什么是恶意短信验证及出现的原因. 恶意短信验证 ...
- [译] 玩转ptrace (一)
[本文翻译自这里: http://www.linuxjournal.com/article/6100?page=0,0,作者:Pradeep Padaia] 你是否曾经想过怎样才能拦截系统调用?你是否 ...