项目GitHub地址 :

https://github.com/FrameReserve/TrainingBoot

Spring Boot(十二)集成spring-boot-starter-mail发送邮件,标记地址:

https://github.com/FrameReserve/TrainingBoot/releases/tag/0.0.12

pom.xml

增加:

spring-boot-starter-mail

spring-boot-starter-velocity -- 模板

<!-- Email --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-mail</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-velocity</artifactId></dependency>

Spring Boot配置文件:

QQ邮箱权限需要在QQ邮箱里设置,163则不用,问题比较少。

src/main/resources/application.yml

    mail:host: smtp.163.comusername: 286352250@163.compassword: 用户密码properties:mail: smtp:auth: truetimeout: 25000

Email配置类:

后期经常使用发件人邮箱,在这统一定义。

src/main/java/com/training/core/email/EmailConfig.java

package com.training.core.email;import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;@Component
public class EmailConfig {/*** 发件邮箱*/@Value("${spring.mail.username}")private String emailFrom;public String getEmailFrom() {return emailFrom;}public void setEmailFrom(String emailFrom) {this.emailFrom = emailFrom;}}

定义Email Service,三种邮件风格:

1. 发送简单邮件。

2. 发送带附件简单邮件

3. 发送模板邮件。

src/main/java/com/training/core/email/EmailService.java

package com.training.core.email;import java.io.File;
import java.util.List;
import java.util.Map;import com.training.core.dto.Pair;public interface EmailService {/*** 发送简单邮件* @param sendTo 收件人地址* @param titel  邮件标题* @param content 邮件内容*/public void sendSimpleMail(String sendTo, String titel, String content);/*** 发送简单邮件* @param sendTo 收件人地址* @param titel  邮件标题* @param content 邮件内容* @param attachments<文件名,附件> 附件列表*/public void sendAttachmentsMail(String sendTo, String titel, String content, List<Pair<String, File>> attachments);/*** 发送模板邮件* @param sendTo 收件人地址* @param titel  邮件标题* @param content<key, 内容> 邮件内容* @param attachments<文件名,附件> 附件列表*/public void sendTemplateMail(String sendTo, String titel, Map<String, Object> content, List<Pair<String, File>> attachments);}

Email Service实现类:

src/main/java/com/training/core/email/EmailServiceImpl.java

package com.training.core.email;import java.io.File;
import java.util.List;
import java.util.Map;import javax.mail.internet.MimeMessage;import org.apache.commons.collections.map.HashedMap;
import org.apache.velocity.app.VelocityEngine;
import org.springframework.beans.factory.annotation.Autowired;
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.Service;
import org.springframework.ui.velocity.VelocityEngineUtils;import com.training.core.dto.Pair;
import com.training.core.exception.RuntimeServiceException;@Service
public class EmailServiceImpl implements EmailService {@Autowiredprivate EmailConfig emailConfig;@Autowiredprivate JavaMailSender mailSender;@Autowiredprivate VelocityEngine velocityEngine;public void sendSimpleMail(String sendTo, String titel, String content) {SimpleMailMessage message = new SimpleMailMessage();message.setFrom(emailConfig.getEmailFrom());message.setTo(sendTo);message.setSubject(titel);message.setText(content);mailSender.send(message);}public void sendAttachmentsMail(String sendTo, String titel, String content, List<Pair<String, File>> attachments) {MimeMessage mimeMessage = mailSender.createMimeMessage();try {MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);helper.setFrom(emailConfig.getEmailFrom());helper.setTo(sendTo);helper.setSubject(titel);helper.setText(content);for (Pair<String, File> pair : attachments) {helper.addAttachment(pair.getLeft(), new FileSystemResource(pair.getRight()));}} catch (Exception e) {throw new RuntimeServiceException(e);}mailSender.send(mimeMessage);}public void sendInlineMail() {MimeMessage mimeMessage = mailSender.createMimeMessage();try {MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);helper.setFrom(emailConfig.getEmailFrom());helper.setTo("286352250@163.com");helper.setSubject("主题:嵌入静态资源");helper.setText("<html><body><img src=\"cid:weixin\" ></body></html>", true);FileSystemResource file = new FileSystemResource(new File("weixin.jpg"));helper.addInline("weixin", file);} catch (Exception e) {throw new RuntimeServiceException(e);}mailSender.send(mimeMessage);}public void sendTemplateMail(String sendTo, String titel, Map<String, Object> content, List<Pair<String, File>> attachments) {MimeMessage mimeMessage = mailSender.createMimeMessage();try {MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);helper.setFrom(emailConfig.getEmailFrom());helper.setTo(sendTo);helper.setSubject(titel);String text = VelocityEngineUtils.mergeTemplateIntoString(velocityEngine, "template.vm", "UTF-8", content);helper.setText(text, true);for (Pair<String, File> pair : attachments) {helper.addAttachment(pair.getLeft(), new FileSystemResource(pair.getRight()));}} catch (Exception e) {throw new RuntimeServiceException(e);}mailSender.send(mimeMessage);}
}

测试邮件发送:

src/main/java/com/training/email/controller/DemoEmailController.java

package com.training.email.controller;import io.swagger.annotations.ApiOperation;import javax.annotation.Resource;import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;import com.training.core.controller.BaseController;
import com.training.core.dto.ResultDataDto;
import com.training.core.email.EmailService;@RestController
@RequestMapping(value="/email")
public class DemoEmailController extends BaseController {@Resourceprivate EmailService emailService;/*** 测试邮件发送*/@ApiOperation(value="测试邮件发送", notes="getEntityById")@RequestMapping(value = "/getTestDemoEmail", method = RequestMethod.GET)public @ResponseBody ResultDataDto getEntityById() throws Exception {String sendTo = "1265400024@qq.com";String titel = "测试邮件标题";String content = "测试邮件内容";emailService.sendSimpleMail(sendTo, titel, content);return ResultDataDto.addSuccess();}
}

Spring Boot(十二)集成spring-boot-starter-mail发送邮件相关推荐

  1. (转)Spring Boot(十二):Spring Boot 如何测试打包部署

    http://www.ityouknow.com/springboot/2017/05/09/spring-boot-deploy.html 有很多网友会时不时的问我, Spring Boot 项目如 ...

  2. Spring(十二)Spring之事务

    java中事务是什么? 事务是访问数据库的一个操作序列,DB应用系统通过事务集来完成对数据的存取. 事务必须遵循4个原则,即常说的 ACID A,Automicity,原子性,即事务要么被全部执行,要 ...

  3. Spring Boot干货系列:(十二)Spring Boot使用单元测试 | 嘟嘟独立博客

    原文地址 2017-12-28 开启阅读模式 Spring Boot干货系列:(十二)Spring Boot使用单元测试 Spring Boot干货系列 Spring Boot 前言 这次来介绍下Sp ...

  4. Spring Boot(十四):spring boot整合shiro-登录认证和权限管理

    Spring Boot(十四):spring boot整合shiro-登录认证和权限管理 使用Spring Boot集成Apache Shiro.安全应该是互联网公司的一道生命线,几乎任何的公司都会涉 ...

  5. Spring Boot (十五): Spring Boot + Jpa + Thymeleaf 增删改查示例

    <p>这篇文章介绍如何使用 Jpa 和 Thymeleaf 做一个增删改查的示例.</p> 先和大家聊聊我为什么喜欢写这种脚手架的项目,在我学习一门新技术的时候,总是想快速的搭 ...

  6. JavaWeb学习总结(五十二)——使用JavaMail创建邮件和发送邮件

    JavaWeb学习总结(五十二)--使用JavaMail创建邮件和发送邮件 一.RFC882文档简单说明 RFC882文档规定了如何编写一封简单的邮件(纯文本邮件),一封简单的邮件包含邮件头和邮件体两 ...

  7. Spring精华问答 | 如何集成Spring Boot?

    Spring框架是一个开源的Java平台,它提供了非常容易,非常迅速地开发健壮的Java应用程序的全面的基础设施支持.今天就让我们一起来看看关于Spring的精华问答吧. 1 Q:如何在自定义端口上运 ...

  8. 第十二章 Spring Cloud Config 统一配置中心详解

    目录 一.配置问题分析及解决方案 1.问题分析 2.解决方案 二.Spring Cloud Config 介绍 1.Spring Cloud Config特性 2.Spring Cloud Confi ...

  9. Spring框架(二)Spring控制反转IoC详解

    目录 一,什么是Spring IoC容器 二,IoC有哪些优点 三,控制反转(IoC)有什么作用 四,IoC和DI有什么区别⭐ 五,Spring IoC的实现机制⭐ 六,IoC支持哪些功能 七,Bea ...

  10. ORM框架之Spring Data JPA(二)spring data jpa方式的基础增删改查

    上一篇主要在介绍hibernate实现jpa规范,如何实现数据增删改查,这一篇将会着重spring data jpa 一.Spring Data JPA 1.1 Spring Data JPA介绍: ...

最新文章

  1. 经济学相关资料20170924.词袋.books
  2. C++_sizeof关键字_实型(也叫浮点型)---C++语言工作笔记011
  3. Oracle 闪回特性(Flashback Query、Flashback Table)
  4. BP神经网络的基本思想,bp神经网络原理简述
  5. 电脑正常但windows安全中心有个黄色感叹号?
  6. STL-Intelligent IME
  7. Folx Pro5最新版适用Mac电脑网络BT下载器
  8. wordpress修改后台站点地址后无法打开的解决办法
  9. vue全局混入minx
  10. 服务器被劫持怎么修复不了,电脑DNS被劫持怎么修复?电脑dns被劫持的完美解决方法...
  11. 论文笔记1:Fast and Robust Multi-Person 3D Pose Estimation from Multiple Views
  12. Vue3配置postcss-pxtorem报错[plugin:vite:css] Failed to load PostCss config
  13. react 路由重定向_如何测试与测试库的路由器重定向React
  14. 西行漫记(14):慌神了
  15. 看了必懂的并查集原理(转载)
  16. 照相制版技术与图形转移技术
  17. 银河麒麟操作系统v10sp1安装eclipse
  18. 510分学计算机能上哪所大学,浙江全部59所大学排名,可分为6个档次,想去浙江上大学必看...
  19. 日本公路管理的基本模式
  20. 应用运维工程师(技术二面)面试笔记

热门文章

  1. php sdk 如何安装使用,0.2 微信SDK下载与安装使用
  2. 3GPP 38.885 V2X解决方案
  3. 搜狗镜像站群程序之搜狗批量推送接口
  4. Java操作poi添加页眉页脚:字体_颜色_大小
  5. iOS 横竖屏的切换以及个别界面横屏
  6. SQLDMO类在C#中的应用
  7. linux安装 gcc 7.2.0,CentOS 7 安装 gcc 9.2.0 最新版 | 24K PLUS
  8. 【linux】Rehat/centOS 离线安装软件(如gcc)的方法
  9. 强大的CSS:图形绘制合集,方便你我!
  10. 使用PDF处理控件Aspose.PDF以编程方式打印PDF文档完整攻略