完成Springboot配置发件邮箱,自动给其他邮箱发送邮件功能

一、创建springboot基础项目,引入依赖

<!-- Spring Boot 邮件依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
<!-- Spring Boot 模板依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

二、配置文件添加必要的配置:

#qq邮箱为例
spring.mail.host=smtp.qq.com

spring.mail.port=465

spring.mail.username=89666XXX@qq.com

spring.mail.password=XXXXXX  #不是邮件密码而是授权码(邮箱设置里获取)

spring.mail.protocol=smtp

spring.mail.test-connection=true

spring.mail.default-encoding=UTF-8

spring.mail.properties.mail.smtp.auth=true

spring.mail.properties.mail.smtp.starttls.enable=true

spring.mail.properties.mail.smtp.starttls.required=true

spring.mail.properties.mail.smtp.ssl.enable=true

spring.mail.properties.mail.display.sendmail=spring-boot-demo

三、工程结构

四、MailService接口
package com.example.mail.demo;

import javax.mail.MessagingException;

public interface MailService {
/**
* 发送文本邮件
*
* @param to 收件人地址
* @param subject 邮件主题
* @param content 邮件内容
* @param cc 抄送地址
*/
void sendSimpleMail(String to, String subject, String content, String... cc); /**
* 发送HTML邮件
*
* @param to 收件人地址
* @param subject 邮件主题
* @param content 邮件内容
* @param cc 抄送地址
* @throws MessagingException 邮件发送异常
*/
void sendHtmlMail(String to, String subject, String content, String... cc) throws MessagingException; /**
* 发送带附件的邮件
*
* @param to 收件人地址
* @param subject 邮件主题
* @param content 邮件内容
* @param filePath 附件地址
* @param cc 抄送地址
* @throws MessagingException 邮件发送异常
*/
void sendAttachmentsMail(String to, String subject, String content, String filePath, String... cc) throws MessagingException; }
五、MailServiceImpl实现
package com.example.mail.demo;

import org.springframework.beans.factory.annotation.Autowired;
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.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File; @Service
public class MailServiceImpl implements MailService { @Autowired
private JavaMailSender mailSender;
@Value("${spring.mail.username}")
private String from; /**
* 发送文本邮件
*
* @param to 收件人地址
* @param subject 邮件主题
* @param content 邮件内容
* @param cc 抄送地址
*/
@Override
public void sendSimpleMail(String to, String subject, String content, String... cc) {
SimpleMailMessage message = new SimpleMailMessage();
message.setFrom(from);
message.setTo(to);
message.setSubject(subject);
message.setText(content);
if (cc != null) {
message.setCc(cc);
}
mailSender.send(message);
} /**
* 发送HTML邮件
*
* @param to 收件人地址
* @param subject 邮件主题
* @param content 邮件内容
* @param cc 抄送地址
* @throws MessagingException 邮件发送异常
*/
@Override
public void sendHtmlMail(String to, String subject, String content, String... cc) throws MessagingException {
MimeMessage message = mailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(message, true);
helper.setFrom(from);
helper.setTo(to);
helper.setSubject(subject);
helper.setText(content, true);
if (cc != null) {
helper.setCc(cc);
}
mailSender.send(message);
} /**
* 发送带附件的邮件
*
* @param to 收件人地址
* @param subject 邮件主题
* @param content 邮件内容
* @param filePath 附件地址
* @param cc 抄送地址
* @throws MessagingException 邮件发送异常
*/
@Override
public void sendAttachmentsMail(String to, String subject, String content, String filePath, String... cc) throws MessagingException {
MimeMessage message = mailSender.createMimeMessage(); MimeMessageHelper helper = new MimeMessageHelper(message, true);
helper.setFrom(from);
helper.setTo(to);
helper.setSubject(subject);
helper.setText(content, true);
if (cc != null) {
helper.setCc(cc);
}
FileSystemResource file = new FileSystemResource(new File(filePath)); String fileName = new File(filePath).getName();
System.out.println("fileName:"+fileName);
helper.addAttachment(fileName, file); mailSender.send(message);
} }
六、EmailController控制类
package com.example.mail.demo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;
import javax.mail.MessagingException;
import java.io.File;
import java.net.URL; @Controller
public class EmailController { @Autowired
private MailService mailService; /**
* 测试发送简单邮件
*/
@GetMapping("/testSendSimpleMail")
@ResponseBody
public String sendSimpleMail() {
mailService.sendSimpleMail("2431886055@qq.com", "这是一封简单邮件", "这是一封普通的SpringBoot测试邮件");
return "ok";
} @Autowired
private TemplateEngine templateEngine; /**
* 发送html邮件
* @return
* @throws MessagingException
*/
@GetMapping("/sendHtmlMail")
@ResponseBody
public String sendHtmlMail() throws MessagingException {
Context context = new Context();
context.setVariable("project", "Spring Boot email");
context.setVariable("author", "万笑佛");
context.setVariable("url", "https://www.cnblogs.com/yclh/");
String emailTemplate = templateEngine.process("welcome", context);
mailService.sendHtmlMail("2431886055@qq.com", "这是一封模板HTML邮件", emailTemplate);
return "ok";
} /**
* 发送带附件的邮件
* @throws MessagingException
*/
@GetMapping("/sendAttachmentsMail")
@ResponseBody
public String sendAttachmentsMail() throws MessagingException {
URL url = null;
try {
// 创建一个文件对象
File file = new File("D:\\IDEANew\\45Mail\\src\\main\\resources\\static\\index.html");
// 将文件对象转换为URI
url = file.toURI().toURL();
// 输出URL
System.out.println("文件的URL: " + url);
} catch (Exception e) {
e.printStackTrace();
}
System.out.println(url.getPath());
mailService.sendAttachmentsMail("2431886055@qq.com", "这是一封带附件的邮件", "邮件中有附件,请注意查收!", url.getPath());
return "ok";
} }
七、postman测试
(1)第一个简单邮件

效果

(2)第二个带模板的邮件

效果

(3)第三个带附件的邮件

效果

 

Springboot 自动发送邮件的更多相关文章

  1. 【C#】新建服务自动发送邮件

    ---windows服务,---自动发送邮件 邮件发送code #region 发送邮件函数 public void SendMailUseZj() { System.Net.Mail.MailMes ...

  2. Jenkins配置自动发送邮件,成功!

    Jenkins自动发送邮件配置: 打开"系统管理"--"系统设置" 在"Jenkins Location"设置系统管理员地址(重要:不能省略 ...

  3. python+selenium生成测试报告后自动发送邮件

    标签(空格分隔): 自动化测试 运行自动化脚本后,会产生测试报告,而将测试报告自动发送给相关人员,能够让对方及时的了解测试情况,查看测试结果. 整个脚本包括三个部分: 生成测试报告 获取最新的测试报告 ...

  4. VBA控制outlook自动发送邮件(转)

    使用Excel VBA实现Outlook自动发送邮件 | 在工作上我们都会遇到批量发送邮件的情况,面对重复而规律性的工作,可以使用Excel的VBA实现自动批量化发送邮件.大大减小工作时间,提升工作效 ...

  5. Jenkins进阶之自动发送邮件的Default Content设置模板

    分享一个简洁实用的Jenkins项目邮件管理系统的"Default Content"设置模板 配置如下: <h1><center><font colo ...

  6. VBS 自动发送邮件

    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 35 36 3 ...

  7. python☞自动发送邮件

    一.SMTP 协议 SMTP(Simple Mail Transfer Protocol)是简单邮件传输协议,它是一组用于由源地址到目的地址传送邮件的规则,由它来控制信件的中转方式 二.smtplib ...

  8. jenkins配置自动发送邮件,抄送

    1.安装插件.系统管理-安装插件:可选插件:搜索Email Extension 2.设置全局变量.系统管理-系统设置:a.Jenkins Location 设置发送方邮件--- b.Extended ...

  9. 【转】2、Jenkins构建完成自动发送邮件

    1.开通163邮箱的授权码服务,和SMTP服务.百度找教程.2.安装 Email Extension Plugin 插件,已安装或版本自带可跳过此步骤.3.进入系统管理–系统设置首先配置 Jenkin ...

  10. SpringBoot自动配置源码调试

    之前对SpringBoot的自动配置原理进行了较为详细的介绍(https://www.cnblogs.com/stm32stm32/p/10560933.html),接下来就对自动配置进行源码调试,探 ...

随机推荐

  1. 在Java中List, Set, Map是否继承自Collection接口?

    List和Set是继承自Collection接口的接口,Set不允许重复的项目,List允许重复项目, Set接口派生的类有TreeSet,HashSet,LinkedHashSet. List接口派 ...

  2. 更快的训练和推理: 对比 Habana Gaudi®2 和英伟达 A100 80GB

    通过本文,你将学习如何使用 Habana Gaudi2 加速模型训练和推理,以及如何使用 Optimum Habana 训练更大的模型.然后,我们展示了几个基准测例,包括 BERT 预训练.Stabl ...

  3. UI自动化项目1说明 | 网页计算器自动化测试项目

    需求: 1.对网页计算器, 进行加法的测试操作. 通过读取数据文件中的数据来执行用例. 2.网址: http://cal.apple886.com/ 测试点: 1.加法:1+1=2 2+9!=10 . ...

  4. 万字长文教你实现华为云IoT+OpenHarmony智能家居开发

    本文分享自华为云社区<华为云IoT+OpenHarmony的智能家居开发>,作者:袁睿. 一.选题说明 1. 选题为基于OpenHarmony的智能家居,应用场景为户用,受益人群为住户. ...

  5. 逻辑漏洞挖掘之XSS漏洞原理分析及实战演练

    一.前言 2月份的1.2亿条用户地址信息泄露再次给各大公司敲响了警钟,数据安全的重要性愈加凸显,这也更加坚定了我们推行安全测试常态化的决心.随着测试组安全测试常态化的推进,有更多的同事对逻辑漏洞产生了 ...

  6. Dockcer上传hub和配置国内镜像源

    Dockcer上传hub和配置国内镜像源 1.Dockcer上传hub 要将本地的Docker镜像上传到Docker镜像仓库,可以按照以下步骤操作: linux环境 1.创建用户 首先,确保你已经在D ...

  7. HTTPS相比HTTP为什么安全

    HTTPS(超文本传输协议[安全]) 1.HTTPS为什么叫安全的超文本传输协议 在HTTPS中,S是Security的意思,是安全的意思,而HTTP是超文本传输协议,这就不得不谈起HTTP在安全方面 ...

  8. 汇编debug的安装

    实验一查看CPU和内存,用机器指令和汇编指令编程 在做实验前需要debug命令. 工具:dosbox,debug.exe 安装:dosbox :https://www.dosbox.com/ debu ...

  9. Java 队列Queue的一些基本操作与概念!!!!!!!!

    首先Java中的队列(Queue)是一种先进先出的数据结构. 其中常见的一些基本操作与方法,包括: 1.创建队列对象.例如:ArrayDeque.LinkedList等. 2.入队操作.将元素添加到队 ...

  10. commons中StringUtils的全解

    StringUtils()方法的导入包是:org.apache.commons.lang3.StringUtils 作用是:StringUtils()方法是 Apache Commons Lang 库 ...