spring|Java技术(SpringBoot实现邮件发送功能)

spring|Java技术(SpringBoot实现邮件发送功能)
文章图片

目录
1、创建一个基本的SpringBoot项目,pom文件导入发送邮件的依赖
2、application.yml 文件配置配置邮件发送信息
3、创建IEmailService 接口文件,定义邮件发送的接口
4、创建IEmailService接口的实现类EmailService.java 文件
5、新建邮件发送模板 email.html
【spring|Java技术(SpringBoot实现邮件发送功能)】6、新建测试类,主要代码如下
7、效果截图

邮件发送功能基本是每个完整业务系统要集成的功能之一,今天小编给大家介绍一下SpringBoot实现邮件发送功能,希望对大家能有所帮助!
今天主要给大家分享简单邮件发送、HTML邮件发送、包含附件的邮件发送三个例子,具体源码链接在文章末尾,有需要的朋友可以自己下载学习一下。
1、创建一个基本的SpringBoot项目,pom文件导入发送邮件的依赖

org.springframework.boot spring-boot-starter-mail org.springframework.boot spring-boot-starter-freemarker

2、application.yml 文件配置配置邮件发送信息
spring: mail: host: smtp.qq.com username: xxx@qq.com#发件人邮箱 password: xxxxx#授权码 protocol: smtp properties.mail.smtp.auth:true properties.mail.smtp.port:465#发件邮箱端口 properties.mail.display.sendmail: xiaoMing properties.mail.display.sendname: xiaoming properties.mail.smtp.starttls.enable:true properties.mail.smtp.starttls.required:true properties.mail.smtp.ssl.enable:true#是否启用ssl default-encoding: utf-8#编码格式 freemarker: cache:false settings: classic_compatible:true suffix: .html charset: UTF-8 template-loader-path: classpath:/templates/

3、创建IEmailService 接口文件,定义邮件发送的接口
package com.springboot.email.email.service; import javax.mail.MessagingException; import java.util.List; public interface IEmailService { /** * 发送简单文本邮件 */ void sendSimpleMail(String receiveEmail, String subject, String content); /** * 发送HTML格式的邮件 */ void sendHtmlMail(String receiveEmail, String subject, String emailContent) throws MessagingException; /** * 发送包含附件的邮件 */ void sendAttachmentsMail(String receiveEmail, String subject, String emailContent, List filePathList) throws MessagingException; }

4、创建IEmailService接口的实现类EmailService.java 文件
package com.springboot.email.email.service.impl; import com.springboot.email.email.service.IEmailService; 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.Service; import javax.annotation.Resource; import javax.mail.MessagingException; import javax.mail.internet.MimeMessage; import java.io.File; import java.util.List; @Service public class EmailServiceImpl implements IEmailService { @Resource private JavaMailSender mailSender; @Value("${spring.mail.username}") private String fromEmail; /** * 发送简单文本邮件 */ public void sendSimpleMail(String receiveEmail, String subject, String content) { SimpleMailMessage message = new SimpleMailMessage(); message.setFrom(fromEmail); message.setTo(receiveEmail); message.setSubject(subject); message.setText(content); mailSender.send(message); } /** * 发送Html格式的邮件 */ publicvoid sendHtmlMail(String receiveEmail,String subject,String emailContent) throws MessagingException { init(receiveEmail, subject, emailContent, mailSender, fromEmail); }public static void init(String receiveEmail, String subject, String emailContent, JavaMailSender mailSender, String fromEmail) throws MessagingException { MimeMessage message= mailSender.createMimeMessage(); MimeMessageHelper helper=new MimeMessageHelper(message,true); helper.setFrom(fromEmail); helper.setTo(receiveEmail); helper.setSubject(subject); helper.setText(emailContent,true); mailSender.send(message); }/** * 发送包含附件的邮件 */ public void sendAttachmentsMail(String receiveEmail, String subject, String emailContent, List filePathList) throws MessagingException { MimeMessage message = mailSender.createMimeMessage(); //带附件第二个参数true MimeMessageHelper helper = new MimeMessageHelper(message, true); helper.setFrom(fromEmail); helper.setTo(receiveEmail); helper.setSubject(subject); helper.setText(emailContent, true); //添加附件资源 for (String item : filePathList) { FileSystemResource file = new FileSystemResource(new File(item)); String fileName = item.substring(item.lastIndexOf(File.separator)); helper.addAttachment(fileName, file); } //发送邮件 mailSender.send(message); } }

5、新建邮件发送模板 email.html
- 锐客网td { border: black 1px solid; } 工资条
序号 姓名 基本工资 在职天数 奖金 社保 个税 实际工资
${salary.index} ${salary.name} ${salary.baseSalary} ${salary.inDays} ${salary.reward} ${salary.socialSecurity} ${salary.tax} ${salary.actSalary}

6、新建测试类,主要代码如下
/** * 测试简单文本文件 */ @Test public void EmailTest() { emailService.sendSimpleMail("hgmyz@outlook.com", "测试邮件", "springboot 邮件测试"); }@Test public void HtmlEmailTest() throws MessagingException { String receiveEmail = "hgmyz@outlook.com"; String subject = "Spring Boot 发送Html邮件测试"; String emailContent = "您好!这里是一封Spring Boot 发送的邮件,祝您天天开心!spring|Java技术(SpringBoot实现邮件发送功能)
文章图片

html文件
spring|Java技术(SpringBoot实现邮件发送功能)
文章图片

包含附件的邮件
spring|Java技术(SpringBoot实现邮件发送功能)
文章图片

Gitee地址:https://gitee.com/hgm1989/springboot-email.git

    推荐阅读