添加依赖

        <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. 探索 Python + HyperLPR 进行车牌识别

    概要 HyperLRP是一个开源的.基于深度学习高性能中文车牌识别库,由北京智云视图科技有限公司开发,支持PHP.C/C++.Python语言,Windows/Mac/Linux/Android/IO ...

  2. JDBC通过资源文件初始化

  3. 【代码总结】PHP面向对象之常见的关键字和魔术方法

    一.关键字的使用 1.final关键字 只能用来修饰类 和 成员方法  不能修饰成员属性 被final修饰的类不能被继承 用final修饰的成员方法 不能被子类覆盖(重写) <?php // f ...

  4. 模块学习-json pickle

    json json序列化 import json def sayhi(name): print("hello",name) info = { 'name':'mogu', 'age ...

  5. Qt5基于smtp服务发送电子邮件

    1.设置邮箱 先登录163邮箱,然后在邮箱界面找到设置,在里面开通smtp服务. 这一步比较关键,要开通smtp服务,在开通的过程中会让你输入一个邮箱客户端授权码,这个才是你后面要用到的密码,而不是你 ...

  6. 一类Log-Gamma积分的一般形式

    \[\Large\int_{0}^{z}x^{t}\ln\Gamma \left ( 1+x \right )\mathrm{d}x~,~z>0\, ,\, t\in N^{*}\] \(\La ...

  7. 在Ubuntu_meta 16.04中设置默认Python3.5的命令

    ubuntu_meta 默认是安装了python2.7 和python3.5两个版本 查看ubuntu的python版本,默认版本是python2.7 输入:python 直接执行这两个命令即可: s ...

  8. 吴裕雄 python 神经网络——TensorFlow 滑动平均类的保存

    import tensorflow as tf v = tf.Variable(0, dtype=tf.float32, name="v") for variables in tf ...

  9. WLC-Download 3-party CA to WLC

    一.基础准备 为了创建和导入第三方SSL-certificate你需要做如下准备:1.一个WLC(随着版本的不同,可能需要准备的也不同)这里以7.0.98版本为例.2.一个外部的证书颁发机构(Cert ...

  10. SQL mybatis动态查询小结

    动态返回mysql某张表指定列的名字 <select id="queryColumns" resultType="map" parameterType=& ...