SpringBoot任务之详解邮件任务
程序员文章站
2022-06-27 14:53:37
一、springboot--任务:邮件任务1.1 添加依赖(增加邮件支持)pom.xml org.spring...
一、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邮件任务内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!