Springboot实现发送邮件

 更新时间:2021年10月21日 11:26:46   作者:玖月梦沉  
这篇文章主要为大家详细介绍了Springboot实现发送邮件功能,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了Springboot实现发送邮件功能的具体代码,供大家参考,具体内容如下

第一章 背景介绍

1.1 使用场景

1、注册验证;
2、网站营销;
3、安全的最后一道防线;
4、提醒、监控警告;
5、触发机制。

1.2 邮件发送原理

1.邮件传输协议:SMTP协议和POP3协议
2.内容不断发展:IMAP和Mme协议

1.3 邮件发送流程

第二章 使用SpringBoot完成邮件发送

2.1 开发流程

2.2 开发简单文本邮件

2.2.1 引入相关jar包

在pom.xml中添加依赖

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-mail</artifactId>
</dependency>

2.2.2 配置邮箱参数

在配置文件里面配置:这里的密码是授权码,不是网页上的密码

spring.mail.host=smtp.163.com
spring.mail.username=XXX@163.com
spring.mail.password=XXX
spring.mail.default-encoding=utf-8

2.2.3 封装SimpleMailMessage

SimpleMailMessage message = new SimpleMailMessage();

2.2.4 JavaMailSender进行发送

@Autowired
private JavaMailSender mailSender;
//使用JavaMailSender发送邮件
mailSender.send(message);

具体的实现:

/**
 * @Description: 发送邮件
 * @Author: yzy
 * @Date:  2021/10/19 14:01
 **/
@Service
public class MailService {

    @Value("${spring.mail.username}")
    private String sendPeople;

    @Autowired
    private JavaMailSender mailSender;

    /**
     * @Description:  发送文本文件
     * @author:       yzy
     * @date:         2021/10/19 14:01
     * @Param:
     * @return:
     */
     public void sendSimpleMail(String to,String subject,String content) {
         SimpleMailMessage message = new SimpleMailMessage();
         //接收方
         message.setTo(to);
         //发送邮件的主题
         message.setSubject(subject);
         //发送邮件内容
         message.setText(content);
         //发送人
         message.setFrom(sendPeople);
         //使用JavaMailSender发送邮件
         mailSender.send(message);

     }

}

测试:

package com.yzy.restaurant.mapper;

import com.yzy.restaurant.MailService;
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;

@RunWith(SpringRunner.class)
@SpringBootTest
public class MailTest {

    @Autowired
    private MailService mailService;


    @Test
    public void sendSimpleMailTest() {
        mailService.sendSimpleMail("yzy20162362@163.com","这是一个简单的demo","哈哈哈,发送成功了!");
    }
}

启动:

效果:

2.3 开发HTML邮件

上代码,在MailService 和MailTest 加

/**
      * @Description:  发送html邮寄
      * @author:       yzy
      * @date:         2021/10/19 14:58
      * @Param:
      * @return:
      */
     public void sendMailHtml(String to,String subject,String content) throws MessagingException {
         MimeMessage message = mailSender.createMimeMessage();
         MimeMessageHelper helper = new MimeMessageHelper(message,true);
         helper.setTo(to);
         helper.setSubject(subject);
         helper.setText(content,true);
         helper.setFrom(sendPeople);
         mailSender.send(message);
     }
/**
      * @Description:  发送html邮寄
      * @author:       yzy
      * @date:         2021/10/19 14:58
      * @Param:
      * @return:
      */
     public void sendMailHtml(String to,String subject,String content) throws MessagingException {
         MimeMessage message = mailSender.createMimeMessage();
         MimeMessageHelper helper = new MimeMessageHelper(message,true);
         helper.setTo(to);
         helper.setSubject(subject);
         helper.setText(content,true);
         helper.setFrom(sendPeople);
         mailSender.send(message);
     }

2.3 开发附件邮件

上代码,在MailService 和MailTest 加

  /**
      * @Description:  发送附件邮件
      * @author:       yzy
      * @date:         2021/10/19 15:12
      * @Param:
      * @return:
      */
     public void sendAttachmentsMail(String to,String subject,String content,String filePath) throws MessagingException {
         MimeMessage message = mailSender.createMimeMessage();
         MimeMessageHelper helper = new MimeMessageHelper(message,true);
         helper.setTo(to);
         helper.setSubject(subject);
         helper.setText(content,true);
         helper.setFrom(sendPeople);

         //读取
         FileSystemResource file = new FileSystemResource(new File(filePath));
         //获取文件名
         String filename = file.getFilename();
         //设置附件
         helper.addAttachment(filename,file);
         //发送
         mailSender.send(message);

     }
 @Test
    public void sendAttachmentsMailTest() throws MessagingException {
        String filePath = "D:/玖佳智能 2020年3月第3周周工作汇总(3月16-3月20日)(1).xlsx";
        mailService.sendAttachmentsMail("yzy20162362@163.com","这是一封附件邮件","哈哈哈,附件邮件发送成功了",filePath);
    }

2.4 图片邮件

上代码,在MailService 和MailTest 加

/**
      * @Description:  带图片邮件
      * @author:       yzy
      * @date:         2021/10/19 15:35
      * @Param:
      * @return:
      */
    public void sendPhotoMail(String to,String subject,String content,String rscPath, String rscId) throws MessagingException {
        MimeMessage message = mailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(message,true);
        helper.setTo(to);
        helper.setSubject(subject);
        helper.setText(content,true);
        helper.setFrom(sendPeople);

        //读取
        FileSystemResource rec = new FileSystemResource(new File(rscPath));
        helper.addInline(rscId,rec);
        //发送
        mailSender.send(message);


}
@Test
    public void sendPhotoMailTest () throws MessagingException {
        String imgPath = "C:\\Users\\yzy\\Desktop\\微信图片_20210917201828.jpg";
        String rsc = "0001";
        mailService.sendPhotoMail("yzy20162362@163.com","这是一封图片邮件","哈哈哈,图片邮件发送成功了",imgPath,rsc);
    }

2.5 邮件模板

模板邮件特别适用于:

1.用户注册的邮件;2.忘记密码的邮件

我用的是thymeleaf,前提是themleaf已经配置好,上代码
新建emailTemplate的页面:

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>邮件模板</title>
</head>
<body>
    你好,感谢您的注册,这是一封验证邮件,请点击下面的连接完成注册,感谢您的支持!<br>
    <a rel="#" th:href="@{https://mail.163.com}" rel="external nofollow" >激活账户</a>>
</body>
</html>

测试代码:

@Test
    public void sendTemplateMailTest () throws MessagingException {
        Context content = new Context();
        content.setVariable("id","111");
        String emailContent = templateEngine.process("emailTemplate", content);
        mailService.sendMailHtml("yzy20162362@163.com","这是一封模板邮件",emailContent);
    }

效果:

常见错误:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • Java定时任务实现优惠码的示例代码

    Java定时任务实现优惠码的示例代码

    在Java中实现定时任务来发放优惠码,我们可以使用多种方法,比如使用java.util.Timer类、ScheduledExecutorService接口,或者更高级的框架如Spring的@Scheduled注解,这篇文章主要介绍了Java定时任务实现优惠码的实例,需要的朋友可以参考下
    2024-07-07
  • 详解Java中的ThreadLocal

    详解Java中的ThreadLocal

    ThreadLocal是JDK包提供的,它提供线程本地变量,如果创建一个ThreadLocal变量,那么访问这个变量的每个线程都会有这个变量的一个副本,在实际多线程操作的时候,操作的是自己本地内存中的变量,从而规避了线程安全问题
    2021-06-06
  • Maven 项目用Assembly打包可执行jar包的方法

    Maven 项目用Assembly打包可执行jar包的方法

    这篇文章主要介绍了Maven 项目用Assembly打包可执行jar包的方法,该方法只可打包非spring项目的可执行jar包,需要的朋友可以参考下
    2023-03-03
  • Java读文件修改默认换行符的实现

    Java读文件修改默认换行符的实现

    这篇文章主要介绍了Java读文件修改默认换行符的实现方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-12-12
  • spring依赖注入知识点分享

    spring依赖注入知识点分享

    在本篇文章里小编给大家整理的是关于spring依赖注入知识点以及相关代码内容,需要的朋友们学习下。
    2019-11-11
  • Java 类与对象超基础讲解

    Java 类与对象超基础讲解

    类(class)和对象(object)是两种以计算机为载体的计算机语言的合称。对象是对客观事物的抽象,类是对对象的抽象。类是一种抽象的数据类型
    2022-03-03
  • 解决idea配置Tomcat Deployment没有artifact选项的问题

    解决idea配置Tomcat Deployment没有artifact选项的问题

    今天在配置的时候tomcat deployment中却找不到artifact,没有artifact就不能打成war包上传到服务器了,那么怎么解决没有artifact选项的问题呢,今天通过本文给大家分享idea配置Tomcat Deployment没有artifact选项的解决方案,一起看看吧
    2023-10-10
  • 详解Maven optional关键字透彻图解

    详解Maven optional关键字透彻图解

    这篇文章主要介绍了详解Maven optional关键字透彻图解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-11-11
  • 一文带你搞懂SpringBoot中自动装配原理

    一文带你搞懂SpringBoot中自动装配原理

    这篇文章主要为大家详细介绍了SpringBoot中自动装配原理的相关知识,文中的示例代码讲解详细,具有一定的借鉴价值,有需要的小伙伴可以参考下
    2025-01-01
  • Groovy动态语言使用教程简介

    Groovy动态语言使用教程简介

    这篇文章主要为大家介绍了Groovy动态语言使用教程简介,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-09-09

最新评论