JavaMail是Java开发中邮件处理的开源类库,支持常用协议如:SMTP、POP3、IMAP
一、SpringBoot整合
1、需要在pom文件中添加依赖spring-boot-starter-mail
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
构建项目,加载相关jar包
2、在application.yml中配置邮件发送的相关信息
spring:
mail:
host: smtp.mxhichina.com #阿里云发送服务器地址
port: 25 #端口号
username: XXX@XXXX.com #发送人地址
password: ENC(Grg2n2TYzgJv9zpwufsf37ndTe+M1cYk) #密码
3、编写邮件发送工具类MailUtil
MailUtil.java
/**
* @author chenzan
* @version V1.0
* @description 邮件发送
*/
@Component
public class MailUtil {
@Autowired
JavaMailSendermailSender;
@Autowired
MailPropertiesmailProperties;
/**
* 发送邮件测试方法
*/
public