添加依赖

        <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. ASP.NET Core搭建多层网站架构【6-注册跨域、网站核心配置】

    2020/01/29, ASP.NET Core 3.1, VS2019, NLog.Web.AspNetCore 4.9.0 摘要:基于ASP.NET Core 3.1 WebApi搭建后端多层网站 ...

  2. 序列变换 HDU - 5256

    序列变换 HDU - 5256 题目链接 题目 我们有一个数列A1,A2...An,你现在要求修改数量最少的元素,使得这个数列严格递增.其中无论是修改前还是修改后,每个元素都必须是整数. 请输出最少需 ...

  3. vb.net 实现多态

    1. 新建一个module,设置public 其他类才可以调用 Public Module Module2 Public Interface MyInterface Property stuName ...

  4. Hibernate学习过程出现的问题

    1  核心配置文件hibernate.cfg.xml添加了约束但是无法自动获取属性值 解决方案:手动将DTD文件导入 步骤:倒开Eclipse,找到[window]->[preference]- ...

  5. Composer包收录

    doctrine/annotations #注解 nesbot/carbon #日期和时间处理 gregwar/captcha symfony/console nikic/fast-route #路由 ...

  6. component:(resolve) => require

    resolve => require(['../pages/home.vue'], resolve)这种写法是异步模块获取,打包的时候每次访问这个路由的时候会单调单个文件,按需加载,不过这种写法 ...

  7. vue.js_①

    前言: Vue (读音 /vjuː/,类似于 view) 是一套用于构建用户界面的渐进式框架.与其它大型框架不同的是,Vue 被设计为可以自底向上逐层应用.Vue 的核心库只关注视图层,不仅易于上手, ...

  8. cmd设置utf8编码

    在中文windows系统中,如果一个文本文件是utf-8编码的,那么在cmd.exe命令行窗口(所谓的dos窗口)中不能正确显示文件中的内容.在默认情况下,命令行窗口中使用的代码页是中文或者美国的,即 ...

  9. ubuntu 更改pip默认源

    mkdir ~/.pip vim ~/.pip/pip.conf [global] timeout = 6000 https://pypi.tuna.tsinghua.edu.cn/simple 保存 ...

  10. nginx防盗链处理模块referer和secure_link模块

    使用场景:某网站听过URI引用你的页面:当用户在网站点击url时:http头部会通过referer头部,将该网站当前页面的url带上,告诉服务本次请求是由这个页面发起的 思路:通过referer模块, ...