目录
一:导包
二:使用
1:在qq邮箱生成授权码:允许此qq可以被我们的电脑指挥发邮件
2:application.properties进行配置(并把授权码放到spring.mail.password=kytxteybnhhldiie)
3:test进行测试(配置邮件内容)
一:导包
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-mail</artifactId>
</dependency>
二:使用
1:在qq邮箱生成授权码:允许此qq可以被我们的电脑指挥发邮件
2:application.properties进行配置(并把授权码放到spring.mail.password=kytxteybnhhldiie)
#application.properties配置mail信息
spring.mail.username=2448003093@qq.com
spring.mail.password=kytxteybnhhldiie
#如果是163邮箱就改成smtp.163.com
spring.mail.host=smtp.qq.com
#开启加密验证
spring.mail.properties.mail.smtp.ssl.enable=true
3:test进行测试(配置邮件内容)
@SpringBootTest
class Springboot12MailApplicationTests {@ResourceJavaMailSenderImpl mailSender;@Testvoid contextLoads() {//一个简单的邮件//邮件信息SimpleMailMessage message = new SimpleMailMessage();message.setSubject("你好241");//标题message.setText("谢谢你的课程");//正文message.setTo("2410069290@qq.com");message.setFrom("2448003093@qq.com");mailSender.send(message);}@Testvoid contextLoad2() throws javax.mail.MessagingException {//一个复杂的邮件MimeMessage mimeMessage = mailSender.createMimeMessage();//组装MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);//正文helper.setSubject("你好241——plus");//标题helper.setText("<h1>你好和</h1>", true);//附件helper.addAttachment("1.jpg", new File("E:\\1.jpg"));helper.setTo("2410069290@qq.com");helper.setFrom("2448003093@qq.com");mailSender.send(mimeMessage);}//封装成一个方法public void sendMail(Boolean htmlBoolean, String subject, Boolean textBoolean, String text, String filename, String pathname, String sendTo, String sendFrom) throws MessagingException {//一个复杂的邮件MimeMessage mimeMessage = mailSender.createMimeMessage();//组装MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, htmlBoolean);//正文helper.setSubject(subject);//标题helper.setText(text, textBoolean);//附件helper.addAttachment(filename, new File(pathname));helper.setTo(sendTo);helper.setFrom(sendFrom);mailSender.send(mimeMessage);}}