目录
1、发送简单邮件(JavaMail)
SpringBoot整合JavaMail
①首先了解三个概念
●SMTP (Simple Mail Transfer Protocol) :简单邮件传输协议,用于发送电子邮件的传输协议
●POP3 (Post Office Protocol - Version3) :用于接收电子邮件的标准协议
●IMAP (Internet Mail Access Protocol) :互联网消息协议,是POP3的替代协议
②导入相应坐标
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
③配置文件中配置JavaMail(password在邮件中获取)
spring:
mail:
host: smtp.qq.com
username: xxxxxxxxxx@qq.com
password: **************
④开启定时任务功能
@Service
public class SendMailServiceImpl implements SendMailService{
//发送人
private String from="xxxxxxxxqq.com";
//接收人
private String to="xxxxxxxqq.com";
//标题
private String subject="一个简单邮件";
//正文
private String context="这是个简单邮件";
}
@Service
public class SendMailServiceImpl implements SendMailService{
@Autowired
private JavaMailSender javaMailSender;
@Override
public void sendMail(){
SimpleMailMessage message =new SimpleMailMessage();
message.setFrom(from);
message.setTo(to);
message.setSubject(subject);
message.setText(context);
javaMailSender.send(mailMessage);
}
}