1、pom.xml配置

 <dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency> <!-- SpringBoot 发送邮件 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
</dependencies>

2、在application.properties中添加邮箱配置

spring.mail.default-encoding=UTF-8
spring.mail.host=smtp.qq.com
#发送者的邮箱密码
spring.mail.password=xxx
#端口
spring.mail.port=25
#协议
spring.mail.protocol=smtp
#发送者的邮箱账号
spring.mail.username=xxx@qq.com
server.port=8081

邮箱密码:

QQ邮箱 --> 邮箱设置 --> 账号

3、项目整体结构

4、业务层service接口

package org.zyu.springboot_email.service;

public interface EmailService {

    /**
* 发送简单邮件
* @param sendTo 接收人
* @param title 邮件标题
* @param content 邮件内容
*/
void sendSimpleMail(String sendTo,String title,String content); /**
* 发送带静态资源的邮件
* @param sendTo 接收人
* @param title 邮件标题
* @param content 邮件内容
* @param path 资源路径
*/
void sendInlineMail(String sendTo,String title,String content,String path); /**
* 发送带附件的邮件
* @param sendTo 接收人
* @param title 邮件标题
* @param content 邮件内容
* @param filePath 附件路径
*/
void sendAttachmentsMail(String sendTo,String title,String content,String filePath);
}

5、serviceImpl实现service接口

package org.zyu.springboot_email.service;

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.internet.MimeMessage;
import java.io.File; @Service
public class EmailServiceImpl implements EmailService { @Autowired
JavaMailSender mailSender; //发送人
@Value("${spring.mail.username}")
private String sendFrom; @Override
public void sendSimpleMail(String sendTo, String title, String content) {
SimpleMailMessage mainMessage = new SimpleMailMessage();
mainMessage.setFrom(sendFrom);
//接收者
mainMessage.setTo(sendTo);
//发送的标题
mainMessage.setSubject(title);
//发送的内容
mainMessage.setText(content);
mailSender.send(mainMessage);
} @Override
public void sendInlineMail(String sendTo, String title, String content,String path) {
MimeMessage mimeMessage = mailSender.createMimeMessage(); try {
FileSystemResource file = new FileSystemResource(new File(path));
MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
helper.setFrom(sendFrom);
helper.setTo(sendTo);
helper.setSubject(title);
helper.setText(content, true);
helper.addInline("weixin", file);
} catch (Exception e) {
System.out.println("发送带静态资源的邮件失败");
} mailSender.send(mimeMessage);
} @Override
public void sendAttachmentsMail(String sendTo, String title, String content, String filePath) {
MimeMessage message=mailSender.createMimeMessage();
try {
MimeMessageHelper helper=new MimeMessageHelper(message,true);
helper.setFrom(sendFrom);
helper.setTo(sendTo);
helper.setSubject(title);
helper.setText(content);
FileSystemResource file=new FileSystemResource(new File(filePath));
String fileName=filePath.substring(filePath.lastIndexOf(File.separator));
//添加多个附件可以使用多条
//helper.addAttachment(fileName,file);
helper.addAttachment(fileName,file);
mailSender.send(message);
System.out.println("带附件的邮件发送成功");
}catch (Exception e){
e.printStackTrace();
System.out.println("发送带附件的邮件失败");
}
}
}

5、业务层Controller

package org.zyu.springboot_email.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.zyu.springboot_email.service.EmailService; import java.nio.file.Path; @RestController
public class MailController { @Autowired
private EmailService emailService; /**
* 简单发送邮件
* @return
*/
@GetMapping("/send")
public String send(){
emailService.sendSimpleMail( "393488908@qq.com", "测试标题", "hello world");
return "发送成功";
} /**
* 发送静态资源邮件
* @return
*/
@GetMapping("/sendInlineMail")
public String sendInlineMail(){
emailService.sendInlineMail("393488908@qq.com", "主题:嵌入静态资源", "<html><body><img src=\"cid:weixin\" ></body></html>","C:\\Users\\Administrator\\Desktop\\timg.jpg");
return "发送成功";
} /**
* 发送带附件的邮件
* @return
*/
@GetMapping("/sendAttachmentsMail")
public String sendAttachmentsMail(){
emailService.sendAttachmentsMail("393488908@qq.com", "主题:携带附件邮件", "hello world", "D:\\谷歌下载\\注册补贴明细列表.xlsx");
return "发送成功";
} }

springboot---发送邮件的更多相关文章

  1. SpringBoot 发送邮件功能实现

    背景 有个小伙伴问我你以前发邮件功能怎么弄的.然后我就给他找了个demo,正好在此也写一下,分享给大家. 理清痛点 发送邮件,大家可以想一下,坑的地方在哪? 我觉得是三个吧. 第一:邮件白名单问题. ...

  2. 记录一次简单的springboot发送邮件功能

    场景:经常在我们系统中有通过邮件功能找回密码,或者发送生日祝福等功能,今天记录下springboot发送邮件的简单功能 1.引入maven <!-- 邮件开发--><dependen ...

  3. 1.使用javax.mail, spring的JavaMailSender,springboot发送邮件

    一.java发邮件 电子邮件服务器:这些邮件服务器就类似于邮局,它主要负责接收用户投递过来的邮件,并把邮件投递到邮件接收者的电子邮箱中,按功能划分有两种类型 SMTP邮件服务器:用户替用户发送邮件和接 ...

  4. 3.4 SpringBoot发送邮件

      spring官方提供了spring-boot-starter-mail来整合邮件发送功能,本质上还是利用了JavaMailSender类. 首先我们要在项目中引入相关依赖 <parent & ...

  5. (入门SpringBoot)SpringBoot发送邮件(十一)

    SpringBoot配置邮件服务: 1.引入jar <!-- 邮件 --> <dependency>    <groupId>org.springframework ...

  6. springboot发送邮件,以及携带邮件附件简单使用

    可以通过springboot官方文档中Sending Email,找到类似如下java mail的使用文档 https://docs.spring.io/spring/docs/5.1.9.RELEA ...

  7. 【快学springboot】使用springboot发送邮件

    前言 在实际项目中,经常需要用到邮件通知功能.比如,用户通过邮件注册,通过邮件找回密码等:又比如通过邮件发送系统情况,通过邮件发送报表信息等等,实际应用场景很多.这篇文章,就教大家通过springbo ...

  8. 使用 FreeMarker模板 Springboot 发送邮件

    四.使用 FreeMarker模板 HTML 标签的字符串拼接是一件很棘手的事.因为在你的大脑中解析HTML标签并想象它在渲染时会是什么样子是挺困难的.而将HTML混合在Java代码中又会使得这个问题 ...

  9. 最简单的 springboot 发送邮件,使用thymeleaf模板

    1,导入需要的包 <dependency> <groupId>org.springframework.boot</groupId> <artifactId&g ...

  10. springboot发送邮件

    1.在发送端邮箱平台开通SMTP服务 1)以163邮箱为例: step 1: step 2: 2.编写代码: 1)添加发送邮箱maven依赖 <dependency> <groupI ...

随机推荐

  1. 使用 .NET Core 3.0 的 AssemblyLoadContext 实现插件热加载

    一般情况下,一个 .NET 程序集加载到程序中以后,它的类型信息以及原生代码等数据会一直保留在内存中,.NET 运行时无法回收它们,如果我们要实现插件热加载 (例如 Razor 或 Aspx 模版的热 ...

  2. python selenium模拟登陆qq空间

    不多说.直接上代码 from selenium import webdriver driver = webdriver.Chrome() driver.get('http://qzone.qq.com ...

  3. jquery复习日记(1)

    jquery封装了JavaScript常用的功能代码,提供一种简便的JavaScript设计模式,优化HTML文档操作.事件处理.动画设计和Ajax交互. 核心关键字: 链式.多功能.高效灵活   1 ...

  4. Python3 GUI开发(PyQt)安装和配置

    Python3 GUI开发(PyQt5)安装和配置: 下载安装好Miniconda3, 并且安装好jupyter 注意:最好关闭360杀毒软件或者把cmd加入信任,否则运行activate会有问题. ...

  5. Linux-rhel-server-7.4-Mysql-5.7安装记录

    解压下载的tar包: tar -xf mysql-5.7.19-1.el7.x86_64.rpm-bundle.tar 安装一下rpm包: sudo rpm -ivh mysql-community- ...

  6. 包名targetPackage和目录名targetProject

    generatorConfig.xml中的 <javaModelGenerator targetPackage="edu.cn.pojo" targetProject=&qu ...

  7. Cocos Creator实现左右跳游戏,提供完整游戏代码工程

    ​1. 玩法说明 游戏开始后,点击屏幕左右两侧,机器人朝左上方或右上方跳一步,如果下一步有石块,成功得1分,否则游戏结束. 2. 模块介绍 游戏场景分为2个:主页场景(home).游戏场景(game) ...

  8. 简单cookie入侵

    在当前网站,按下F12键进入开发者模式,在console控制台输入:document.cookie获取cookie值如: 复制你得到cookie值,你或通过每种方式获取Cookie,例如:当别人点击你 ...

  9. BZOJ 4621: Tc605

    Description 最初你有一个长度为 N 的数字序列 A.为了方便起见,序列 A 是一个排列. 你可以操作最多 K 次.每一次操作你可以先选定一个 A 的一个子串,然后将这个子串的数字全部变成原 ...

  10. Django2.1.3 urls.py path模块配置

    learning_log/urls.py learning_logs/urls.py django2.0和1.x的区别是非常明显的,2.0开始使用path和re_path代替原来的url,而且用法有了 ...