10.整合email
整合email
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
<!--freemarker-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
spring.mail.host=smtp.qq.com
spring.mail.username=xxx@qq.com
# 授权码 qq邮箱->设置->账户->pop3/SMTP服务
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
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
public class EmailConfig {
@Value("${spring.mail.username}")
private String emailFrom;
public String getEmailFrom() {
return emailFrom;
}
public void setEmailFrom(String emailFrom) {
this.emailFrom = emailFrom;
}
}
import freemarker.template.Template;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;
import javax.mail.internet.MimeMessage;
import java.io.File;
import java.util.HashMap;
import java.util.Map;
@Service
public class EmailService {
@Autowired
private EmailConfig emailConfig;
@Autowired
private JavaMailSender javaMailSender;
@Autowired
private FreeMarkerConfigurer freeMarkerConfigurer;
public void sendSimpleMail(String sendTo,String subject,String content){
SimpleMailMessage mailMessage = new SimpleMailMessage();
mailMessage.setFrom(emailConfig.getEmailFrom());
mailMessage.setTo(sendTo);
mailMessage.setSubject(subject);
mailMessage.setText(content);
javaMailSender.send(mailMessage);
}
//发送带附件的邮件
public void sendAttachmentMail(String sendTo, String subject, String content, File file){
MimeMessage mimeMessage = javaMailSender.createMimeMessage();
try {
MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
helper.setFrom(emailConfig.getEmailFrom());
helper.setTo(sendTo);
helper.setSubject(subject);
helper.setText(content);
helper.addAttachment("附件",new FileSystemResource(file));//文件名,文件
} catch (Exception e) {
e.printStackTrace();
}
javaMailSender.send(mimeMessage);
}
//发送模版邮件
//这里用的是freemarker
public void sendTemplateMail(String sendTo, String subject, String content, String templateName){
MimeMessage mimeMessage = javaMailSender.createMimeMessage();
try {
MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
helper.setFrom(emailConfig.getEmailFrom());
helper.setTo(sendTo);
helper.setSubject(subject);
Map<String,Object> model = new HashMap<>();
model.put("name",content);
Template template = freeMarkerConfigurer.getConfiguration().getTemplate(templateName);
String html = FreeMarkerTemplateUtils.processTemplateIntoString(template, model);
helper.setText(html,true);
} catch (Exception e) {
e.printStackTrace();
}
javaMailSender.send(mimeMessage);
}
}
测试
import com.app.SpringDemoApp;
import com.fly.email.EmailService;
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.SpringJUnit4ClassRunner;
import java.io.File;
@SpringBootTest(classes = SpringDemoApp.class)
@RunWith(SpringJUnit4ClassRunner.class)
public class EmailServiceTest {
@Autowired
private EmailService emailService;
@Test
public void test1(){
emailService.sendSimpleMail("xxx@qq.com","test","test email");
}
@Test
public void test2(){
File file = new File("src/main/resources/banner.txt");
emailService.sendAttachmentMail("xxx@qq.com","test","test email2",file);
}
@Test
public void test3(){
emailService.sendTemplateMail("xxx@qq.com","test","test email3","index.ftl");
}
}
templates/index.ftl
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>index</title>
</head>
<body>
<h4 style="color: red">${name}</h4>
</body>
</html>
10.整合email的更多相关文章
- SpringMVC:学习笔记(10)——整合Ckeditor且实现图片上传
SpringMVC:学习笔记(10)——整合Ckeditor且实现图片上传 配置CKEDITOR 精简文件 解压之后可以看到ckeditor/lang下面有很多语言的js,如果不需要那么多种语言的,可 ...
- eclipse spring4 ehache2.10 整合
http://blog.csdn.net/tonytfjing/article/details/39251507 http://my.oschina.net/duoduo3369/blog/17392 ...
- SpringBoot: 10.整合mybatis(转)
需求:通过使用 SpringBoot+SpringMVC+MyBatis 整合实现一个对数据库中的 t_user 表的 CRUD 的操作 1.创建maven项目,添加项目所需依赖 <!--spr ...
- SpringBoot2.x整合Email并利用AOP做一个项目异常通知功能
因为不知aop能干嘛,因此用aop做个小功能,再结合最近学的springboot-Email做了个系统异常自动邮件通知的功能, 感觉满满的成就感. AOP不懂的可以看上一篇:https://www.c ...
- SpringBoot整合Email(电子邮件服务)
(1).导入starter依赖 <dependency> <groupId>org.springframework.boot</groupId> <artif ...
- springcloud系列10 整合Hystrix遇到的坑:
首先配置类: @Bean public ServletRegistrationBean getServlet(){ HystrixMetricsStreamServlet streamServlet ...
- springboot笔记10——整合Redis
依赖 <dependencies> <!--web依赖--> <dependency> <groupId>org.springframework.boo ...
- S2SH框架整合(注解)Struts2+Spring+Hibernate+MySql
整合简介 Struts2+Spring4+hibernate4整合,Maven管理jar包,Eclipse工具.注解方式 架构截图 1.Spring整合Hibernate 1.1.创建Hibern ...
- liberty | 在IDEA整合Springboot与IBM liberty
在IDEA整合Springboot与IBM liberty 简介 Liberty 是一款全新的轻量级应用服务器,它将用户的良好开发体验作为最主要的出发点.其主要特点和内容包括: 高模块化--该功能允许 ...
随机推荐
- Linux下安装Harbor 1.8.0 仓库的安装和使用(亲测)
根据Harbor官方描述: Harbor是一个用于存储和分发Docker镜像的企业级Registry服务器,通过添加一些企业必需的功能特性,例如安全.标识和管理等,扩展了开源Docker Distri ...
- 第一周训练 | STL和基本数据结构
A - 圆桌问题: HDU - 4841 #include<iostream> #include<vector> #include<stdio.h> #includ ...
- 31 October
https://www.cnblogs.com/RabbitHu/p/51nod1353.html 树形 DP 求所有联通块 \(\ge K\) 的方案数. 切断:\(\forall i\in\lef ...
- ::before和::after的详细介绍
原文传送门: https://www.cnblogs.com/staro... 一.介绍 css3为了区分伪类和伪元素,伪元素采用双冒号写法. 常见伪类--:hover,:link,:active,: ...
- 判断逻辑 先判断协议字段返回,再判断业务返回,最后判断交易状态 API密钥
[微信支付]微信小程序支付开发者文档 https://pay.weixin.qq.com/wiki/doc/api/wxa/wxa_api.php?chapter=4_1 协议规则 商户接入微信支付, ...
- composer的自动加载机制(autoload)
composer的出现真是让人们眼前一亮,web开发从此变成了一件很『好玩』的事情,开发一个CMS就像在搭积木,从packagist中取出『积木』搭建在自己的代码中,一点一点搭建出一个属于自己的王国. ...
- tp 框架 -文件上传
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 <?ph ...
- Learn Python the hard way, ex39 列表的操作
#!/usr/bin/python #coding:utf-8 ten_things = "apples oranges crows telephone light sugar" ...
- Java thread(3)
线程间的调度策略 通常是选择优先级高的线程,但是若发生以下情况则终止线程的运行: 1 调用yield 让出对cpu的占用权. 2 调用sleep 3 线程由于I/O操作而受阻 4 更高优先级的线 ...
- vue构造器注册UI组件
import ConfirmComponent from '../../components/confirm/index' import { mergeOptions } from '../plugi ...