完成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. React中setState的使用与同步异步

    在react中,修改状态如果直接使用this.state,不会引起组件的重新渲染,需要通过 this.setState来对组件的属性进行修改. 1.this.setState的两种定义方式 定义初始状 ...

  2. Node.js安装中出现的问题及其解决方案

    Node.js安装与配置流程,请参考 1.npm -v测试时出现警告 更好的选择是安装一个更完善的版本 问题出现的原因 node更新后是最新版 但是npm的版本没有相应的更新存在版本滞后导致问题出现 ...

  3. 使用kubeadm部署kubernetes

    k8s版本:1.15.0 前期准备 节点: master:172.50.13.103(2核2G) node-1:172.50.13.104(2核2G) node-2:172.50.13.105(2核2 ...

  4. 【后端面经-数据库】Redis详解——Redis基本概念和特点

    目录 1. Redis基本概念 2. Redis特点 2.1 优点 2.2 缺点 3. Redis的应用场景 面试模拟 参考资料 声明:Redis的相关知识是面试的一大热门知识点,同时也是一个庞大的体 ...

  5. 轻松玩转70亿参数大模型!借助Walrus在AWS上部署Llama2

    Llama 2 是 Meta 的下一代开源大语言模型.它是一系列经过预训练和微调的模型,参数范围从 70 亿到 700 亿个.Meta Llama 2 可免费用于研究和商业用途并且提供了一系列具有不同 ...

  6. 深入理解Linux内核——内存管理(1)

    提要:本系列文章主要参考MIT 6.828课程以及两本书籍<深入理解Linux内核> <深入Linux内核架构>对Linux内核内容进行总结. 内存管理的实现覆盖了多个领域: ...

  7. Teamcenter RAC 开发之《日期控件》

    背景 Teamcenter Rac 开发客制化表单过程中,发现一个问题,就是使用日期控件JXDatePicker展示无法实现"签出"禁用控件输入 解决办法 选择使用JDateCho ...

  8. Linux中python更换pip源

    Linux中python更换pip源 执行以下命令,更换为阿里源: echo " [global] trusted-host = mirrors.aliyun.com index-url = ...

  9. Python基础——变量、常量、数字类型、四 列表list、五 字典dict、六 布尔bool、垃圾回收机制、用户交互、运算符、流程控制

    文章目录 变量 一 引入 一.什么是变量? 二.为什么要有变量? 三.怎么使用变量(先定义.后使用) 3.1.变量的定义与使用 3.2.变量名的命名规范 3.3.变量名的命名风格 3.4.变量值的三大 ...

  10. gestureRecognition

    这段代码定义了一个名为 gestureRecognition 的函数,它用于识别手势并显示在摄像头或指定图像上.以下是对代码的详细注释:1. 初始化一个空字符串 ges,用于存储手势结果.如果 sel ...