前面已经讲了使用springboot采用常规的javaweb方式发送邮件使用spring模板发送邮件。但是发送的都是文本文件,现在来说一下使用spring模板发送一些其他的邮件。

1.pom.xml依赖

     <!--spring发送邮件-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
<!--模板邮件-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
<dependency>
<groupId>commons-collections</groupId>
<artifactId>commons-collections</artifactId>
</dependency>

2.配置文件:

#####163邮箱########
spring.mail.host=smtp.163.com
spring.mail.username=xxxxxxx@163.com
#163邮箱授权码
spring.mail.password=*********
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true
spring.mail.properties.mail.smtp.starttls.required=true

3.发送各种邮件方法:

@Controller
public class ModelController { @Autowired
private JavaMailSender mailSender;
@Value("${spring.mail.username}")
private String Sender; //读取配置文件中的参数
@Autowired
private FreeMarkerConfigurer freeMarkerConfigurer; /**
* spring发送文本邮件
*/
@RequestMapping("/mail")
@ResponseBody
public String sendMail(){
SimpleMailMessage message = new SimpleMailMessage();
message.setFrom(Sender);
message.setTo("*********@qq.com");
message.setSubject("主题:简单邮件");
message.setText("测试邮件内容");
mailSender.send(message);
return "发送成功!";
} /**
* spring发送Html邮件
*/
@RequestMapping("/mailHtml")
@ResponseBody
public String sendHtmlMail(){
MimeMessage message = null;
try {
message = mailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(message, true);
helper.setFrom(Sender);
helper.setTo("*********@qq.com");
helper.setSubject("标题:发送Html内容"); StringBuffer sb = new StringBuffer();
sb.append("<h1>大标题-h1</h1>")
.append("<p style='color:#F00'>红色字</p>")
.append("<p style='text-align:right'>右对齐</p>");
helper.setText(sb.toString(), true);
} catch (Exception e) {
e.printStackTrace();
}
mailSender.send(message);
return "发送成功!";
} /**
* spring发送带附件的邮件
*/
@RequestMapping("/mailAttach")
@ResponseBody
public String sendAttachmentsMail(){
MimeMessage message = null;
try {
message = mailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(message, true);
helper.setFrom(Sender);
helper.setTo("*********@qq.com");
helper.setSubject("主题:带附件的邮件");
helper.setText("带附件的邮件内容");
//注意项目路径问题,自动补用项目路径
FileSystemResource file = new FileSystemResource(new File("F:\\临时\\SpringBoot\\src\\main\\resources\\static\\image\\迪丽热巴.jpg"));
//加入邮件
helper.addAttachment("热巴.jpg", file);
} catch (Exception e){
e.printStackTrace();
}
mailSender.send(message);
return "发送成功!";
} /**
* spring发送带静态资源的邮件,资源直接发在邮件内容里,不是附件形式。
*/
@RequestMapping("/mailLine")
@ResponseBody
public String sendInlineMail(){
MimeMessage message = null;
try {
message = mailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(message, true);
helper.setFrom(Sender);
helper.setTo("*********@qq.com");
helper.setSubject("主题:带静态资源的邮件");
//第二个参数指定发送的是HTML格式,同时cid:是固定的写法
helper.setText("<html><body>带静态资源的邮件内容 图片:<img src='cid:picture' /></body></html>", true); FileSystemResource file = new FileSystemResource(new File("F:\\临时\\SpringBoot\\src\\main\\resources\\static\\image\\迪丽热巴.jpg"));
helper.addInline("picture",file);
} catch (Exception e){
e.printStackTrace();
}
mailSender.send(message);
return "发送成功!";
} /**
* spring发送模板邮件
*/
@RequestMapping("/mailTemplate")
@ResponseBody
public String sendTemplateMail(){
MimeMessage message = null;
try {
message = mailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(message, true);
helper.setFrom(Sender);
helper.setTo("*********@qq.com");
helper.setSubject("主题:模板邮件"); Map<String, Object> model = new HashedMap();
model.put("username", "zggdczfr"); //修改 application.properties 文件中的读取路径
// FreeMarkerConfigurer configurer = new FreeMarkerConfigurer();
// configurer.setTemplateLoaderPath("classpath:templates");
//读取 html 模板
Template template = freeMarkerConfigurer.getConfiguration().getTemplate("mail.html");
String html = FreeMarkerTemplateUtils.processTemplateIntoString(template, model);
helper.setText(html, true);
} catch (Exception e) {
e.printStackTrace();
}
mailSender.send(message);
return "发送成功!";
} }

4.图片和模板存放位置:

spring boot发送其他邮件的更多相关文章

  1. Spring Boot实战系列-----------邮件发送

    快速导航 添加Maven依赖 配置文件增加邮箱相关配置 Service.Test项目代码构建 五种邮件发送类型讲解 文本邮件 html邮件 附件邮件 html内嵌图片邮件 模板邮件 问题汇总 添加ma ...

  2. Spring Boot Mail 实现邮件发送

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

  3. spring boot:发送带附件的邮件和html内容的邮件(以163.com邮箱为例/spring boot 2.3.2)

    一,网站哪些情况下需要发送电子邮件? 作为一个电商网站,以下情况需要发邮件通知用户: 注册成功的信息 用邮箱接收验证码 找回密码时发链接 发送推广邮件 下单成功后的订单通知 给商户的对账单邮件 说明: ...

  4. Spring Boot (十):邮件服务

    Spring Boot 仍然在狂速发展,才几个多月没有关注,现在看官网已经到 2.1.0.RELEASE 版本了.准备慢慢在写写 Spring Boot 相关的文章,本篇文章使用 Spring Boo ...

  5. (转)Spring Boot (十):邮件服务

    http://www.ityouknow.com/springboot/2017/05/06/spring-boot-mail.html Spring Boot 仍然在狂速发展,才几个多月没有关注,现 ...

  6. Spring Boot笔记之邮件(spring-boot-starter-mail)

    Spring Boot环境中发送邮件 pom.xml引入`spring-boot-starter-mail` application.yml配置 163邮箱 QQ邮箱 Gmail邮箱 发送邮件 ser ...

  7. spring boot集成spring-boot-starter-mail邮件功能

    前情提要 以目前IT系统功能来看,邮件功能是非常重要的一个功能.例如:找回密码.邮箱验证,邮件动态码.忘记密码,邮件营销等,都需要用到邮件功能.结合当下最流行的spring boot微服务,推出了sp ...

  8. spring boot 加入mail邮件支持

    一.添加依赖 <!-- 邮件整合 --> <dependency> <groupId>org.springframework.boot</groupId> ...

  9. 使用Spring Mail发送QQ邮件

    一.邮箱设置 QQ邮箱设置:http://service.mail.qq.com/cgi-bin/help?id=28, 下面这些服务需要开启(需要设置邮箱独立密码): 二.applicationCo ...

随机推荐

  1. java订单生成工具类

    欢迎来到付宗乐个人博客网站.本个人博客网站提供最新的站长新闻,各种互联网资讯. 还提供个人博客模板,最新最全的java教程,java面试题.在此我将尽我最大所能将此个人博客网站做的最好! 谢谢大家,愿 ...

  2. Two types of people with high scores of English exams

    I believe that there are two types of people who get high scores in English exams: 1) have high inte ...

  3. 使用抽象工厂反射获取不到Dal层对象,未能加载文件或程序集......

    Put aside the fog and see the essence 解决问题之前,要明白问题为什么会出现 我相信能点开这篇帖子的人,都是具有探索精神的人,因为,只有心存疑问才会搜索 如果只想单 ...

  4. node一键发布,并运行

    作为一个前端开发人员如果你只会写一些业务代码,从程序员的角度来考虑已经可以了.但是从架构的角度来考虑那远远不够: 在此记录下成长中的经历: 想要达成的目的:运行一个脚本实现代码的打包,上传至服务器并部 ...

  5. Sublime Text 3 使用手册

    Ctrl+Shift+P:打开命令面板 Ctrl+P:搜索项目中的文件 Ctrl+G:跳转到第几行 Ctrl+W:关闭当前打开文件 Ctrl+Shift+W:关闭所有打开文件 Ctrl+Shift+V ...

  6. JSP引擎、JSP容器、Web服务器

    JSP引擎与JSP容器指的都是同一样的东西,他们都是用来同一管理和运行Web引用程序的“软件”.常见的JSP引擎有Tomcat.JRun.Resin 广义上来说,JSP引擎是用来管理和运行Web应用程 ...

  7. Gradle-构建生命周期

    两个重要的概念 项目 实际上,一个项目是什么取决于你要用 Gradle 做什么?项目通常代表的是构建内容. 例如在 Android 中,一个 module 就是一个项目: 项目是注册在 setting ...

  8. c语言和c++的交换函数

    #include<iostream> using namespace std; namespace LiuGang{//在命名空间中写函数 void swap(int&aa,int ...

  9. C++通过宏定义判断操作系统及编译器

    INTRODUCTION: C++的编译环境千奇百怪,很多时候一些代码在某些编译环境下可用,一旦移到其他环境下,就会干脆Compile Error 对此,我们可以使用C++的宏定义来判断操作系统,从而 ...

  10. Leetcode之回溯法专题-79. 单词搜索(Word Search)

    Leetcode之回溯法专题-79. 单词搜索(Word Search) 给定一个二维网格和一个单词,找出该单词是否存在于网格中. 单词必须按照字母顺序,通过相邻的单元格内的字母构成,其中“相邻”单元 ...