生活随笔
收集整理的這篇文章主要介紹了
springboot与任务(邮件任务)
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
- 郵件發(fā)送需要引入spring-boot-starter-mail
- Spring Boot 自動(dòng)配置MailSenderAutoConfiguration
- 定義MailProperties內(nèi)容,配置在application.yml中
- 自動(dòng)裝配JavaMailSender
-
測(cè)試郵件發(fā)送
?
?
pom文件配置: <!--郵件發(fā)送--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-mail</artifactId></dependency> ?
applicationproperties配置: spring.mail.username=442624769@qq.com
#自己郵箱的授權(quán)碼
spring.mail.password=lufufllqrylobijg
spring.mail.host=smtp.qq.com#開(kāi)啟安全(有時(shí)需要)
spring.mail.properties.mail.smtp.ssl.enable=true ?
測(cè)試類(lèi): @RunWith(SpringRunner.class)
@SpringBootTest
public class Springboot04TaskApplicationTests {@AutowiredJavaMailSenderImpl mailSender;@Testpublic void contextLoads() {//創(chuàng)建一個(gè)簡(jiǎn)單的消息郵件SimpleMailMessage simpleMailMessage = new SimpleMailMessage();simpleMailMessage.setSubject("通知-今晚開(kāi)會(huì)");simpleMailMessage.setText("今晚7點(diǎn)30開(kāi)會(huì)");simpleMailMessage.setTo("2163370170@qq.com");simpleMailMessage.setFrom("442624769@qq.com");mailSender.send(simpleMailMessage);}@Testpublic void test02() throws MessagingException {//創(chuàng)建一個(gè)復(fù)雜的消息郵件
MimeMessage mimeMessage = mailSender.createMimeMessage();//準(zhǔn)備上傳文件MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true);//郵件設(shè)置helper.setSubject("通知-今晚開(kāi)會(huì)");//設(shè)置寫(xiě)的這段內(nèi)容為htmlhelper.setText("<b style='color:red'>今天7:30開(kāi)會(huì)</b>",true);helper.setTo("2163370170@qq.com");helper.setFrom("442624769@qq.com");//上傳文件helper.addAttachment("1.png",new File("C:\\Users\\44262\\Desktop\\1.png"));mailSender.send(mimeMessage);}
} ?
轉(zhuǎn)載于:https://www.cnblogs.com/MagicAsa/p/10895109.html
總結(jié)
以上是生活随笔為你收集整理的springboot与任务(邮件任务)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。