Springboot實現(xiàn)Java郵件任務(wù)過程解析
更新時間:2019年09月18日 09:00:34 作者:Jump233
這篇文章主要介紹了Springboot實現(xiàn)Java郵件任務(wù)過程解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
1.maven引入依賴
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId> </dependency>
2.application.properties配置發(fā)送郵箱
//用戶郵箱 spring.mail.username=753029781@qq.com //QQ郵箱開通第三方登錄的授權(quán)碼 spring.mail.password=xxxxxxxxxxxx //主機地址 spring.mail.host=smtp.qq.com //開啟安全的連接 spring.mail.properties.mail.smtp.enable=true
3.測試
import cn.kgc.elastic.vo.Book; import org.junit.Test; import org.junit.runner.RunWith; 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; import org.springframework.mail.javamail.MimeMailMessage; import org.springframework.mail.javamail.MimeMessageHelper; import org.springframework.test.context.junit4.SpringRunner; import javax.mail.MessagingException; import javax.mail.internet.MimeMessage; import java.io.File; @RunWith(SpringRunner.class) @SpringBootTest public class SpringBoot03ApplicationTests { //注入郵件發(fā)送器 @Autowired JavaMailSenderImpl mailSender; @Test public void sentMail(){ //發(fā)送簡單的郵件 SimpleMailMessage message=new SimpleMailMessage(); //郵件設(shè)置 //標題 message.setSubject("注意"); //內(nèi)容 message.setText("有內(nèi)鬼,終止交易"); //發(fā)送人 message.setFrom("753029781@qq.com"); //收件人 message.setTo("jumpjiang233@gmail.com"); mailSender.send(message); } @Test public void tesr01() throws MessagingException { //復雜郵件發(fā)送 MimeMessage mimeMessage=mailSender.createMimeMessage(); //使用helper上傳文件 MimeMessageHelper helper=new MimeMessageHelper(mimeMessage,true); helper.setSubject("注意"); //可以HTML樣式 helper.setText("<b style='color:blue'>有內(nèi)鬼,終止交易</b>",true); //上傳文件,可以上傳多個 helper.addAttachment("1.jpg",new File("C:\\Users\\Jump\\Pictures\\1.jpg")); helper.setFrom("753029781@qq.com"); helper.setTo("2871382340@qq.com"); mailSender.send(mimeMessage); } @Test public void contextLoads() { } }
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
springSecurity之AuthenticationProvider用法解析
這篇文章主要介紹了springSecurity之AuthenticationProvider用法解析,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-03-03Spring 環(huán)境下實現(xiàn)策略模式的示例
這篇文章主要介紹了Spring 環(huán)境下實現(xiàn)策略模式的示例,幫助大家更好的理解和使用spring框架,感興趣的朋友可以了解下2020-10-10