(提供源代码)

Spring提供了非常好用的JavaMailSender接口实现邮件发送。在Spring Boot的Starter模块中也为此提供了自动化配置。下面通过实例看看如何在Spring Boot中使用JavaMailSender发送邮件。

快速入门:

那么如何进行使用呢?很简单最核心的就两个步骤:

在Spring Boot的工程中的pom.xml中引入spring-boot-starter-mail依赖:

<!-- 发送邮件. -->

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-mail</artifactId>

</dependency>

如其他自动化配置模块一样,在完成了依赖引入之后,只需要在application.properties中配置相应的属性内容。

下面我们以QQ邮箱为例,在application.properties中加入如下配置(注意替换自己的用户名和密码):

########################################################

###mail setting

########################################################

# 设置邮箱主机

spring.mail.host=smtp.qq.com

# 设置用户名

spring.mail.username=用户名

# 设置密码

spring.mail.password=密码

# 设置是否需要认证,如果为true,那么用户名和密码就必须的,

#如果设置false,可以不设置用户名和密码,当然也得看你的对接的平台是否支持无密码进行访问的。

spring.mail.properties.mail.smtp.auth=true

# STARTTLS[1]  是对纯文本通信协议的扩展。它提供一种方式将纯文本连接升级为加密连接(TLS或SSL),而不是另外使用一个端口作加密通信。

spring.mail.properties.mail.smtp.starttls.enable=true

spring.mail.properties.mail.smtp.starttls.required=true

接下来我们通过单元测试来测试简单邮件的发送:

(如何在spring boot进行单元测试可以参看文章:

http://412887952-qq-com.iteye.com/blog/2292739

package com.kfit;

import org.junit.Test;

import org.junit.runner.RunWith;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.boot.test.SpringApplicationConfiguration;

import org.springframework.mail.SimpleMailMessage;

import org.springframework.mail.javamail.JavaMailSender;

import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

/**

*

* @author Angel(QQ:412887952)

* @version v.0.1

*/

@RunWith(SpringJUnit4ClassRunner.class)

@SpringApplicationConfiguration(classes = App.class)

public class AppTest {

@Autowired

private JavaMailSender mailSender;

/**

* 修改application.properties的用户,才能发送。

*/

@Test

public void sendSimpleEmail(){

SimpleMailMessage message = new SimpleMailMessage();

message.setFrom("412887952@qq.com");//发送者.

message.setTo("1473773560@qq.com");//接收者.

message.setSubject("测试邮件(邮件主题)");//邮件主题.

message.setText("这是邮件内容");//邮件内容.

mailSender.send(message);//发送邮件

}

}

一个简单的邮件发送就完成了,运行一下该单元测试,看看效果如何?

由于Spring Bootstarter模块提供了自动化配置,所以在引入了spring-boot-starter-mail依赖之后,会根据配置文件中的内容去创建JavaMailSender实例,因此我们可以直接在需要使用的地方直接@Autowired来引入邮件发送对象。

进阶使用:

我们通过使用SimpleMailMessage实现了简单的邮件发送,但是实际使用过程中,我们还可能会带上附件、或是使用邮件模块等。这个时候我们就需要使用MimeMessage来设置复杂一些的邮件内容,下面我们就来依次实现一下。

发送附件

发送附件主要通过MimeMessageHelper来进行操作的,实际中也很简单。

/**

* 测试发送附件.(这里发送图片.)

* @throws MessagingException

*/

@Test

public void sendAttachmentsEmail() throws MessagingException{

//这个是javax.mail.internet.MimeMessage下的,不要搞错了。

MimeMessage mimeMessage =  mailSender.createMimeMessage();

MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);

//基本设置.

helper.setFrom("412887952@qq.com");//发送者.

helper.setTo("1473773560@qq.com");//接收者.

helper.setSubject("测试附件(邮件主题)");//邮件主题.

helper.setText("这是邮件内容(有附件哦.)");//邮件内容.

//org.springframework.core.io.FileSystemResource下的:

//附件1,获取文件对象.

FileSystemResource file1 = new FileSystemResource(new File("D:/test/head/head1.jpg"));

//添加附件,这里第一个参数是在邮件中显示的名称,也可以直接是head.jpg,但是一定要有文件后缀,不然就无法显示图片了。

helper.addAttachment("头像1.jpg", file1);

//附件2

FileSystemResource file2 = new FileSystemResource(new File("D:/test/head/head2.jpg"));

helper.addAttachment("头像2.jpg", file2);

mailSender.send(mimeMessage);

}

嵌入静态资源:

除了发送附件之外,我们在邮件内容中可能希望通过嵌入图片等静态资源,让邮件获得更好的阅读体验,而不是从附件中查看具体图片,下面的测试用例演示了如何通过MimeMessageHelper实现在邮件正文中嵌入静态资源。

内嵌图片,给定一个CID值即可,增加附件,使用MimeMessageHelper的addAttachment即可现在一般不会做内嵌图片,因为这样邮件会很大,容易对服务器造成压力,一般做法是使用图片链接另外,如果要做内嵌或发送图片,你应该使用信用较高的邮箱帐户,否则会报错:554 DT:SPM 发送的邮件内容包含了未被许可的信息,或被系统识别为垃圾邮件。请检查是否有用户发送病毒或者垃圾邮件
对于163邮箱服务器会产生的其他问题,参见:http://help.163.com/09/1224/17/5RAJ4LMH00753VB8.html

以下是发送静态资源的核心代码:

/**

* 邮件中使用静态资源.

* @throws Exception

*/

@Test

public void sendInlineMail() throws Exception {

MimeMessage mimeMessage = mailSender.createMimeMessage();

MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);

//基本设置.

helper.setFrom("412887952@qq.com");//发送者.

helper.setTo("1473773560@qq.com");//接收者.

helper.setSubject("测试静态资源(邮件主题)");//邮件主题.

// 邮件内容,第二个参数指定发送的是HTML格式

//说明:嵌入图片<img src='cid:head'/>,其中cid:是固定的写法,而aaa是一个contentId。

helper.setText("<body>这是图片:<img src='cid:head' /></body>", true);

FileSystemResource file = new FileSystemResource(new File("D:/test/head/head1.jpg"));

helper.addInline("head",file);

mailSender.send(mimeMessage);

}

这里需要注意的是addInline函数中资源名称head需要与正文中cid:head对应起来

嵌入图片<img src='cid:head'/>,其中cid:是固定的写法,而aaa是一个contentId。


 

模板邮件:

通常我们使用邮件发送服务的时候,都会有一些固定的场景,比如重置密码、注册确认等,给每个用户发送的内容可能只有小部分是变化的。所以,很多时候我们会使用模板引擎来为各类邮件设置成模板,这样我们只需要在发送时去替换变化部分的参数即可。

在Spring Boot中使用模板引擎来实现模板化的邮件发送也是非常容易的,下面我们以freemarker为例实现一下。

引入freemarker模块的依赖:

<!-- 引入模板引擎. -->

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-freemarker</artifactId>

</dependency>

加入配置信息:

########################################################

###FREEMARKER (FreeMarkerAutoConfiguration)

########################################################

spring.freemarker.allow-request-override=false

spring.freemarker.cache=true

spring.freemarker.check-template-location=true

spring.freemarker.charset=UTF-8

spring.freemarker.content-type=text/html

spring.freemarker.expose-request-attributes=false

spring.freemarker.expose-session-attributes=false

spring.freemarker.expose-spring-macro-helpers=false

#spring.freemarker.prefix=

#spring.freemarker.request-context-attribute=

#spring.freemarker.settings.*=

#spring.freemarker.suffix=.ftl

#spring.freemarker.template-loader-path=classpath:/templates/ #comma-separated list

resources/templates/下,创建一个模板页面email.ftl

<html>

<body>

<h3>你好, ${username}, 这是一封模板邮件!</h3>

</body>

</html>

最后,我们在单元测试中加入发送模板邮件的测试用例,具体如下:

/**

* 模板邮件;

* @throws Exception

*/

@Test

public void sendTemplateMail() throws Exception {

MimeMessage mimeMessage = mailSender.createMimeMessage();

MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);

//基本设置.

helper.setFrom("412887952@qq.com");//发送者.

helper.setTo("1473773560@qq.com");//接收者.

helper.setSubject("模板邮件(邮件主题)");//邮件主题.

Map<String, Object> model = new HashMap<String, Object>();

model.put("username", "林峰");

Configuration cfg = new Configuration(Configuration.VERSION_2_3_23);

// 设定去哪里读取相应的ftl模板

cfg.setClassForTemplateLoading(this.getClass(), "/templates");

// 在模板文件目录中寻找名称为name的模板文件

Template template   = cfg.getTemplate("email.ftl");

String html = FreeMarkerTemplateUtils.processTemplateIntoString(template, model);

helper.setText(html, true);

mailSender.send(mimeMessage);

}

运行一下,就可以收到内容为你好,林峰, 这是一封模板邮件!的邮件。这里,我们通过传入username的参数,在邮件内容中替换了模板中的${username}变量。

Spring Boot 系列博客】

54. spring boot日志升级篇—logback【从零开始学Spring Boot】

52. spring boot日志升级篇—log4j多环境不同日志级别的控制【从零开始学Spring Boot】

51. spring boot属性文件之多环境配置【从零开始学Spring Boot】

50. Spring Boot日志升级篇—log4j【从零开始学Spring Boot】

49. spring boot日志升级篇—理论【从零开始学Spring Boot】

48. spring boot单元测试restfull API【从零开始学Spring Boot】

47. Spring Boot发送邮件【从零开始学Spring Boot】

46. Spring Boot中使用AOP统一处理Web请求日志

45. Spring Boot MyBatis连接Mysql数据库【从零开始学Spring Boot】

44. Spring Boot日志记录SLF4J【从零开始学Spring Boot】

43. Spring Boot动态数据源(多数据源自动切换)【从零开始学Spring Boot】

42. Spring Boot多数据源【从零开始学Spring Boot】

41. Spring Boot 使用Java代码创建Bean并注册到Spring中【从零开始学Spring Boot】

40. springboot + devtools(热部署)【从零开始学Spring Boot】

39.4 Spring Boot Shiro权限管理【从零开始学Spring Boot】

39.3 Spring Boot Shiro权限管理【从零开始学Spring Boot】

39.2. Spring Boot Shiro权限管理【从零开始学Spring Boot】

39.1 Spring Boot Shiro权限管理【从零开始学Spring Boot】

38 Spring Boot分布式Session状态保存Redis【从零开始学Spring Boot】

37 Spring Boot集成EHCache实现缓存机制【从零开始学Spring Boot】

36 Spring Boot Cache理论篇【从零开始学Spring Boot】

35 Spring Boot集成Redis实现缓存机制【从零开始学Spring Boot】

34Spring Boot的启动器Starter详解【从零开始学Spring Boot】

33 Spring Boot 监控和管理生产环境【从零开始学Spring Boot】

32 Spring Boot使用@SpringBootApplication注解【从零开始学Spring Boot】

31 Spring Boot导入XML配置【从零开始学Spring Boot】

更多查看博客: http://412887952-qq-com.iteye.com/

47. Spring Boot发送邮件【从零开始学Spring Boot】的更多相关文章

  1. (41)Spring Boot 使用Java代码创建Bean并注册到Spring中【从零开始学Spring Boot】

    已经好久没有讲一些基础的知识了,这一小节来点简单的,这也是为下节的在Spring Boot中使用多数据源做准备. 从Spring 3.0开始,增加了一种新的途径来配置Bean Definition,这 ...

  2. 57. Spring 自定义properties升级篇【从零开始学Spring Boot】

    之前在两篇文章中都有简单介绍或者提到过 自定义属性的用法: 25.Spring Boot使用自定义的properties[从零开始学Spring Boot] 51. spring boot属性文件之多 ...

  3. 17、Spring Boot普通类调用bean【从零开始学Spring Boot】

    转载:http://blog.csdn.net/linxingliang/article/details/52013017 我们知道如果我们要在一个类使用spring提供的bean对象,我们需要把这个 ...

  4. 21. Spring Boot过滤器、监听器【从零开始学Spring Boot】

    转载:http://blog.csdn.net/linxingliang/article/details/52069490 上一篇文章已经对定义Servlet 的方法进行了说明,过滤器(Filter) ...

  5. 81. Spring Boot集成JSP疑问【从零开始学Spring Boot】

    [原创文章,转载请注明出处] 针对文章: ()Spring Boot 添加JSP支持[从零开始学Spring Boot] 有网友提了这么一些疑问: 1.Spring Boot使用jsp时,仍旧可以打成 ...

  6. 78. Spring Boot完美使用FastJson解析JSON数据【从零开始学Spring Boot】

    [原创文章,转载请注明出处] 个人使用比较习惯的json框架是fastjson,所以spring boot默认的json使用起来就很陌生了,所以很自然我就想我能不能使用fastjson进行json解析 ...

  7. 77. Spring Boot Use Thymeleaf 3【从零开始学Spring Boot】

    [原创文章,转载请注明出处] Spring Boot默认选择的Thymeleaf是2.0版本的,那么如果我们就想要使用3.0版本或者说指定版本呢,那么怎么操作呢?在这里要说明下 3.0的配置在spri ...

  8. 75. Spring Boot 定制URL匹配规则【从零开始学Spring Boot】

    在之前有一篇文章说了,博客名称从原来的<从零开始学Spring Boot>更改为<Spring Boot常见异常汇总>,后来写了几篇文章之后发展,有些文章还是一些知识点,所以后 ...

  9. 74. Spring Data JPA方法定义规范【从零开始学Spring Boot】

    [从零开始学习Spirng Boot-常见异常汇总] 事情的起因:有人问过我们这个这个问题:为什么我利用Spring data jpa写的方法没有按照我想要的情况进行执行呢?我记得当时只是告诉他你你先 ...

随机推荐

  1. C++中正确使用PRId64 (转载)

    转自:http://blog.csdn.net/win_lin/article/details/7912693 例子参考高性能流媒体服务器SRS:https://github.com/winlinvi ...

  2. PCB 漏加阻抗条的臆想(转)

    阻抗条,我对你是有感情的,这你一定要相信我! 否则,不会在之前的每一次拼板,都不忘拥你入Panel之怀. 自做CAM开始,已记不清我们曾有多少次不期而遇, 我们一同迎接朝阳,送走晚霞,凝望窗外如洗的月 ...

  3. bzoj 1050: [HAOI2006]旅行comf【枚举+并查集】

    m是5000,就想到了直接枚举比例 具体做法是是先把边按照边权从小到大排序,然后先枚举最小边权,再枚举最大边权,就是从最小边权里一个一个加进并查集里,每次查st是否联通,联通则退出,更新答案 #inc ...

  4. bzoj1015星球大战(并查集+离线)

    1015: [JSOI2008]星球大战starwar Time Limit: 3 Sec  Memory Limit: 162 MBSubmit: 5572  Solved: 2563 Descri ...

  5. 启动VMware环境下的Linux操作系统,添加新分区

    启动VMware环境下的Linux操作系统,添加新分区,需要root账号身份. 3.1 [fdisk -l] 最大分区为/dev/sda3,说明新创建的分区将会是sda4 3.2 输入[fdisk / ...

  6. linux编译安装gcc5.3.0

    1.下载GCC5.3.0安装包 #su #cd /opt #wget http://ftp.gnu.org/gnu/gcc/gcc-5.3.0/gcc-5.3.0.tar.gz 2.解压 #.tar. ...

  7. 全面学习ORACLE Scheduler特性(2)管理jobs

    1.2  管理Jobs 1.2.1  启用Jobs 前面创建JOB时,由于未显式的指定ENABLED参数,因此即使指定了START_DATE,不过默认情况下JOB不会自动执行.对于这种情况,DBMS_ ...

  8. jQuery中$this和$(this)的区别

    要写一个点击弹窗任意地方,关闭弹窗.点击事件写标签在元素上 onclick =  closepop(this),这时候很容易搞不清楚怎么去获取当前元素 function closepop(e){ va ...

  9. SQL Server的安装笔记

    SQL安装笔记 安装SQL Server 2008 打开SQL Server 2008中的setup.exe,显示SQL安装程序的对话框. 提示必须安装相关组件Microsoft.NET Framew ...

  10. C#压缩文件夹至zip,不包含所选文件夹【转+修改】

    转自园友:jimcsharp的博文C#实现Zip压缩解压实例[转] 在此基础上,对其中的压缩文件夹方法略作修正,并增加是否对父文件夹进行压缩的方法.(因为笔者有只压缩文件夹下的所有文件,却不想将选中的 ...