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 是一款全新的轻量级应用服务器,它将用户的良好开发体验作为最主要的出发点.其主要特点和内容包括: 高模块化--该功能允许 ...
随机推荐
- RabbitMQ生产者消费者模型构建(三)
ConnectionFactory:获取连接(地址,端口号,用户名,密码,虚拟主机等) Connection:一个连接 Channel:数据通信信道,可发送.接收消息 Queue:具体的消息存储队列 ...
- 网站名,服务器名,url,ip,域名的区别和联系。
平时我们可能容易混淆这几个名词含义,今天我打算捋一捋这几个概念. 我们知道,两台计算机要想互相通信,就像古代写信一样,地址必须要唯一的,不然就会出错.计算机之间通信也是一样的,要保证计算机的地址的唯一 ...
- CentOS-7.4(1708)release notes发行注记
Red Hat Enterprise Linux 当前的最新版本是 7.3. Red Hat Enterprise Linux 7 当前仅支持 64 位CPU:64-bit AMD.64-bit In ...
- R python在无图形用户界面时保存图片
在用python的matplotlib,和R中自带的作图,如果想保存图片时,当你有图形用户界面时是没有问题的,但是当没有图形用户界面时,会报错: 在R中,解决办法: https://blog.csdn ...
- Chrome开发小技巧--浏览器控制台现写并运行js代码--snippets
想简单等运行一段js代码,以前可能会新建一个html 里面包含script标签,或者引入一个js,然后chrome浏览器打开.这样很麻烦. 想再console控制台写,也不方便,换行处理麻烦. 基于在 ...
- How to resolve error “Failed to resolve: org.jetbrains.kotlin:kotlin-stdlib-jre7…” when building in Android Studio Ask Question
//implementation"org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version" implementation & ...
- redis连接报错:MISCONF Redis is configured to save RDB snapshots, but it is currently not able to...
连接redis报错: MISCONF Redis is configured to save RDB snapshots, but it is currently not able to persis ...
- 2019/11/09 TZOJ
1001 Interesting Integers http://www.tzcoder.cn/acmhome/problemdetail.do?&method=showdetail& ...
- [Linux] 026 光盘 yum 源搭建
光盘 yum 搭建步骤 (1) 挂载光盘 $ mount /dev/cdrom /mnt/cdrom/ (2) 让网络 yum 源文件失效 $ cd /etc/yum.repos.d/ $ mv Ce ...
- css画百分比圆环
html: <div class="circle"> <div class="percent-circle percent-circle-left&qu ...