使用SpringBoot发送邮件的方法详解
作者:掉头发的王富贵 时间:2023-01-30 02:21:37
第一步,导jar包
<!--Email-->
<dependency>
<groupId>com.sun.mail</groupId>
<artifactId>javax.mail</artifactId>
<version>1.6.2</version>
</dependency>
<!--Email-->
第二步,申请email授权密码
这里以163邮箱举例:
第三步,编写yml文件
spring:
mail:
host: smtp.163.com #固定写死的 163邮箱
username: masiyi163163@163.com #刚刚生成授权码的邮箱
password: NOTZSJISFIOOWDLNY #刚刚生成的授权码
default-encoding: UTF-8
第四步,编写工具类
一个Java邮件工具类,使用Spring Boot和JavaMailSender实现了发送邮件的功能。该类使用了@Component注解,标识为一个Spring Bean,可以被其他类依赖注入使用。通过@Autowired注解注入了JavaMailSender类,以便使用其提供的邮件发送功能。同时,通过@Value注解注入了配置文件中的发件人邮件地址,方便发送邮件时设置发件人。
该工具类提供了两种发送邮件的方法,分别为简单文本邮件和带有附件的邮件。其中,简单文本邮件使用了SimpleMailMessage类实现,而带有附件的邮件使用了MimeMessage和MimeMessageHelper类实现。同时,通过传递不同的参数,实现了发送不同的邮件内容和附件。
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.messaging.MessagingException;
import org.springframework.stereotype.Component;
import javax.mail.internet.MimeMessage;
import java.io.File;
/**
* @author MaSiyi
* @version 1.0.0 2021/11/20
* @since JDK 1.8.0
*/
@Component
public class EmailUtil {
@Autowired
private JavaMailSender javaMailSender;
@Value("${spring.mail.username}")
private String from;
/**
* 简单文本邮件
* @param to 接收者邮件
* @param subject 邮件主题
* @param content 邮件内容
*/
public void sendSimpleMail(String to, String subject, String content){
SimpleMailMessage message = new SimpleMailMessage();
message.setTo(to);
message.setSubject(subject);
message.setText(content);
message.setFrom(from);
javaMailSender.send(message);
}
/**
* 附件邮件
* @param to 接收者邮件
* @param subject 邮件主题
* @param content HTML内容
* @param filePath 附件路径
* @throws MessagingException
*/
public void sendAttachmentsMail(String to, String subject, String content,
String filePath) throws MessagingException {
try {
MimeMessage mimeMessage = javaMailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
helper.setTo(to);
helper.setSubject(subject);
helper.setText(content, true);
helper.setFrom(from);
FileSystemResource file = new FileSystemResource(new File(filePath));
String fileName = file.getFilename();
helper.addAttachment(fileName, file);
javaMailSender.send(mimeMessage);
} catch (javax.mail.MessagingException e) {
e.printStackTrace();
}
}
}
第五步,编写controller类
一个邮件服务相关的控制器类EmailController,使用了SpringBoot框架中的@RestController注解来标识它是一个Web API控制器类。同时,它还使用了@Api注解来标识这是一个邮件服务的API,方便其他开发者对这个API进行理解和使用。
这个控制器类中有两个API,分别是sendSimpleMail和sendAttachmentsMail。这两个API都是GET请求,并使用了@ApiOperation注解来描述它们的功能和参数。其中,sendSimpleMail方法调用了EmailUtil类中的sendSimpleMail方法,用于发送简单邮件;sendAttachmentsMail方法调用了EmailUtil类中的sendAttachmentsMail方法,用于发送带附件的邮件。
import com.wangfugui.apprentice.common.util.EmailUtil;
import com.wangfugui.apprentice.common.util.ResponseUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @author MaSiyi
* @version 1.0.0 2021/11/20
* @since JDK 1.8.0
*/
@Api(tags = "邮件服务")
@RestController
@RequestMapping("/email")
public class EmailController {
@Autowired
private EmailUtil emailUtil;
@GetMapping("/sendSimpleMail")
@ApiOperation("发送简单邮件")
public ResponseUtils sendSimpleMail(String email,String subject,String content) {
emailUtil.sendSimpleMail(email,subject,content);
return ResponseUtils.success();
}
@GetMapping("/sendAttachmentsMail")
@ApiOperation("发送附件邮件")
public ResponseUtils sendAttachmentsMail(String email,String subject,String content,String filePath) {
emailUtil.sendAttachmentsMail(email, subject, content, filePath);
return ResponseUtils.success();
}
}
第六步,测试
查看邮箱
成功!!
这篇文章主要介绍了如何在Spring Boot项目中使用JavaMailSender发送邮件,包括导入所需的jar包、申请邮箱授权密码、编写配置文件、编写JavaMailSender工具类和Controller类,最后演示了如何调用工具类发送简单文本邮件和带有附件的邮件。
好了,就是这么的简单,完整代码请移至SpringBoot+Email查看
来源:https://juejin.cn/post/7229858467804823589
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
SpringBoot使用prometheus监控的示例代码
![](https://img.aspxhome.com/file/2023/0/65590_0s.jpg)
Mybatis如何通过接口实现sql执行原理解析
![](https://img.aspxhome.com/file/2023/0/60480_0s.jpg)
MyBatis学习教程(四)-如何快速解决字段名与实体类属性名不相同的冲突问题
使用Jackson反序列化遇到的问题及解决
svn 清理失败 (cleanup 失败) 的快速解决方法
![](https://img.aspxhome.com/file/2023/3/60843_0s.jpg)
java实现把对象数组通过excel方式导出的功能
Mybatis中的延迟加载案例解析
![](https://img.aspxhome.com/file/2023/1/68221_0s.png)
Java 集合概览(小结)
![](https://img.aspxhome.com/file/2023/0/59000_0s.jpg)
Java中的Map集合简单汇总解析
java自动生成ID号的方法
Springboot mybatis plus druid多数据源解决方案 dynamic-datasource的使用详解
使用Jenkins来构建GIT+Maven项目的方法步骤
![](https://img.aspxhome.com/file/2023/8/61838_0s.png)
java设计模式笔记之适配器模式
![](https://img.aspxhome.com/file/2023/2/62242_0s.png)
spring boot devtools在Idea中实现热部署方法
springboot配置文件中属性变量引用方式@@解读
![](https://img.aspxhome.com/file/2023/6/59506_0s.png)
C# goto语句的具体使用
![](https://img.aspxhome.com/file/2023/6/71766_0s.png)
浅谈virtual、abstract方法和静态方法、静态变量理解
Java IO文件后缀名过滤总结
java设计模式之工厂方法模式
![](https://img.aspxhome.com/file/2023/3/66543_0s.jpg)
Flutter倒计时/计时器的实现代码
![](https://img.aspxhome.com/file/2023/5/103895_0s.jpg)