iis服务器助手广告
返回顶部
首页 > 资讯 > 后端开发 > Python >Springboot实现发送邮件
  • 274
分享到

Springboot实现发送邮件

2024-04-02 19:04:59 274人浏览 独家记忆

Python 官方文档:入门教程 => 点击学习

摘要

本文实例为大家分享了SpringBoot实现发送邮件功能的具体代码,供大家参考,具体内容如下 第一章 背景介绍 1.1 使用场景 1、注册验证; 2、网站营销; 3、安全的最后一道防

本文实例为大家分享了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);

具体的实现:



@Service
public class MailService {

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

    @Autowired
    private JavaMailSender mailSender;

    
     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 加



     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);
     }


     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 加


  
     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 加



    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);
    }

效果:

常见错误:

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

--结束END--

本文标题: Springboot实现发送邮件

本文链接: http://www.lsjlt.com/news/155055.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • Springboot实现发送邮件
    本文实例为大家分享了Springboot实现发送邮件功能的具体代码,供大家参考,具体内容如下 第一章 背景介绍 1.1 使用场景 1、注册验证; 2、网站营销; 3、安全的最后一道防...
    99+
    2024-04-02
  • SpringBoot实现发送电子邮件
    目录1. 前言1.1 电子邮件发展史1.2 电子邮件原理1.3 电子邮件地址的构成1.4 电子邮件传输协议2. 实现发送电子邮件3. 码农来洞见1. 前言 电子邮件是—种...
    99+
    2024-04-02
  • SpringBoot整合Javamail实现邮件发送
    博客主页:踏风彡的博客 博主介绍:一枚在学习的大学生,希望在这里和各位一起学习。 所属专栏:SpringBoot学习笔记 文章创作不易,期待各位朋友的互动,有什么学习问题都可在评论区留言或者私信我...
    99+
    2023-08-31
    spring boot java spring
  • 怎么用SpringBoot实现QQ邮箱发送邮件
    本篇内容主要讲解“怎么用SpringBoot实现QQ邮箱发送邮件”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“怎么用SpringBoot实现QQ邮箱发送邮件”吧!1.获取QQ邮箱授权码2.导入邮...
    99+
    2023-06-22
  • springboot实现发送QQ邮箱
    springboot发送电子邮箱,供大家参考,具体内容如下 1.开启qq邮箱开启IMAP/SMTP服务* 首先进入qq邮箱 点击设置 点击账户,然后往下拉 开启IMAP/SMT...
    99+
    2024-04-02
  • SpringBoot实现发送邮件、发送微信公众号推送功能
    目录SpringBoot实现发送邮件pom.xmlapplication.yml代码实现SpringBoot实现发送微信公众号推送pom.xml代码实现SpringBoot实现发送邮...
    99+
    2024-04-02
  • SpringBoot实现邮件发送的示例代码
    工具类: package com.lhh.utils; import com.lhh.bean.EmailEntity; import javax.mail.*; import...
    99+
    2024-04-02
  • springboot发送邮件功能的实现代码
           发邮件是一个很常见的功能,在java中实现需要依靠JavaMailSender这个接口。在spri...
    99+
    2024-04-02
  • Springboot发送邮件功能的实现详解
    目录前言成果展示表设计引入依赖邮箱工具类mapperXmlServiceimplEmailServiceImpl写完后要去进行配置获取授权码总结前言 大多数小伙伴在练习与学习的过程中...
    99+
    2024-04-02
  • 如何用springboot实现发送邮件功能
    本篇内容介绍了“如何用springboot实现发送邮件功能”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!   ...
    99+
    2023-06-20
  • Python实现邮件发送
    使用smtplib模块发送邮件,它对smtp协议进行了简单的封装。smtp协议的基本命令包括:    HELO 向服务器标识用户身份    MAIL 初始化邮件传输 mail from:    RCPT 标识单个的邮件接收人;常在MAIL命...
    99+
    2023-01-31
    邮件发送 Python
  • Javaweb实现邮件发送
    本文实例为大家分享了Javaweb实现邮件发送的具体代码,供大家参考,具体内容如下 发送邮件使用的是SMTP协议:一般是smtp.xxx.com ,比如smtp.qq.com 接收邮...
    99+
    2024-04-02
  • java实现发送邮件
    本文介绍下java实现邮件的发送,意在网站用户评论时能够及时通知站长和用户评论被回复后能够及时通知用户。 下文介绍下具体实现。 java实现 首先引入springboot的邮箱依赖 org.springframework.boot ...
    99+
    2023-08-20
    java spring spring boot
  • tp6实现邮件发送
    tp6实现邮件发送 phpMailer 是一个非常强大的 ph p发送邮件类,可以设定发送邮件地址、回复地址、邮件主题、html网页,上传附件,并且使用起来非常方便。 phpMailer 的特点: 1、在邮件中包含多个 TO、CC、BCC ...
    99+
    2023-09-15
    服务器 php 运维
  • PHP实现发送邮件功能代码|PHP怎么实现QQ邮件发送|Php发送邮件代码
    最近学习PHP的过程中发现了一个很实用的功能那就是发送QQ邮件,因为这个功能很常用我也是研究了半天找到一个很好的demo感兴趣的可以自己下载学习一下这个Php发送邮件代码真的很实用而且很好用 使用方法: 上传整体压缩包到服务器解压 修改se...
    99+
    2023-09-16
    php 服务器 apache
  • springboot实现自动邮件发送任务详解
    目录1.导入jar包2.配置文件3.测试复杂的邮件发送springboot可以很容易实现邮件的发送 具体实现步骤: 1.导入jar包 <dependency> &...
    99+
    2024-04-02
  • springboot怎么实现自动邮件发送任务
    这篇“springboot怎么实现自动邮件发送任务”文章的知识点大部分人都不太理解,所以小编给大家总结了以下内容,内容详细,步骤清晰,具有一定的借鉴价值,希望大家阅读完这篇文章能有所收获,下面我们一起来看看这篇“springboot怎么实现...
    99+
    2023-06-29
  • SpringBoot实现发送QQ邮件的示例代码
    目录配置发送邮件1、引入SpringBoot的Mail依赖2、配置邮箱发送的Bean3、发送测试邮件4、查看效果应用启动&停止邮件通知总结在跑个人应用的时候,想引入一个通知机...
    99+
    2024-04-02
  • django 实现QQ邮箱发送邮件
    要使用Django来发送QQ邮件,您需要完成以下步骤:1. 在您的Django项目的settings.py文件中,配置邮件发送的相关...
    99+
    2023-09-21
    django
  • JavaMail实现发送邮件(QQ邮箱)
    本文实例为大家分享了JavaMail实现发送邮件的具体代码,供大家参考,具体内容如下 用的qq邮箱,需要去邮箱设置那边开一下stmp服务啥的获得下面要用到的密码,具体开服务自己百度,...
    99+
    2022-11-13
    JavaMail发送邮件 JavaMail发送QQ邮箱 Java发送邮件
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作