SpringBoot任务之详解邮件任务
01: 异步任务
02: 定时任务
一、SpringBoot--任务:邮件任务
1.1 添加依赖(增加邮件支持)
pom.xml
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId> </dependency>
1.2 获取第三方授权码
1.2.1 登录QQ邮箱
1.2.2 点击设置
1.2.3 切换到账户
1.2.4 开启服务
1.2.5 验证密保
1.2.6 成功获取授权码
这是为了不直接暴露QQ密码设置的机制
ljoghujwhbkpbaff
1.2.7 保存更改(左下角)
1.3 配置邮件信息
application.properties
# qq邮箱 spring.mail.username=1585526224@qq.com # 刚刚生成的授权码 spring.mail.password=ljoghujwhbkpbaff # qq邮箱的host spring.mail.host=smtp.qq.com #开启加密验证(qq邮箱) spring.mail.properties.mail.smtp.ssl.enable=true
1.4 简单邮件发送
直接调用SpringBoot
的JavaMailSenderImpl
类,使用SimpleMailMessage
发送简单邮件
EmaildemoApplicationTests.java
package com.tian.emaildemo; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.mail.SimpleMailMessage; import org.springframework.mail.javamail.JavaMailSenderImpl; @SpringBootTest class EmaildemoApplicationTests { @Autowired JavaMailSenderImpl mailSender; @Test void contextLoads() { SimpleMailMessage simpleMailMessage = new SimpleMailMessage(); // 邮件主题 simpleMailMessage.setSubject("你好"); // 邮件文本 simpleMailMessage.setText("Hello world"); // 接收方 simpleMailMessage.setTo("1585526224@qq.com"); // 发送方 simpleMailMessage.setFrom("1585526224@qq.com"); // 发送邮件 mailSender.send(simpleMailMessage); } }
运行结果:
1.5 复杂邮件发送
调用mailSender.createMimeMessage()
并使用MimeMessageHelper
配置邮件内容,发送即可,邮件内容后设置为true
可以解析html
格式的内容
EmaildemoApplicationTests.java
@Test void contextLoads() throws MessagingException { SimpleMailMessage simpleMailMessage = new SimpleMailMessage(); //复杂邮件 MimeMessage mimeMessage = mailSender.createMimeMessage(); //组装 MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage, true); mimeMessageHelper.setSubject("复杂邮件"); // 发送文本: html文本 mimeMessageHelper.setText("<p style='color: orange;'>\n" + " 海客谈瀛洲,烟涛微茫信难求;<br>\n" + "\n" + " 越人语天姥,云霞明灭或可睹。<br>\n" + "\n" + " 天姥连天向天横,势拔五岳掩赤城。<br>\n" + "\n" + " 天台四万八千丈,对此欲倒东南倾。<br>\n" + "\n" + " (四万 一作:一万)我欲因之梦吴越,一夜飞度镜湖月。<br>\n" + "\n" + " (度 通:渡)湖月照我影,送我至剡溪。<br>\n" + "\n" + " 谢公宿处今尚在,渌水荡漾清猿啼。<br>\n" + " <br>\n" + " 脚著谢公屐,身登青云梯。<br>\n" + "\n" + " 半壁见海日,空中闻天鸡。<br>\n" + "\n" + " 千岩万转路不定,迷花倚石忽已暝。<br>\n" + "\n" + " 熊咆龙吟殷岩泉,栗深林兮惊层巅。<br>\n" + "\n" + " 云青青兮欲雨,水澹澹兮生烟。<br>\n" + "\n" + " 列缺霹雳,丘峦崩摧。<br>\n" + " <br>\n" + " 洞天石扉,訇然中开。<br>\n" + "\n" + " 青冥浩荡不见底,日月照耀金银台。<br>\n" + "\n" + " 霓为衣兮风为马,云之君兮纷纷而来下。<br>\n" + "\n" + " 虎鼓瑟兮鸾回车,仙之人兮列如麻。<br>\n" + "\n" + " 忽魂悸以魄动,恍惊起而长嗟。<br>\n" + " <br>\n" + " 惟觉时之枕席,失向来之烟霞。<br>\n" + "\n" + " 世间行乐亦如此,古来万事东流水。<br>\n" + "\n" + " 别君去兮何时还?<br>\n" + "\n" + " 且放白鹿青崖间。<br>\n" + "\n" + " 须行即骑访名山。<br>\n" + "\n" + " 安能摧眉折腰事权贵,使我不得开心颜!<br>\n" + " </p>", true);//true,开启html解析 // 发送图片 mimeMessageHelper.addAttachment("重庆理工大学夜景.jpg", new File("D:\\图片\\schoolNight.jpg")); mimeMessageHelper.setTo("1585526224@qq.com"); mimeMessageHelper.setFrom("1585526224@qq.com"); mailSender.send(mimeMessage); }
运行结果:
我不知道为什么:我写的CSS样式在电脑上面不可预见,但是在我的手机QQ邮箱确是可以看见的。欢迎知道的大神告诉我
1.6 可以把上面复杂发送邮件封装成一个方法
public void SendMail(Boolean html, String title, String text, File file, String sendTo, String sendFrom) throws MessagingException { //复杂邮件 MimeMessage mimeMessage = mailSender.createMimeMessage(); //组装 MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage, true); mimeMessageHelper.setSubject(title); mimeMessageHelper.setText(text, html);//true,开启html解析 mimeMessageHelper.addAttachment("1.jpg", file); mimeMessageHelper.setTo(sendTo); mimeMessageHelper.setFrom(sendFrom); mailSender.send(mimeMessage); }
到此这篇关于SpringBoot任务之详解邮件任务的文章就介绍到这了,更多相关SpringBoot邮件任务内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!
赞 (0)