- 浏览: 3514324 次
- 性别:
- 来自: 大连
博客专栏
-
使用Titanium Mo...
浏览量:37481
-
Cordova 3.x入门...
浏览量:604342
-
常用Java开源Libra...
浏览量:678107
-
搭建 CentOS 6 服...
浏览量:87280
-
Spring Boot 入...
浏览量:399819
-
基于Spring Secu...
浏览量:69075
-
MQTT入门
浏览量:90487
文章分类
最新评论
-
afateg:
阿里云的图是怎么画出来的?用什么工具?
各云服务平台的架构图 -
cbn_1992:
博主,采用jdbctoken也就是数据库形式之后,反复点击获取 ...
Spring Security OAuth2 Provider 之 数据库存储 -
ipodao:
写的很是清楚了,我找到一份中文协议:https://mcxia ...
MQTT入门(6)- 主题Topics -
Cavani_cc:
还行
MQTT入门(6)- 主题Topics -
fexiong:
博主,能否提供完整源码用于学习?邮箱:2199611997@q ...
TensorFlow 之 构建人物识别系统
(1)配置
pom.xml
application.properties
*** 一般只需要配置spring.mail.host属性即可。
(2)文本邮件
(3)HTML邮件
(4)带附件邮件
设置特殊编码
(5)Template模板
无论thymeleaf或freemarker都可以。(使用FreeMarker居多)
模板文件
src/main/resources/templates/email
(6)国际化对应
传入locale
模板文件
src/main/resources/templates/email
(7)异步发送邮件
开启异步支持@EnableAsync
设置@Async
(8)测试
FakeSMTP https://github.com/Nilhcem/FakeSMTP
smtp4dev https://github.com/rnwood/smtp4dev
Papercut https://github.com/changemakerstudios/papercut
GreenMail https://github.com/greenmail-mail-test/greenmail
代码示例
pom.xml
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId> </dependency>
application.properties
引用
spring.mail.host=localhost
spring.mail.protocol=smtp # Protocol
spring.mail.port=25 # SMTP server port.
spring.mail.username= # Login user of the SMTP server.
spring.mail.password= # Login password of the SMTP server.
spring.mail.default-encoding=UTF-8 # Default MimeMessage encoding.
# Additional JavaMail session properties.
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true
spring.mail.protocol=smtp # Protocol
spring.mail.port=25 # SMTP server port.
spring.mail.username= # Login user of the SMTP server.
spring.mail.password= # Login password of the SMTP server.
spring.mail.default-encoding=UTF-8 # Default MimeMessage encoding.
# Additional JavaMail session properties.
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true
*** 一般只需要配置spring.mail.host属性即可。
(2)文本邮件
@Service public class MailService { @Autowired private JavaMailSender mailSender; public void sendMail(String from, String to, String subject, String msg) { SimpleMailMessage message = new SimpleMailMessage(); message.setFrom(from); message.setTo(to); message.setSubject(subject); message.setText(msg); mailSender.send(message); } }
(3)HTML邮件
引用
message.setText("<html><h1>HTML EMAIL</h1></html>", true);
(4)带附件邮件
引用
File attachmentFile = new File("d://test.txt");
FileSystemResource file = new FileSystemResource(attachmentFile);
messageHelper.addAttachment("test.txt", file);
FileSystemResource file = new FileSystemResource(attachmentFile);
messageHelper.addAttachment("test.txt", file);
设置特殊编码
引用
File attachmentFile = new File("d://test.csv");
String csv = FileUtils.readFileToString(attachmentFile, "GBK");
javax.activation.DataSource dataSource = new ByteArrayDataSource(csv, "text/csv; charset=GBK");
messageHelper.addAttachment("test.csv", dataSource);
String csv = FileUtils.readFileToString(attachmentFile, "GBK");
javax.activation.DataSource dataSource = new ByteArrayDataSource(csv, "text/csv; charset=GBK");
messageHelper.addAttachment("test.csv", dataSource);
(5)Template模板
无论thymeleaf或freemarker都可以。(使用FreeMarker居多)
@Service public class MailService { @Autowired private JavaMailSender mailSender; @Autowired private TemplateEngine thymeleafTemplateEngine; @Autowired private Configuration freemarkerConfiguration; public void prepareAndSend(SimpleMailMessage msg, Map<String, Object> tplVariables) { MimeMessagePreparator preparator = new MimeMessagePreparator() { public void prepare(MimeMessage mimeMessage) throws Exception { MimeMessageHelper message = new MimeMessageHelper(mimeMessage); message.setTo(msg.getTo()); message.setFrom(msg.getFrom()); message.setSubject(msg.getSubject()); String body = buildThymeleaf(tplVariables); message.setText(body, true); } }; try { mailSender.send(preparator); } catch (MailException e) { // ... } } public String buildThymeleaf(String templateName, Map<String, Object> model) { Context context = new Context(); context.setVariables(model); return templateEngine.process(templateName, context); } public String buildFreeMarker(String templateName, Map<String, Object> model) { Template tpl = freemarkerConfiguration.getTemplate(templateName); return FreeMarkerTemplateUtils.processTemplateIntoString(tpl, model); } }
模板文件
src/main/resources/templates/email
引用
template_name.html
或
template_name.tpl
或
template_name.tpl
(6)国际化对应
传入locale
public String buildThymeleaf(String templateName, Map<String, Object> model, Locale locale) { Context context = new Context(locale); context.setVariables(model); return templateEngine.process(templateName, context); } public String buildFreeMarker(String templateName, Map<String, Object> model, Locale locale) { Template tpl = freemarkerConfiguration.getTemplate(templateName, locale); return FreeMarkerTemplateUtils.processTemplateIntoString(tpl, model); }
模板文件
src/main/resources/templates/email
引用
template_name.html
template_name_zh.html
template_name_ja.html
或
template_name.tpl
template_name_zh.tpl
template_name_ja.tpl
template_name_zh.html
template_name_ja.html
或
template_name.tpl
template_name_zh.tpl
template_name_ja.tpl
(7)异步发送邮件
开启异步支持@EnableAsync
@EnableAsync // 追加 @SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } }
设置@Async
@Async public void sendMail(String from, String to, String subject, String msg) { // ... }
(8)测试
FakeSMTP https://github.com/Nilhcem/FakeSMTP
smtp4dev https://github.com/rnwood/smtp4dev
Papercut https://github.com/changemakerstudios/papercut
GreenMail https://github.com/greenmail-mail-test/greenmail
代码示例
@Service public class MailService { @Autowired private JavaMailSender mailSender; @Autowired private TemplateEngine thymeleafTemplateEngine; @Autowired private Configuration freemarkerConfiguration; public void sendTextMail(String from, String to, String subject, String text) { return sendMail(from, to, subject, text, false); } public void sendHtmlMail(String from, String to, String subject, String htmlBody) { return sendMail(from, to, subject, htmlBody, true); } public void sendMail(String from, String to, String subject, String msg, Boolean isHtml) { SimpleMailMessage message = new SimpleMailMessage(); message.setFrom(from); message.setTo(to); message.setSubject(subject); message.setText(msg, isHtml); mailSender.send(message); } public void prepareAndSend(SimpleMailMessage msg, Map<String, Object> tplVariables) { MimeMessagePreparator preparator = new MimeMessagePreparator() { public void prepare(MimeMessage mimeMessage) throws Exception { MimeMessageHelper message = new MimeMessageHelper(mimeMessage); message.setTo(msg.getTo()); message.setFrom(msg.getFrom()); message.setSubject(msg.getSubject()); String body = buildThymeleaf(tplVariables); // ... message.setText(body, true); } }; try { mailSender.send(preparator); } catch (MailException e) { // ... } } public String buildThymeleaf(String templateName, Map<String, Object> model) { Context context = new Context(); context.setVariables(model); return templateEngine.process(templateName, context); } public String buildThymeleaf(String templateName, Map<String, Object> model, Locale locale) { Context context = new Context(locale); context.setVariables(model); return templateEngine.process(templateName, context); } public String buildFreeMarker(String templateName, Map<String, Object> model) { Template tpl = freemarkerConfiguration.getTemplate(templateName); return FreeMarkerTemplateUtils.processTemplateIntoString(tpl, model); } public String buildFreeMarker(String templateName, Map<String, Object> model, Locale locale) { Template tpl = freemarkerConfiguration.getTemplate(templateName, locale); return FreeMarkerTemplateUtils.processTemplateIntoString(tpl, model); } }
发表评论
-
Spring Boot 入门 - 进阶篇(8)- 应用监控(Actuator)
2017-03-16 14:57 17504作为Spring Boot的另外一大亮点,就是actuator ... -
Spring Boot 入门 - 进阶篇(7)- 自动配置(AutoConfigure)
2017-03-16 11:05 62140自动配置是Spring Boot的最大亮点,完美的展示了CoC ... -
Spring Boot 入门 - 进阶篇(6)- 启动加载(CommandLineRunner)
2017-03-15 15:04 14975启动成功后可以通过以下方法运行自己的初始代码: @PostCo ... -
Spring Boot 入门 - 进阶篇(5)- 数据缓存(@Cacheable)
2017-03-14 16:28 34567缓存可以缓解数据库访 ... -
Spring Boot 入门 - 进阶篇(4)- REST访问(RestTemplate)
2017-03-14 11:07 45170经常需要发送一个GET/POST请求到其他系统(REST AP ... -
Spring Boot 入门 - 进阶篇(3)- 定时任务(@Scheduled)
2017-03-13 13:23 23652主要用于定时发送邮件、夜间自动维护等。 (1)开启定时任务功 ... -
Spring Boot 入门 - 进阶篇(2)- 异步调用(@Async)
2017-03-07 15:59 20003异步处理 Java的异步处理Thread/Runnable、 ... -
Spring Boot 入门 - 进阶篇(1)- Servlet、Filter、Listener、Interceptor
2017-03-07 10:39 10547用户认证授权、日志记录MDC、编码解码、UA检查、多端对应等都 ... -
Spring Boot 入门 - 基础篇(15)- 工程部署
2017-02-16 15:31 8992(1)开发阶段 一般开发过程: 1)-写代码 2)- [Ru ... -
Spring Boot 入门 - 基础篇(14)- 参数设置
2017-02-16 15:25 5679(1)读取优先顺序 a - 命令行参数 --key=val ... -
Spring Boot 入门 - 基础篇(13)- 异常处理
2017-02-16 10:23 8605先要了解Spring的异常处理:http://rensanni ... -
Spring Boot 入门 - 基础篇(12)- 数据校验
2017-02-16 09:53 19758除过在客户端做JavaScript数据校验外,服务器端做数据校 ... -
Spring Boot 入门 - 基础篇(11)- 数据源配置
2017-02-15 11:12 16318(1)单一数据源 默认Spring Boot会在classp ... -
Spring Boot 入门 - 基础篇(9)- 文件上传下载
2017-02-14 10:01 15790(1)单文件上传 Form方式 <form id=&qu ... -
Spring Boot 入门 - 基础篇(8)- 数据库操作
2017-02-10 16:17 8605(1)导入mybatis-spring-boot-starte ... -
Spring Boot 入门 - 基础篇(7)- 国际化
2017-02-10 13:58 13074Spring Boot默认支持国际化配置,只需要添加配置文件即 ... -
Spring Boot 入门 - 基础篇(6)- 页面模板
2017-02-09 15:00 6395Spring Boot支持很多模板引擎,但嵌入式容器JSP有限 ... -
Spring Boot 入门 - 基础篇(5)- 使用WebJars
2017-02-09 14:20 11663WebJars能使Maven的依赖管理支持OSS的JavaSc ... -
Spring Boot 入门 - 基础篇(4)- 静态资源
2017-02-09 13:10 10648静态资源包括:HTML、CSS、JS、图像、视频、PDF/Of ... -
Spring Boot 入门 - 基础篇(3)- 日志管理
2017-02-09 09:39 8421Spring Boot支持JUL,Log4J2和Logback ...
相关推荐
NULL 博文链接:https://rensanning.iteye.com/blog/2357171
00、尚硅谷_SpringBoot_源码、课件 01、尚硅谷_SpringBoot_入门-课程简介 02、尚硅谷_SpringBoot_入门-Spring Boot简介 03、尚硅谷_SpringBoot_入门-微服务简介 04、尚硅谷_SpringBoot_入门-环境准备 05、尚硅谷_...
---Day01SpringBoot基础重温篇.mp4 ---Day02SpringBoot核心原理篇.mp4 ---Day03SpringBoot完美收官篇.mp4 优秀作业名单 ---Day01优秀作业名单(排名不分先后).txt ---Day02优秀作业名单(排名不分先后).txt 源码和软件...
推荐工具:-OpenWrite:Markdown微信编辑器是一种... 二,系列文章目录『Spring Boot 2快速教程』『基础-入门篇』『基础-Web业务开发篇』Spring Boot使用Swagger2构建RESRful API文档Spring Boot集成JSP 弹簧靴集成胸腺
内容概要:该资源是Spring全家桶视频课程的第三部分Spring Boot的源码,其对于Spring Boot的最常用的第一个小程序、JavaConfig和常见注解、启动改变、热部署、Springboot整合Thymeleaf/freemarker/JSP、异常处理、...
入门/基础 - 在本文中,我们将学习如何使用 Spring MVC 5 +、JSP、Maven 构建工具和 Eclipse IDE 创建一个简单的 Hello World Spring MVC 应用程序 - 在本文中,我们将学习如何使用 Spring MVC 5+、Maven 构建工具、...
Spring Boot入门 Spring Boot配置 Spring Boot与日志 Spring Boot与Web开发 Spring Boot与Docker Spring Boot与数据访问 Spring Boot启动配置原理
主要给大家介绍了关于Spring boot的相关资料,本文属于基础入门教程,对各位学习Spring boot的新手们具有一定的参考学习价值,,要的朋友们下面来一起看看吧。
一、Spring Boot 入门 1、Spring Boot 简介 简化Spring应用开发的一个框架; 整个Spring技术栈的一个大整合; J2EE开发的一站式解决方案; 2、微服务 2014,martin fowler 微服务:架构风格(服务微化) 一个应用...
Spring启动教程项目简介Spring Boot Tutorial是一个入门级的教程,主要包含Spring Boot的使用,以及如何集成各种第三方组件等内容。比较适合初学Spring Boot的同学们。博客:本系列教程PDF:准备中,尽请期待...目录...
02、尚硅谷_SpringBoot_入门-Spring Boot简介 03、尚硅谷_SpringBoot_入门-微服务简介 04、尚硅谷_SpringBoot_入门-环境准备 05、尚硅谷_SpringBoot_入门-springboot-helloworld 06、尚硅谷_SpringBoot_入门-...
这个例子展示了如何将Angular和Spring Boot部署到Heroku。 它还显示了如何使用ng deploy将Angular部署到Firebase,Netlify和AWS S3。 请阅读以了解如何创建此应用程序。 这篇博客文章是系列文章的第3部分。 本系列...
Atomikos曾介绍使用TCC作为微服务的分布式事务解决方案,有一篇简单的译文可作为入门资料。经文章叙述,Atomikos所设计的TCC交互完全整合在HTTP协议之上,并充分地替代了HTTP语义特性,是一个与应用层协议紧接解决...
小编 做的这个项目有部分功能用到了 Activiti工作流,目前对工作流的掌握程度是基本会用,我之前写过一篇 Activiti 入门篇,当前这篇文章是属于Activiti 实战部分,如果你想知道Activiti 工作流是如何使用的话,那就...
包含:socket网络编程及实战,系统学习docker,看的见的算法 7个经典应用诠释算法精髓,一站式学习Redis 从入门到高可用分布式实践,Spring Boot 2.0深度实践之核心技术篇,Scala 学习 进击大数据Spark生态圈,剑指Java面试...
这两个基本上是主要问的Java框架: 框架这部分包含很多,入门框架有Spring、Spring MVC、Mybatis,目前SSH很少使用了,微服务生态这边有Spring Boot、Spring Cloud分布式:市面上流行的分布式框架,分布式缓存,消息...