Spring Boot笔记之邮件(spring-boot-starter-mail)
Spring Boot环境中发送邮件
pom.xml引入spring-boot-starter-mail
Spring Boot2.x集成了mail模块,在dependencies
引入这个
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
application.yml配置
spring:
mail:
# 163
host: smtp.163.com
port:
username: yimcarson@163.com
password: ************
protocol: smtp
default-encoding: UTF-8
properties:
mail.smtp.auth: true
mail.smtp.starttls.enable: true
mail.smtp.starttls.required: true
mail.smtp.socketFactory.port: 465
mail.smtp.socketFactory.class: javax.net.ssl.SSLSocketFactory
mail.smtp.socketFactory.fallback: false
其中spring.mail.host
spring.mail.port
spring.mail.username
spring.mail.password
不同邮箱的配置方法也不同
163邮箱
spring:
mail:
host: smtp.163.com
port:
username: yimcarson@163.com
password: ************
其中spring.mail.port
不指定;spring.mail.password
不是邮箱密码,需要登录mail.163.com
,前往设置
客户端授权密码
中获取的一个16个字符的密码,同时允许POP3/SMTP服务。
QQ邮箱
spring:
mail:
host: smtp.qq.com
port: 587
username: yimcarson@qq.com
password: ************
spring.mail.password
不是QQ密码,登录mail.qq.com
,前往设置
账户
POP3/IMAP/SMTP/Exchange/CardDAV/CalDAV服务
开启POP3/SMTP服务
获取一个16个字符的密码
Gmail邮箱
spring:
mail:
host: smtp.gmail.com
port: 465
username: yimcarson@gmail.com
password: ****************
spring.mail.password
是Gmail的密码,但是要前往Google账户的安全性较低的应用的访问权限中允许不安全应用。
发送邮件
这是一个验证码模版邮件
service实现类
import com.my.demo.project.service.MailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.mail.javamail.MimeMessagePreparator;
import org.springframework.stereotype.Service;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;
import java.util.UUID;
@Service
public class MailServiceImpl implements MailService {
<span class="token annotation punctuation">@Autowired</span>
<span class="token keyword">private</span> JavaMailSender mailSender<span class="token punctuation">;</span>
<span class="token comment">/**
* 用来发送模版邮件
*/</span>
<span class="token annotation punctuation">@Autowired</span>
<span class="token keyword">private</span> TemplateEngine templateEngine<span class="token punctuation">;</span>
<span class="token annotation punctuation">@Value</span><span class="token punctuation">(</span><span class="token string">"${spring.mail.username}"</span><span class="token punctuation">)</span>
<span class="token keyword">private</span> String from<span class="token punctuation">;</span>
<span class="token annotation punctuation">@Override</span>
<span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">send</span><span class="token punctuation">(</span>String to<span class="token punctuation">,</span> String subject<span class="token punctuation">,</span> String text<span class="token punctuation">)</span> <span class="token punctuation">{</span>
// SimpleMailMessage message = new SimpleMailMessage();
// message.setFrom(from);
// message.setTo(to);
// message.setSubject(subject);
// message.setText(text);
Context context <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Context</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
context<span class="token punctuation">.</span><span class="token function">setVariable</span><span class="token punctuation">(</span><span class="token string">"project"</span><span class="token punctuation">,</span> <span class="token string">"demo"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
context<span class="token punctuation">.</span><span class="token function">setVariable</span><span class="token punctuation">(</span><span class="token string">"author"</span><span class="token punctuation">,</span> <span class="token string">"yimcarson"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
context<span class="token punctuation">.</span><span class="token function">setVariable</span><span class="token punctuation">(</span><span class="token string">"code"</span><span class="token punctuation">,</span> text<span class="token punctuation">)</span><span class="token punctuation">;</span>
String emailContent <span class="token operator">=</span> templateEngine<span class="token punctuation">.</span><span class="token function">process</span><span class="token punctuation">(</span><span class="token string">"mail"</span><span class="token punctuation">,</span> context<span class="token punctuation">)</span><span class="token punctuation">;</span>
MimeMessage message <span class="token operator">=</span> mailSender<span class="token punctuation">.</span><span class="token function">createMimeMessage</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
MimeMessageHelper helper <span class="token operator">=</span> null<span class="token punctuation">;</span>
<span class="token keyword">try</span> <span class="token punctuation">{</span>
helper <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">MimeMessageHelper</span><span class="token punctuation">(</span>message<span class="token punctuation">,</span> <span class="token boolean">true</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
helper<span class="token punctuation">.</span><span class="token function">setFrom</span><span class="token punctuation">(</span>from<span class="token punctuation">)</span><span class="token punctuation">;</span>
helper<span class="token punctuation">.</span><span class="token function">setTo</span><span class="token punctuation">(</span>to<span class="token punctuation">)</span><span class="token punctuation">;</span>
helper<span class="token punctuation">.</span><span class="token function">setSubject</span><span class="token punctuation">(</span>subject<span class="token punctuation">)</span><span class="token punctuation">;</span>
helper<span class="token punctuation">.</span><span class="token function">setText</span><span class="token punctuation">(</span>emailContent<span class="token punctuation">,</span> <span class="token boolean">true</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span> <span class="token keyword">catch</span> <span class="token punctuation">(</span><span class="token class-name">MessagingException</span> e<span class="token punctuation">)</span> <span class="token punctuation">{</span>
e<span class="token punctuation">.</span><span class="token function">printStackTrace</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
mailSender<span class="token punctuation">.</span><span class="token function">send</span><span class="token punctuation">(</span>message<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
}
templates模版
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>yimcarson</title>
<style>
body {
text-align: center;
margin-left: auto;
margin-right: auto;
}
#main {
text-align: center;
position: absolute;
}
</style>
</head>
<body>
<div id="main">
<h3>Welcome <span th:text="${project}"></span> -By <span th:text=" ${author}"></span></h3>
Your Verification Code is
<h2><span th:text="${code}"></span></h2>
</div>
</body>
</html>
测试
import com.my.demo.Application;
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;
import org.thymeleaf.TemplateEngine;
import java.util.Date;
import java.util.UUID;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = {Application.class})
public class MailServiceTest {
@Autowired
private MailService mailService;
<span class="token annotation punctuation">@Test</span>
<span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">testSend</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
String to <span class="token operator">=</span> <span class="token string">"yimcarson@qq.com"</span><span class="token punctuation">;</span>
mailService<span class="token punctuation">.</span><span class="token function">send</span><span class="token punctuation">(</span>to<span class="token punctuation">,</span> <span class="token string">"模板邮件"</span><span class="token punctuation">,</span> UUID<span class="token punctuation">.</span><span class="token function">randomUUID</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">toString</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">toUpperCase</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
}
结语
163
QQ
这两个邮箱比较常用,至于Gmail
……,如果服务器是阿里云香港或者国外、亚马逊这些的话可以正常使用,否则……,Couldn't connect to host, port: smtp.gmail.com, 465; timeout -1
原文地址:https://blog.csdn.net/yimcarson/article/details/84936440
Spring Boot笔记之邮件(spring-boot-starter-mail)的更多相关文章
- spring入门笔记-(一)、spring boot HelloWorld
什么是spring boot Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程.该框架使用了特定的方式来进行配置,从而使开发人员 ...
- 【Spring学习笔记-0】Spring开发所需要的核心jar包
spring开发所需要的核心jar 1. libs目录下的核心jar包: 2. common-logging-xxx.jar 来自为知笔记(Wiz) 附件列表
- 【Spring学习笔记-MVC-2】spring导出Excel
说明: 1.结合Spring MVC实现Excel导出功能: 2. 在MVC配置文件中配置Excel视图解析器: 需要的jar包 以poi开头的jar包都是必须的 web.xml <?xml v ...
- 【Spring学习笔记-5】Spring中的抽象bean以及bean继承
*.hl_mark_KMSmartTagPinkImg{background-color:#ffaaff;}*.hl_mark_KMSmartTagBlueImg{background-color:# ...
- 【Spring学习笔记-MVC-16】Spring MVC之重定向-解决中文乱码
概述 spring MVC框架controller间跳转,需重定向,主要有如下三种: 不带参数跳转:形如:http://localhost:8080/SpringMVCTest/test/myRedi ...
- 【Spring学习笔记-MVC-15】Spring MVC之异常处理
作者:ssslinppp 1. 描述 在J2EE项目的开发中,不管是对底层的数据库操作过程,还是业务层的处理过程,还是控制层的处理过程,都不可避免会遇到各种可预知的.不可预知的异常需要处理 ...
- 【Spring学习笔记-MVC-14】Spring MVC对静态资源的访问
作者:ssslinppp 参考链接: http://www.cnblogs.com/luxh/archive/2013/03/14/2959207.html http://www.cnb ...
- 【Spring学习笔记-MVC-13】Spring MVC之文件上传
作者:ssslinppp 1. 摘要 Spring MVC为文件上传提供了最直接的支持,这种支持是通过即插即用的MultipartResolve实现的.Spring使用Jakarta Co ...
- 【Spring学习笔记-MVC-12】Spring MVC视图解析器之ResourceBundleViewResolver
场景 当我们设计程序界面的时候,中国人希望界面是中文,而美国人希望界面是英文. 我们当然希望后台代码不需改变,系统能够通过配置文件配置,来自己觉得是显示中文界面还是英文界面. 这是,Spring mv ...
随机推荐
- 关于sublime text2的一些问题(为解决)
1. 编写markdown文件后,如何转成pdf ? 2. 执行程序时,如何在控制台输入数据?
- Spring → 04:Bean(1)
一.Bean概念 Spring Bean是被实例的,组装的及被Spring 容器管理的Java对象. Spring 容器会自动完成@bean对象的实例化. 创建应用对象之间的协作关系的行为称为:装配( ...
- Directx11教程(67) 显示模型文件
原文:Directx11教程(67) 显示模型文件 在前面的教程中,我们都是通过在ModelClass中直接产生顶点和索引数据,简单的三角形,立方体等等还好说,毕竟比较简单,如何显示复杂的 ...
- 显示调用dll
原dll中导出的接口如图: Head.h: struct zint_render_line { float x, y, length, width; struct zint_render_line * ...
- CC-Debugger 最小调试接法
CC-Debugger 最小调试接法 以 CC2541 为例,最少需要四根 DD DC RST GND. 一般 VCC 目标调试板都有,所以这里你需要将 CC-Debugger 的 PIN 2 和 P ...
- oracle控制何时触发审计动作
1)By session / By Access by session对每个session中发生的重复操作只记录一次 by access对每个session中发生的每次操作都记录,而不管是否重复. 对 ...
- Leetcode830.Positions of Large Groups较大分组的位置
在一个由小写字母构成的字符串 S 中,包含由一些连续的相同字符所构成的分组. 例如,在字符串 S = "abbxxxxzyy" 中,就含有 "a", " ...
- oralce基本select语句
SELECT [DISTINCT] *|{column1,column2,column3. . .} FROM table l select指定查询哪些列的数据. l column指 ...
- 一维数组的求平均成绩 Day06
package com.sxt.arraytest1; /* * 求班里学生的平均成绩,以及成绩的综合 输出每个同学的成绩 */ import java.util.Arrays; import jav ...
- Calendar类实现当前日期的日历
package com.sxt.home1; import java.text.DateFormat; import java.text.ParseException; import java.tex ...