springboot发送email邮件
添加依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
<version>2.1.7.RELEASE</version>
</dependency>
配置:去邮箱中开启SMTP服务

注意密码是邮箱的生成授权码

代码:
package com.drawnblue.springbootemail; 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.Component; import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;
import java.util.Date; @Component
public class EmailUtil {
private String from="1248375279@qq.com";
@Autowired
private JavaMailSender sender; /**
* 发送一般文本邮件
* @param to
* @param subject
* @param content
*/
public void sendTextEmail(String to,String subject,String content){
SimpleMailMessage message = new SimpleMailMessage();
message.setFrom(from);
message.setTo(to);
message.setSubject(subject);
message.setText(content);
message.setSentDate(new Date());
sender.send(message);
} /**
* @param to
* @param subject
* @param content
* @param imgPath
* @param imgId
* @throws MessagingException
* 发送带图片并显示在邮件中的邮件
*/
public void sendImageMail(String to, String subject, String content, String imgPath, String imgId) throws MessagingException {
//创建message
MimeMessage message = sender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(message, true);
//发件人
helper.setFrom(from);
//收件人
helper.setTo(to);
//标题
helper.setSubject(subject);
//true指的是html邮件,false指的是普通文本
helper.setText(content, true);
//添加图片
FileSystemResource file = new FileSystemResource(new File(imgPath));
helper.addInline(imgId, file);
//发送邮件
sender.send(message);
} }
测试
package com.drawnblue.springbootemail; 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.SpringRunner; import javax.mail.MessagingException; @RunWith(SpringRunner.class)
@SpringBootTest
public class SpringbootEmailApplicationTests {
@Autowired
EmailUtil text; /**
* 文本
*/
@Test
public void contextLoads() {
text.sendTextEmail("yourEmailAddr","test","helloworld!!!");
} /**
* @throws MessagingException
* 发送带图片的邮件
*/
@Test
public void sendImageEmailTest() throws MessagingException {
text.sendImageMail("yourEmailAddr","image测试","<h1 style='color:red'>helloWorld</h1><img src='cid:0011'/>","G:\\壁纸\\timg.jpg","001");
}
}
效果


要了解其他的也可以参考https://www.cnblogs.com/mxwbq/p/10625612.html博文
springboot发送email邮件的更多相关文章
- C#发送Email邮件(实例:QQ邮箱和Gmail邮箱)
下面用到的邮件账号和密码都不是真实的,需要测试就换成自己的邮件账号. 需要引用: using System.Net.Mail; using System.Text; using System.Net; ...
- [转]C#发送Email邮件 (实例:QQ邮箱和Gmail邮箱)
下面用到的邮件账号和密码都不是真实的,需要测试就换成自己的邮件账号. 需要引用:using System.Net.Mail;using System.Text;using System.Net; 程序 ...
- 【转】C#发送Email邮件
转自:http://hi.baidu.com/bluesky_cn/item/8bb060ace834c53f020a4df2 下面用到的邮件账号和密码都不是真实的,需要测试就换成自己的邮件账号. 需 ...
- SpringBoot 发送简单邮件
使用SpringBoot 发送简单邮件 1. 在pom.xml中导入依赖 <!--邮件依赖--> <dependency> <groupId>org.springf ...
- 使用SpringBoot发送mail邮件
1.前言 发送邮件应该是网站的必备拓展功能之一,注册验证,忘记密码或者是给用户发送营销信息.正常我们会用JavaMail相关api来写发送邮件的相关代码,但现在springboot提供了一套更简易使用 ...
- python 发送email邮件带附件
EMAIL功能实现: 1.发送EMAIL带附件,并且带压缩文件夹做为附件 #_*_coding:utf-8_*_ import smtplib from email.mime.text import ...
- Oracle PLSQL通过SMTP发送E-MAIL邮件代码
登录到SMTPserver发送邮件,支持HTML CREATE OR REPLACE PROCEDURE send_mail( p_recipient VARCHAR2, -- 邮件接收 ...
- (十三)SpringBoot 发送E-mail
一:添加mail依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId ...
- Java发送Email邮件及SpringBoot集成
一:普通方式发送 1.导包 <!--Java MAil 发送邮件API--> <dependency> <groupId>javax.mail</groupI ...
随机推荐
- 计算机基础,Python - Map和Reduce
例子1. python中实现hashable def __hash__(self): hashes = map(hash, self.components) return functools.redu ...
- CSS3绘制不规则图形,代码收集
三角形系列(三角形.倒三角.左三角.右三角.左上三角.右上三角.左下三角.右下三角) 主要用到的是:宽度高度设置为0, border的各个边的设置(各个边的透明或不透明): .triangle-up ...
- 2019牛客暑期多校训练营(第七场)A String (字符串的最小表示)
思路 这题思路如果是递归的话,应该是比较正确的.但是实际上只用切割两次就可以了. 先把原串从后向前切割一次,再把每一部分切割一次. 切两次的思路实际上是有漏洞的. 递归的思路,终点是,如果串长为1,或 ...
- ZOJ4104 Sequence in the Pocket(2019浙江省赛)
思维~ #include<bits/stdc++.h> using namespace std; ; int a[maxn]; int b[maxn]; int N; int main ( ...
- USER 指定当前用户,希望以某个已经建立好的用户来运行某个服务进程,不要使用 su 或者 sudo,这些都需要比较麻烦的配置,而且在 TTY 缺失的环境下经常出错。建议使用 gosu
USER 指定当前用户 格式:USER <用户名>[:<用户组>] USER 指令和 WORKDIR 相似,都是改变环境状态并影响以后的层.WORKDIR 是改变工作目录,US ...
- 【原】Django问题总结
一:python manage.py makemigrations No changes detected 在修改了models.py后,有些用户会喜欢用python manage.py makemi ...
- security.web.firewall.RequestRejectedException: The request was rejected because the URL contained a potentially malicious String ";"
今天有个接口打算使用矩阵变量来绑定参数,即使用@MatrixVariable注解来接收参数 调用接口后项目报了如下错误 org.springframework.security.web.firewal ...
- python实现队列(queue)
队列队列是一种先进先出的数据结构,主要操作包括入队,出队.入队的元素加入到对尾,从队头取出出队的元素.这里用列表简单模拟队列,其实现如下: queue()is_empty()size()enqueue ...
- java集合体系结构总结
好,首先我们根据这张集合体系图来慢慢分析.大到顶层接口,小到具体实现类. 首先,我想说为什么要用集合?简单的说:数组长度固定,且是同种数据类型.不能满足需求.所以我们引入集合(容器)来存储任意数据类型 ...
- Python磁力获取器命令行工具 torrent-cli
作为一个搞代码的,找资源这种事肯定不能像普通人一样打开百度盲目查找,你需要写个爬虫工具来帮你完成这件事情啦! 兼容环境 Windows/Linux/MacOs 安装 pip 安装 $ pip inst ...