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的更多相关文章

  1. Spring Boot Mail 实现邮件发送

    此 demo 主要演示了 Spring Boot 如何整合邮件功能,包括发送简单文本邮件. 邮件服务在开发中非常常见,比如用邮件注册账号.邮件作为找回密码的途径.用于订阅内容定期邮件推送等等,下面就简 ...

  2. Spring Boot Mail通过QQ邮箱发送邮件

    本文将介绍如何在Spring Boot工程完成QQ邮箱配置,实现邮件发送功能. 一.在pom文件中添加依赖 <dependency> <groupId>org.springfr ...

  3. Spring Boot 揭秘与实战(七) 实用技术篇 - Java Mail 发送邮件

    文章目录 1. Spring Boot 集成 Java Mail 2. 单元测试 3. 源代码 Spring 对 Java Mail 有很好的支持.因此,Spring Boot 也提供了自动配置的支持 ...

  4. Spring Boot 2.0(六):使用 Docker 部署 Spring Boot 开源软件云收藏

    云收藏项目已经开源2年多了,作为当初刚开始学习 Spring Boot 的练手项目,使用了很多当时很新的技术,现在看来其实很多新技术是没有必要使用的,但做为学习案例来讲确实是一个绝佳的 Spring ...

  5. (转)Spring Boot 2 (六):使用 Docker 部署 Spring Boot 开源软件云收藏

    http://www.ityouknow.com/springboot/2018/04/02/docker-favorites.html 云收藏项目已经开源2年多了,作为当初刚开始学习 Spring ...

  6. Spring Boot 2 (六):使用 Docker 部署 Spring Boot 开源软件云收藏

    Spring Boot 2 (六):使用 Docker 部署 Spring Boot 开源软件云收藏 云收藏项目已经开源3年多了,作为当初刚开始学习 Spring Boot 的练手项目,使用了很多当时 ...

  7. Spring Boot 邮件配置

    ######################################################## spring boot mail tls ###################### ...

  8. 值得使用的Spring Boot

    2013年12月12日,Spring发布了4.0版本.这个本来只是作为Java平台上的控制反转容器的库,经过将近10年的发展已经成为了一个巨无霸产品.不过其依靠良好的分层设计,每个功能模块都能保持较好 ...

  9. 一键式Spring集成工具 Spring Boot

    最近公司使用Spring boot进行开发,稍微了解一下,不过自我感觉把集中式配置applicate.properties搞明白,注解用过Spring MVC的boot绝对没问题的 比如拦截器:@As ...

  10. Spring Boot Admin 的使用 2

    http://blog.csdn.net/kinginblue/article/details/52132113 ******************************************* ...

随机推荐

  1. codeforces 877b

    B. Nikita and string time limit per test 2 seconds memory limit per test 256 megabytes input standar ...

  2. hdu 3915 高斯消元

    http://acm.hdu.edu.cn/showproblem.php?pid=3915 这道题目是和博弈论挂钩的高斯消元.本题涉及的博弈是nim博弈,结论是:当先手处于奇异局势时(几堆石子数相互 ...

  3. python之随机验证码

    一.内置函数 chr ------把数字转换为字母 ord-------把字母转换为数字 n = chr(65) print (n) m = ord("A") print(m) 二 ...

  4. 2-Sat小结

    关于2-sat,其实就是一些对于每个问题只有两种解,一般会给出问题间的关系,比如and,or,not等关系,判定是否存在解的问题.. 具体看http://blog.csdn.net/jarjingx/ ...

  5. acdream 20140730 D题

    今天见识到了“数学上来先打表”............ #include<iostream> using namespace std; #include<iomanip> #d ...

  6. 转:iOS9的新特性以及适配方案

    2015年9月8日,苹果宣布iOS 9操作系统的正式版在太平洋时间9月16日正式推出,北京时间9月17日凌晨1点推送. 新的iOS 9系统比iOS8更稳定,功能更全面,而且还更加开放.iOS 9加入了 ...

  7. [ASE][Daily Scrum]11.12

    这几天Jiafan同学回学校去了,服务器的问题暂时未解决.继续搭建服务器中: View Shilin Liu 修复tank的错位问题 产生残缺地图         Client Jiafan Zhu( ...

  8. 【DirectX】 AudioVideoPlayback 中的事件BUG

    当访问 Video 中的 Audio 属性时,会造成 Video 的所有事件失效.经过反汇查看源码,原来在访问Audio属性时,Audio会通过当前Video对象创建一个新实例.而这个新实例会覆盖掉当 ...

  9. Bs4 BeautifulSoup取值

    原文网址:https://blog.csdn.net/u010244522/article/details/79627073 从网页获取HTML数据后,获取对应标签.属性的值 取值方法主要有以下几种: ...

  10. [Swift]优先队列PriorityQueue(自定义数据结构)

    优先队列[priority queue] 普通的队列是一种先进先出的数据结构,元素在队列尾追加,而从队列头删除. 优先队列特点:在优先队列中,元素被赋予优先级. 当访问元素时,具有最高优先级的元素最先 ...