目录
1.三种协议
SMTP : 简单邮件传输协议,用于发送电子邮件的传输协议
POP3 : 用于接收电子邮件的标准协议
IMAP: 互联网消息协议,是POP3的替代协议
2.简单的邮件发送整合
1.导坐标
<!-- mail-starter -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
2. 做配置
yml:
spring:
mail:
host: smtp.qq.com
username: 133xxx@qq.com
password: 服务器提供的密码 不是邮箱本身的
3.开启定时任务功能
接口:
public interface SendMailService {
void sendMail();
}
实现类:
public class SendMailServiceImpl implements SendMailService {
@Autowired //Springboot 整合的发送邮件的客户端
private JavaMailSender javaMailSender;
//发送人
private String from = "**@qq.com";
//接收人
private String to = "***163.com";
//标题
private String subject = "测试邮件";
//正文
private String context = "测试邮件正文内容"
@Override
public void sendMail(){
SimpleMailMessage messages = new SimpleMailMessage();
message.setFrom(from);
message.setTo(to);
message.setSubject(subject);
message.setText(context);
javaMailSender.send(message);
}
}
测试类:
@SpringBootTest
class SpringbootMailApplicationTest{
@Autowired
private SendMailService sendMailService;
@Test
void contextLoads(){
}
}
邮件内容夹杂链接或者图片等复杂内容的发送:
public class SendMailServiceImpl implements SendMailService {
@Autowired //Springboot 整合的发送邮件的客户端
private JavaMailSender javaMailSender;
//标题
private String subject = "测试邮件";
//正文
private String context = "<a href='https;//www.gggg.com.cn'>点开有惊喜</a>"
@Override
public void sendMail(){
try{
MimeMessage message = javaMailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(message,true);// true 是否可以添加多个附件
helper.setFrom(from);
helper.setTo(to);
helper.setSubject(subject);
helper.setText(context,true);
//如何添加附件
File f = new File("附件的绝对路径");
helper.addAttachment(f.getName(),f1);
javaMailSender.send(message);
} catch(Exception e)
e.printStackTrace();
}
}