springboot3——Email
maven导入包:
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-mail</artifactId><version>2.1.6.RELEASE</version></dependency>
参数配置:
# MailProperties
spring.mail.host=smtp.sina.com
spring.mail.port=465
spring.mail.username=hebtu_ftz@sina.com
spring.mail.password=c33ce75afaff8015
spring.mail.protocol=smtps
spring.mail.properties.mail.smtp.ssl.enable=true
spring.mail.properties.mail.smtl.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true
spring.mail.properties.mail.smtp.starttls.required=true
写发送工具类:
package com.now.community.community.util;import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;@Component
public class MailClient {private static final Logger logger = LoggerFactory.getLogger(MailClient.class);@Autowiredprivate JavaMailSender mailSender;@Value("${spring.mail.username}")private String from;public void sendMail(String to, String subject, String content) {try {MimeMessage message = mailSender.createMimeMessage();MimeMessageHelper helper = new MimeMessageHelper(message);helper.setFrom(from);helper.setTo(to);helper.setSubject(subject);helper.setText(content, true);mailSender.send(helper.getMimeMessage());} catch (MessagingException e) {logger.error("发送邮件失败:" + e.getMessage());}}
}
测试类:
package com.now.community.community;import com.now.community.community.util.MailClient;
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.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;@RunWith(SpringRunner.class)
@SpringBootTest
@ContextConfiguration(classes = CommunityApplication.class)
public class MailTests {@Autowiredprivate MailClient mailClient;@Autowiredprivate TemplateEngine templateEngine;@Testpublic void testTextMail(){mailClient.sendMail("450427951@qq.com","TEST","welcome");}@Testpublic void testHtmlMail(){Context context=new Context();context.setVariable("username","fantianzuo");String content=templateEngine.process("/mail/demo",context);System.out.println(content);mailClient.sendMail("450427951@qq.com","HTML",content);}
}
html:
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head><meta charset="UTF-8"><title>邮件示例</title>
</head>
<body>
<p>欢迎你, <span style="color:red;" th:text="${username}"></span>!</p>
</body>
</html>
springboot3——Email相关推荐
- 【大总结2】大学两年,写了这篇几十万字的干货总结
本文是我大学两年知识的总结.涵盖数据结构.算法.语言基础.操作系统.关系数据库.NOSQL.网络/前端/项目基础知识.安全和测试.框架的学习.中间件和工具.设计模式和框架原理.我推荐的资料.我的建议 ...
- springboot3 笔记(雷峰阳)
文章目录 springboot3核心特性 SpringBoot3-快速入门 环境要求 SpringBoot是什么 快速体验 1. 开发流程 1. 创建项目 2. 导入场景 3.主程序 4. 业务 5. ...
- 解决gitte提交报错 error: GE007: Your push would publish a private email address.
错误情况: gitte提交报错,无法提交上去,报错如下: remote: Powered by GITEE.COM [GNK-6.0] remote: error: GE007: Your push ...
- 正则表达式用户名密码电话身份证Email使用
月末了,这个月才写了2遍文章,对自己略感失望了,最近是有些忙,等闲些日子后,再整理一些文章分享给大家! 这遍是关于正则表达式,因为写项目时要用到正则表达式,所以就学习了下,另写一遍文章,方便记忆! 1 ...
- remote: error: GH007: Your push would publish a private email address.
最近,在学习Github的过程中遇到的问题:remote: error: GH007: Your push would publish a private email address. 使用命令:gi ...
- Setting the Reply-To Header in an Email using CDONTS.NewMail Object and CDO Message
代码 1 <% 2 Option Explicit 3 4 Dim objMail 5 Dim strSubject 6 Dim strBody 7 8 strSubject ...
- asp.net实现在网页上自动显示超链接以及Email地址
人们总喜欢在帖子中加上各种有用的URL链接或Email地址.而笔者当初设计时没有考虑到这一点,使得这些URL链接或Email地址只能以文字的形式而并不是以超链接的形式显示,其它浏览帖子的人还必须把这些 ...
- EMAIL发送系统(C#+基于SMTP认证) 2.0
这个是对于 EMAIL发送系统(C#+基于SMTP认证) 1.0 的改版这个第一版是2002年11月写的,过了一年半了,才有人提意见,就修正了一下,因为后来做的项目一直用不上,也就没有注意到 前段时间 ...
- ASP.NET结合COM组件发送Email
在开发电子邮件发送程序的时候,我们经常需要使用到相应的组件,其实不需要第三方的组件(例如:Jmail)照常可以做到发送Email的功能. 在系统目录(如c:/winnt或c:/windows)的sys ...
最新文章
- basicLSTMCELL() num_units参数代表了LSTM输出向量的维数
- css transform旋转属性
- SAP ECC6.0内存参数调整和调优
- python 查询文本文件的层次
- step3 . day2 数据结构之线性表链表
- linux配置dhcp中继服务
- matlab运行支持向量机不出f,求助各位大神关于libsvm,svmpredict总是出不来结果,调试了好久还是不行...
- javascript小技巧JavaScript[对象.属性]集锦
- Soft NMS算法笔记
- 计算机三级嵌入式系统考试之矩阵键盘
- 《Cisco安全防火墙服务模块(FWSM)解决方案》——2.7 软件架构
- Sublime 自定义格式化快捷键
- 石家庄地铁站项目最终总结报告
- Scala折叠(fold)
- LeetCode——1931. 用三种不同颜色为网格涂色(Painting a Grid With Three Different Colors)困难]——分析及代码(Java)
- 色彩搭配 — 总结1
- git提交代码出现 fatal: The remote end hung up unexpectedly-错误处理
- 混合云管平台排名您知道吗?看这里
- String截取指定字符前面(后面)所有字符和String的常用方法
- linux服务器用lighttpd+mysql5+php5+SupeSite/X-Space+discuz构建社区门户
热门文章
- UE4 远程调用函数
- INTEL和AMD两大巨头的前身
- 修改Linux内核的启动Logo和禁用启动光标
- python的map怎么用_python中的map怎么使用
- golang调用java的函数_大话golang性能分析(一):profile基本原理
- angular4输入有效性_Angular 2 用户输入
- python编写界面遍历_python和pywin32实现窗口查找、遍历和点击的示例代码
- 中国大学慕课计算机专业导论,2015秋计算机专业导论(大连大学)
- hive sql 怎么实现循环_Hive存储过程实现-hpsql
- 【转】C#实现SM2国密加密