添加依赖

        <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邮件的更多相关文章

  1. C#发送Email邮件(实例:QQ邮箱和Gmail邮箱)

    下面用到的邮件账号和密码都不是真实的,需要测试就换成自己的邮件账号. 需要引用: using System.Net.Mail; using System.Text; using System.Net; ...

  2. [转]C#发送Email邮件 (实例:QQ邮箱和Gmail邮箱)

    下面用到的邮件账号和密码都不是真实的,需要测试就换成自己的邮件账号. 需要引用:using System.Net.Mail;using System.Text;using System.Net; 程序 ...

  3. 【转】C#发送Email邮件

    转自:http://hi.baidu.com/bluesky_cn/item/8bb060ace834c53f020a4df2 下面用到的邮件账号和密码都不是真实的,需要测试就换成自己的邮件账号. 需 ...

  4. SpringBoot 发送简单邮件

    使用SpringBoot 发送简单邮件 1. 在pom.xml中导入依赖 <!--邮件依赖--> <dependency> <groupId>org.springf ...

  5. 使用SpringBoot发送mail邮件

    1.前言 发送邮件应该是网站的必备拓展功能之一,注册验证,忘记密码或者是给用户发送营销信息.正常我们会用JavaMail相关api来写发送邮件的相关代码,但现在springboot提供了一套更简易使用 ...

  6. python 发送email邮件带附件

    EMAIL功能实现: 1.发送EMAIL带附件,并且带压缩文件夹做为附件 #_*_coding:utf-8_*_ import smtplib from email.mime.text import ...

  7. Oracle PLSQL通过SMTP发送E-MAIL邮件代码

    登录到SMTPserver发送邮件,支持HTML CREATE OR REPLACE PROCEDURE send_mail(        p_recipient VARCHAR2, -- 邮件接收 ...

  8. (十三)SpringBoot 发送E-mail

    一:添加mail依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId ...

  9. Java发送Email邮件及SpringBoot集成

    一:普通方式发送 1.导包 <!--Java MAil 发送邮件API--> <dependency> <groupId>javax.mail</groupI ...

随机推荐

  1. java比较时间的方法

    一.通过compareTo Date date = new Date(1576118709574L); Date date1 = new Date(1576118709574L); Date date ...

  2. LeetCode练题——35. Search Insert Position

    1.题目 35. Search Insert Position Easy 1781214Add to ListShare Given a sorted array and a target value ...

  3. 前端 9.16腾讯-2019校园招聘(正式卷)编程题题解(js)

    第一题 和谐的数字 牛牛很喜欢研究数字.一天,他发明了一种数字,叫做“和谐的数字”. 和谐的数字定义如下: 定义S(n)为数字n各位数字之和,如果S(n)能够整除n,那么就称n为一个“和谐的数字”. ...

  4. 操作系统OS - 阻塞(Blocking)非阻塞(Non-Blocking)与同步(Synchronous)异步(Asynchronous)

    参考: http://blog.jobbole.com/103290/ https://www.zhihu.com/question/19732473/answer/23434554 http://b ...

  5. 【知识学习】PHP实现批量替换字典后缀

    <?php //要打开字典的物理路径 $filename = 'E:\Local Test\WWW\password.txt'; $handle = fopen($filename,'r') o ...

  6. Services: ARP Caching

    Online Help for Cisco IOS Release 12.2(15)JA Services: ARP Caching   ARP caching on the access point ...

  7. 对于java中反编译命令的使用以及Integer包装类的查看

    Integer是基于int的包装类 我们可以用测试代码来看看Integer类是如何实现装箱和拆箱的 public class BoxAndUnbox { /** * @param args */ pu ...

  8. 「Luogu P5080 Tweetuzki 爱序列」

    题目大意 给出一些数,需要求出 \(\frac{a_{i+1}}{3}=a_i\) 或 \(a_{i+1}=2 \times a_i\) 时最长的序列 \(a\). 分析 可以发现符合条件的序列 \( ...

  9. hutoolJava工具类的使用

    前言 安装 友情开源项目 Hutool相关博客(软文) 捐赠使用公开 核心(Hutool-core) 克隆 支持泛型的克隆接口和克隆类 类型转换 类型转换工具类-Convert 自定义类型转换-Con ...

  10. python开发接口

    享一段代码,开发了3个接口:            1.上传文件            2.查看所有文件            3.下载文件 使用python开发,需要安装flask模块,使用pip ...