这篇文章主要介绍SpringBoot任务之邮件任务的示例分析,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!01: 异步任务02: 定时任务一、springBoot--任务:邮件任务1.1 添加依赖(增加邮件支持)po
这篇文章主要介绍SpringBoot任务之邮件任务的示例分析,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!
01: 异步任务
02: 定时任务
pom.xml
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId> </dependency>
这是为了不直接暴露QQ密码设置的机制
ljoghujwhbkpbaff
application.properties
# qq邮箱spring.mail.username=1585526224@qq.com# 刚刚生成的授权码spring.mail.passWord=ljoghujwhbkpbaff# qq邮箱的hostspring.mail.host=smtp.qq.com#开启加密验证(qq邮箱)spring.mail.properties.mail.smtp.ssl.enable=true
直接调用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;@SpringBootTestclass 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); }}
运行结果:
调用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邮箱确是可以看见的。欢迎知道的大神告诉我
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任务之邮件任务的示例分析”这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注编程网精选频道!
--结束END--
本文标题: SpringBoot任务之邮件任务的示例分析
本文链接: https://lsjlt.com/news/279456.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0