当前位置:   article > 正文

Java技术:SpringBoot实现邮件发送功能_springboot email

springboot email

      d0543ce9e311620ce89e2ac606383ea2.png        

目录

1、创建一个基本的SpringBoot项目,pom文件导入发送邮件的依赖

2、application.yml 文件配置配置邮件发送信息

3、创建IEmailService 接口文件,定义邮件发送的接口

4、创建IEmailService接口的实现类EmailService.java 文件

5、新建邮件发送模板 email.html

6、新建测试类,主要代码如下

7、效果截图


邮件发送功能基本是每个完整业务系统要集成的功能之一,今天小编给大家介绍一下SpringBoot实现邮件发送功能,希望对大家能有所帮助!

今天主要给大家分享简单邮件发送、HTML邮件发送、包含附件的邮件发送三个例子,具体源码链接在文章末尾,有需要的朋友可以自己下载学习一下。

1、创建一个基本的SpringBoot项目,pom文件导入发送邮件的依赖

  1. <!--邮件发送依赖包-->
  2. <dependency>
  3. <groupId>org.springframework.boot</groupId>
  4. <artifactId>spring-boot-starter-mail</artifactId>
  5. </dependency>
  6. <!--freemarker制作Html邮件模板依赖包-->
  7. <dependency>
  8. <groupId>org.springframework.boot</groupId>
  9. <artifactId>spring-boot-starter-freemarker</artifactId>
  10. </dependency>

2、application.yml 文件配置配置邮件发送信息

  1. spring:
  2. mail:
  3. host: smtp.qq.com
  4. username: xxx@qq.com #发件人邮箱
  5. password: xxxxx #授权码
  6. protocol: smtp
  7. properties.mail.smtp.auth:true
  8. properties.mail.smtp.port:465#发件邮箱端口
  9. properties.mail.display.sendmail: xiaoMing
  10. properties.mail.display.sendname: xiaoming
  11. properties.mail.smtp.starttls.enable:true
  12. properties.mail.smtp.starttls.required:true
  13. properties.mail.smtp.ssl.enable:true#是否启用ssl
  14. default-encoding: utf-8#编码格式
  15. freemarker:
  16. cache:false
  17. settings:
  18. classic_compatible:true
  19. suffix: .html
  20. charset: UTF-8
  21. template-loader-path: classpath:/templates/

3、创建IEmailService 接口文件,定义邮件发送的接口

  1. package com.springboot.email.email.service;
  2. import javax.mail.MessagingException;
  3. import java.util.List;
  4. public interface IEmailService {
  5. /**
  6. * 发送简单文本邮件
  7. */
  8. void sendSimpleMail(String receiveEmail, String subject, String content);
  9. /**
  10. * 发送HTML格式的邮件
  11. */
  12. void sendHtmlMail(String receiveEmail, String subject, String emailContent) throws MessagingException;
  13. /**
  14. * 发送包含附件的邮件
  15. */
  16. void sendAt
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/2023面试高手/article/detail/728313
推荐阅读
相关标签
  

闽ICP备14008679号