计算机网站建设/今日桂林头条新闻

计算机网站建设,今日桂林头条新闻,上海800做网站,做动感影集的网站目录 1、创建一个基本的SpringBoot项目,pom文件导入发送邮件的依赖 2、application.yml 文件配置配置邮件发送信息 3、创建IEmailService 接口文件,定义邮件发送的接口 4、创建IEmailService接口的实现类EmailService.java 文件 5、新建邮件发送模板 ema…

      d0543ce9e311620ce89e2ac606383ea2.png        

目录

1、创建一个基本的SpringBoot项目,pom文件导入发送邮件的依赖

2、application.yml 文件配置配置邮件发送信息

3、创建IEmailService 接口文件,定义邮件发送的接口

4、创建IEmailService接口的实现类EmailService.java 文件

5、新建邮件发送模板 email.html

6、新建测试类,主要代码如下

7、效果截图


邮件发送功能基本是每个完整业务系统要集成的功能之一,今天小编给大家介绍一下SpringBoot实现邮件发送功能,希望对大家能有所帮助!

今天主要给大家分享简单邮件发送、HTML邮件发送、包含附件的邮件发送三个例子,具体源码链接在文章末尾,有需要的朋友可以自己下载学习一下。

1、创建一个基本的SpringBoot项目,pom文件导入发送邮件的依赖

<!--邮件发送依赖包-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
<!--freemarker制作Html邮件模板依赖包-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>

2、application.yml 文件配置配置邮件发送信息

spring:
mail:
host: smtp.qq.com
username: xxx@qq.com  #发件人邮箱
password: xxxxx  #授权码
protocol: smtp
properties.mail.smtp.auth:true
properties.mail.smtp.port:465#发件邮箱端口
properties.mail.display.sendmail: xiaoMing
properties.mail.display.sendname: xiaoming
properties.mail.smtp.starttls.enable:true
properties.mail.smtp.starttls.required:true
properties.mail.smtp.ssl.enable:true#是否启用ssl
default-encoding: utf-8#编码格式    
freemarker:
cache:false
settings:
classic_compatible:true
suffix: .html
charset: UTF-8
template-loader-path: classpath:/templates/

3、创建IEmailService 接口文件,定义邮件发送的接口

package com.springboot.email.email.service;import javax.mail.MessagingException;
import java.util.List;public interface IEmailService {/*** 发送简单文本邮件*/void sendSimpleMail(String receiveEmail, String subject, String content);/*** 发送HTML格式的邮件*/void sendHtmlMail(String receiveEmail, String subject, String emailContent) throws MessagingException;/*** 发送包含附件的邮件*/void sendAttachmentsMail(String receiveEmail, String subject, String emailContent, List<String> filePathList) throws MessagingException;
}

4、创建IEmailService接口的实现类EmailService.java 文件

package com.springboot.email.email.service.impl;import com.springboot.email.email.service.IEmailService;
import org.springframework.beans.factory.annotation.Value;
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 javax.annotation.Resource;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;
import java.util.List;
@Service
public class EmailServiceImpl implements IEmailService {@Resourceprivate JavaMailSender mailSender;@Value("${spring.mail.username}")private String fromEmail;/*** 发送简单文本邮件*/public void sendSimpleMail(String receiveEmail, String subject, String content) {SimpleMailMessage message = new SimpleMailMessage();message.setFrom(fromEmail);message.setTo(receiveEmail);message.setSubject(subject);message.setText(content);mailSender.send(message);}/*** 发送Html格式的邮件*/public  void sendHtmlMail(String receiveEmail,String subject,String emailContent) throws MessagingException{init(receiveEmail, subject, emailContent, mailSender, fromEmail);}public static void init(String receiveEmail, String subject, String emailContent, JavaMailSender mailSender, String fromEmail) throws MessagingException {MimeMessage message= mailSender.createMimeMessage();MimeMessageHelper helper=new MimeMessageHelper(message,true);helper.setFrom(fromEmail);helper.setTo(receiveEmail);helper.setSubject(subject);helper.setText(emailContent,true);mailSender.send(message);}/*** 发送包含附件的邮件*/public void sendAttachmentsMail(String receiveEmail, String subject, String emailContent, List<String> filePathList) throws MessagingException {MimeMessage message = mailSender.createMimeMessage();//带附件第二个参数trueMimeMessageHelper helper = new MimeMessageHelper(message, true);helper.setFrom(fromEmail);helper.setTo(receiveEmail);helper.setSubject(subject);helper.setText(emailContent, true);//添加附件资源for (String item : filePathList) {FileSystemResource file = new FileSystemResource(new File(item));String fileName = item.substring(item.lastIndexOf(File.separator));helper.addAttachment(fileName, file);}//发送邮件mailSender.send(message);}
}

5、新建邮件发送模板 email.html

<!DOCTYPE html>
<html>
<head lang="en"><meta charset="UTF-8"/><title></title><style>td {border: black 1px solid;}</style>
</head>
<body>
<h1>工资条</h1>
<table style="border: black 1px solid;width: 750px"><thead><td>序号</td><td>姓名</td><td>基本工资</td><td>在职天数</td><td>奖金</td><td>社保</td><td>个税</td><td>实际工资</td></thead><tbody><tr><td>${salary.index}</td><td>${salary.name}</td><td>${salary.baseSalary}</td><td>${salary.inDays}</td><td>${salary.reward}</td><td>${salary.socialSecurity}</td><td>${salary.tax}</td><td>${salary.actSalary}</td></tr></tbody>
</table>
</body>
</html>

6、新建测试类,主要代码如下

/*** 测试简单文本文件*/
@Test
public void EmailTest() {emailService.sendSimpleMail("hgmyz@outlook.com", "测试邮件", "springboot 邮件测试");
}@Test
public void HtmlEmailTest() throws MessagingException {String receiveEmail = "hgmyz@outlook.com";String subject = "Spring Boot 发送Html邮件测试";String emailContent = "<h2>您好!</h2><p>这里是一封Spring Boot 发送的邮件,祝您天天开心!<img " + "src='https://p3.toutiaoimg.com/origin/tos-cn-i-qvj2lq49k0/a43f0608912a4ecfa182084e397e4b81?from=pc' width='500' height='300' /></p>" + "<a href='https://programmerblog.xyz' title='IT技术分享设社区' targer='_blank'>IT技术分享设社区</a>";emailService.sendHtmlMail(receiveEmail, subject, emailContent);
}@Test
public void templateEmailTest() throws IOException, TemplateException, MessagingException {String receiveEmail = "hgmyz@outlook.com";String subject = "Spring Boot 发送Templete邮件测试";//添加动态数据,替换模板里面的占位符SalaryVO salaryVO = new SalaryVO(1, "小明", 2, 9000.00, 350.06, 280.05, 350.00, 7806.00);Template template = freeMarkerConfigurer.getConfiguration().getTemplate("email.html");//将模板文件及数据渲染完成之后,转换为html字符串Map<String, Object> model = new HashMap<>();model.put("salary", salaryVO);String templateHtml = FreeMarkerTemplateUtils.processTemplateIntoString(template, model);emailService.sendHtmlMail(receiveEmail, subject, templateHtml);
}@Test
public void emailContailAttachmentTest() throws IOException, TemplateException, MessagingException {String receiveEmail = "hgmyz@outlook.com";String subject = "Spring Boot 发送包含附件的邮件测试";//添加动态数据,替换模板里面的占位符SalaryVO salaryVO = new SalaryVO(1, "小王", 2, 9000.00, 350.06, 280.05, 350.00, 7806.00);Template template = freeMarkerConfigurer.getConfiguration().getTemplate("email.html");//将模板文件及数据渲染完成之后,转换为html字符串Map<String, Object> model = new HashMap<>();model.put("salary", salaryVO);String templateHtml = FreeMarkerTemplateUtils.processTemplateIntoString(template, model);List<String> fileList = new ArrayList<>();fileList.add("F:\\邮件测试.docx");fileList.add("F:\\5.png");fileList.add("F:\\db.jpg");emailService.sendAttachmentsMail(receiveEmail, subject, templateHtml, fileList);
}

7、效果截图

简单文版邮件

       67b287e79e92ae7f6b6e155ca16c6ae8.png        

html文件

       899417287aa1967d3cb1b8b2f73f7220.png        

包含附件的邮件

       687a355957514ac1cfbe4ea65371b6af.png        

Gitee地址:https://gitee.com/hgm1989/springboot-email.git

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/270085.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

电脑技巧:Win10无线投屏功能介绍

Win10操作系统可以将电脑中的内容投屏到其他显示设备&#xff0c;比如将电脑屏幕投屏到电视上&#xff0c;这是通过Miracast技术来实现的。其实Win10电脑自身也可以作为被投屏的那一方&#xff01;比如可以将手机屏幕投屏到电脑屏幕上去&#xff0c;方便给客户演示APP功能或者将…

电脑技巧:Win10系统中的这六种模式介绍

目录 1. 夜间模式 2. 游戏模式 3. 飞行模式 4. 卓越模式 5. 专注模式 6. 平板模式 在Win10中&#xff0c;隐藏着很多不为人知的小秘密。有些小功能虽然看起来不起眼&#xff0c;但关键时候却能让我们的Win10跑得更爽&#xff0c;比如win10中六种不同的模式&#xff0c;该什么时…

电脑卡顿,最先升级这个硬件,运行速度可快速提升

电脑用着用着就变慢了&#xff0c;不少人为之苦恼。有钱的人儿早早换上了新电脑&#xff0c;没钱的人儿仍然在苦苦地支撑着~ 但是&#xff0c;电脑卡顿就跟汽车变速箱坏了一样&#xff0c;我们可以为其更换零件&#xff0c;从而“治好了”它&#xff0c;使用寿命不就延长了吗&a…

Java技术:SpringBoot集成FreeMarker生成word文件

今天给大家分享SpringBoot集成FreeMarker模板引擎生成word文件的用法&#xff0c;感兴趣的可以学一下&#xff0c;完整源码地址在文章末尾处&#xff0c;欢迎互相沟通交流&#xff01; 一、什么是FreeMarker&#xff1f; FreeMarker 是一款开源的模板引擎&#xff1a;是一种基于…

电脑技巧:Win10自带远程控制软件介绍

Win10本身就自带了远程协助功能&#xff01;这在Win10中叫做“快速助手”&#xff0c;你是不是从来没有注意到呢&#xff1f; Win10的快速助手使用非常简单。通过搜索功能&#xff0c;直接就可以找到它。 通过搜索可以找到快速助手 说明&#xff1a;快速助手使用的是Windows的远…

网络技巧:手机信号满格,上网却很慢,教你关闭双频优选开关,网速飞快

目前大部分家里都安装了宽带&#xff0c;大家最常用的就是百兆千兆的宽带网络&#xff0c;但是也避免不了家里面使用WiFi上网的时候&#xff0c;网络会时好时坏&#xff0c;有些时候就算是我们手机信号显示的是满格&#xff0c;但是上网的速度也还是很慢&#xff0c;今天小编就…

玩转HTML5+跨平台开发[4] HTML表格标签

表格标签 在过去表格标签用的非常非常的多, 绝大多数的网站都是使用表格标签来制作的, 也就是说表格标签是一个时代的代表 http://2004.sina.com.cn作用:以表格形式将数据显示出来, 当数据量非常大的时候, 表格这种展现形式被认为是最为清晰的一种展现形式格式: table定义表格t…

硬件知识:如何快速挑选一款好的固态硬盘?

固态硬盘作为目前旗舰电脑必备的存储设备&#xff0c;可以大幅度提高电脑的运行速度&#xff0c;拥有一块好的固态硬盘&#xff0c;还是十分有必要的&#xff0c;今天小编给大家分享如何挑选一款好的固态硬盘&#xff0c;希望对大家能有所帮助&#xff01; 1、跑分 大家可以直…

你知道CDN是什么吗?本文带你搞明白CDN

最近在了解边缘计算&#xff0c;发现我们经常听说的CDN也是边缘计算里的一部分。那么说到CDN&#xff0c;好像只知道它中文叫做内容分发网络。那么具体CDN的原理是什么&#xff1f;能够为用户在浏览网站时带来什么好处呢&#xff1f;解决这两个问题是本文的目的。 CDN概念 CD…

Swift - 警告提示框(UIAlertController)的用法

import UIKitclass ViewController: UIViewController {override func viewDidLoad() {super.viewDidLoad()}override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {// 创建let alertController UIAlertController(title: "提示",…

运维笔记:Windows下如何实现nginx服务自启动

目录 下载Windows Service Wrapper nginx-service.exe 拷贝到nginx的根目录当中 创建配置文件 管理员身份运行安装Nginx服务命令 Nginx作为有每一个两三年工作经验的程序员来说都不陌生&#xff0c;基本上每个线上部署的项目都需要用到&#xff0c;Nginx常用的功能有负载均衡、…

你知道三地五中心吗

两地三中心这个架构&#xff0c;如下图&#xff1a; 这种架构具备容灾能力&#xff0c;比如生产数据中心停电了&#xff0c;那么可以把所有流量都切到同城灾备中心或异地灾备中心&#xff0c;那么现在的问题是假如真到了停电的那一天&#xff0c;你敢把所有的流量都切到灾备中心…

Win10操作系统隐藏6个实用小功能

目录 功能一、分屏 功能二、录屏 功能三、截图 功能四、便签功能 功能五、视频剪辑 功能六、计算器 功能一、分屏 Win10操作系统其实是自带分屏功能的&#xff0c;这个功能对我来说真的太喜欢了&#xff0c;尤其是核对文档的时候&#xff0c;真的是太方便了&#xff01; 操作方…

面试一口气说出Spring的声明式事务@Transactional注解的6种失效场景

一、Spring事务管理的两种方式 事务管理在系统开发中是不可缺少的一部分&#xff0c;Spring提供了很好事务管理机制&#xff0c;主要分为编程式事务和声明式事务两种。 编程式事务&#xff1a;是指在代码中手动的管理事务的提交、回滚等操作&#xff0c;代码侵入性比较强&…

JAVA断点调试

1、条件断点&#xff0c;点击添加条件 2、异常断点&#xff0c;点击添加异常 转载于:https://www.cnblogs.com/binbang/p/6378897.html