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