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基础(一)--python介绍
1. Python语言 1.1 编程语言 语言是人类最重要的交际工具,是人类之间进行信息交换的主要表达方式. 编程语言是用来定义计算机程序的语言,用来向计算机发出指令. 1.2 Python语言 Py ...
- POJ-2891 Strange Way to Express Integers(拓展中国剩余定理)
放一个写的不错的博客:https://www.cnblogs.com/zwfymqz/p/8425731.html POJ好像不能用__int128. #include <iostream> ...
- 【知识学习】Sublime Text 快捷键精华版
Sublime Text 快捷键精华版 Ctrl+Shift+P:打开命令面板 Ctrl+P:搜索项目中的文件 Ctrl+G:跳转到第几行 Ctrl+W:关闭当前打开文件 Ctrl+Shift+W:关 ...
- Linux 常用命令——解压缩文件
tar 解包:tar xvf FileName.tar打包:tar cvf FileName.tar DirName(注:tar是打包,不是压缩!)———————————————.gz解压1:gunz ...
- 关于永久POE
1.传统POE 在我们的企业网络中,经常会使用交换机给IP电话或者无线AP供电,以使得其正常的工作. 正常情况下,我们都知道,普通的POE是在PSE交换机启动完成后,然后再给PD(Power Devi ...
- PostgreSQL数据库-分页sql--offset
select * from users order by score desc limit 3;--取成绩的前3名=====select * from users order by score des ...
- 【 hibernate 】基本配置
hibernate.cfg.xml <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibe ...
- SVN中如何创建共享文件夹
http://wenku.baidu.com/link?url=E8tC1idSdTABc3JzyqGYF8OXakDTIF_yWp8BvFqSX5dgcpkbm8Z57ursZtLpjmQAEABM ...
- Caused by: java.lang.NoClassDefFoundError: org/apache/commons/pool2/impl/GenericObjectPoolConfig
Caused by: java.lang.NoClassDefFoundError: org/apache/commons/pool2/impl/GenericObjectPoolConfig at ...
- JavaScript - 编译性还是解释性?
疑问 在JS的变量和声明式函数的提升看到了"预编译/预处理/预解释"中"预编译"这个字眼,产生了一个疑问:JS是熟知的解释性语言,但JS能被编译吗? 参考 ht ...