您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

springboot怎么实现自动邮件发送任务

2024/2/24 12:36:47发布20次查看
1.导入jar包<dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-mail</artifactid> <version>2.5.2</version></dependency>
2.配置文件获取qq邮箱的pop3/smtp服务,获取加密的密码,并编写springboot的配置文件
spring.mail.username=2080038188@qq.com #qq号不是我的哦
spring.mail.password=gzcmhxxybgvbcjil #这里就是加密的密码
spring.mail.host=smtp.qq.com
spring.mail.properties.mail.smtp.ssl.enable=true #这一步是开启加密验证
3.测试在测试类中进行测试
@springboottestclass sprintboot09testapplicationtests { @autowired javamailsenderimpl mailsender; @test void contextloads() { simplemailmessage mailmessage = new simplemailmessage(); mailmessage.setsubject("你好呀"); mailmessage.settext("谢谢你"); mailmessage.setto("2080038188@qq.com"); mailmessage.setfrom("2080038188@qq.com"); mailsender.send(mailmessage); }}
然后就发送成功了,你的qq邮箱就会收到你自己发送的信息,这里是非常有成就感的哦!!!
重点:在我测试中一直有bug,找了近半个小时才找到,结果就是我们springboot配置文件多打了一个空格,肉眼都无法看到,这种错误,属实找的好辛苦,一度怀疑是jar包版本或qq邮箱的问题,结果是多虑了。今后一定要注意springboot配置文件的空格问题!!!
复杂的邮件发送@testvoid contextloads2() throws messagingexception { //一个复杂的邮件 mimemessage mimemessage = mailsender.createmimemessage(); //组装 mimemessagehelper helper = new mimemessagehelper(mimemessage, true); //正文 helper.setsubject("小狂神你好呀~plus"); helper.settext("<p style='color:red'>谢谢你的狂神说java系列课程~</p>",true); //附件 helper.addattachment("1.jpg",new file("c:\\users\\86187\\desktop\\1.jpg")); helper.setto("2080038187@qq.com"); helper.setfrom("2080038187@qq.com"); mailsender.send(mimemessage);}
以上就是springboot怎么实现自动邮件发送任务的详细内容。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product