SpringBoot QQ郵箱發(fā)送郵件實例代碼
SpringBoot整合郵件任務(wù)(QQ郵箱發(fā)送)
1.獲取QQ郵箱授權(quán)碼
2.導(dǎo)入郵箱發(fā)送依賴啟動器
使用定制郵件模板的方法實現(xiàn)通用郵件發(fā)送,Thymeleaf構(gòu)建郵件模板需要一起導(dǎo)入依賴。
<!-- Mail --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId> </dependency> <!-- thymeleaf模板依賴 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency>
3.配置文件yml添加郵件服務(wù)配置
# Spring配置 spring: mail: host: smtp.qq.com username: ********@qq.com # password是第一步QQ郵箱開通的smtp服務(wù)后得到的客戶端授權(quán)碼 password: ****************** default-encoding: UTF-8 properties: mail: smtp: auth: true starttls: enable: true required: true #thymeleaf模板引擎配置太簡單,就不貼出來了
4.編寫接口IMailService
public interface IMailService { void sendHtmlMailThymeLeaf(String mailFrom, String mailFromNick, String mailTo, String cc, String subject, String content); }
5.編寫實現(xiàn)MailServiceImpl
@Service public class MailServiceImpl implements IMailService { /** * JavaMailSender是Spring Boot在MailSenderPropertiesConfiguration 類中配直好的,該類在 Mail * 自動配置類 MailSenderAutoConfiguration 中導(dǎo)入 因此這里注入 JavaMailSender 就可以使用了 */ @Autowired private JavaMailSender mailSender; @Override public void sendHtmlMailThymeLeaf(String mailFrom, String mailFromNick, String mailTo, String cc, String subject, String content) { MimeMessage mimeMessage = mailSender.createMimeMessage(); try { MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage, true); mimeMessageHelper.setFrom(new InternetAddress(mailFromNick + " <" + mailFrom + ">")); // 設(shè)置多個收件人 String[] toAddress = mailTo.split(","); mimeMessageHelper.setTo(toAddress); if (!StringUtils.isEmpty(cc)) { mimeMessageHelper.setCc(cc); } mimeMessageHelper.setSubject(subject); // 第二個參數(shù)為true表示郵件正文是html格式的,默認(rèn)是false mimeMessageHelper.setText(content, true); mailSender.send(mimeMessage); } catch (MessagingException e) { System.out.println(e); } } }
6.Controller調(diào)用
// 發(fā)件人要跟yml配置文件里填寫的郵箱一致 String mailFrom = "******@qq.com"; // 收件人 String mailTo = "******@qq.com,******@qq.com"; // 抄送(可為空) String cc = "******@qq.com"; // 注入mailService @Autowired private IMailService mailService; // 注入TemplateEngine @Autowired TemplateEngine templateEngine; @RequestMapping("/other/test")//請求路徑 @ResponseBody public void testMail() { //注意1:這里我是查詢對應(yīng)的內(nèi)容,使用富文本編輯器存儲html標(biāo)簽的內(nèi)容 Strategy strategy = strategyService.selectStrategyByStrategyId(Long.valueOf(1)); Context context = new Context(); // 導(dǎo)包是org.thymeleaf.context //注意2:獲取發(fā)送的內(nèi)容傳入thymeleaf模板中 context.setVariable("content", strategy.getStrategyContent()); String content = templateEngine.process("mailTemplate.html", context); //System.out.println(content); mailService.sendHtmlMailThymeLeaf(mailFrom, "定義發(fā)件人名字", mailTo, cc, "定義郵件標(biāo)題", content); System.out.println("郵件發(fā)送成功"); }
7.thymeleaf模板 mailTemplate.html
<!DOCTYPE html> <html lang="en" xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>郵件發(fā)送</title> </head> <body> <!--使用富文本框包含HTML標(biāo)簽 使用 th:utext標(biāo)簽 會解析html,顯示相應(yīng)的效果--> <div th:utext="${content}">Some escaped text</div> </body> </html>
總結(jié)
到此這篇關(guān)于SpringBoot QQ郵箱發(fā)送郵件實例代碼的文章就介紹到這了,更多相關(guān)SpringBoot 郵箱發(fā)信內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
struts1之簡單mvc示例_動力節(jié)點(diǎn)Java學(xué)院整理
這篇文章主要介紹了struts1之簡單mvc示例的相關(guān)資料,需要的朋友可以參考下2017-09-09SpringCloud中的斷路器(Hystrix)和斷路器監(jiān)控(Dashboard)
本篇主要介紹的是SpringCloud中的斷路器(Hystrix)和斷路器指標(biāo)看板(Dashboard)的相關(guān)使用知識,需要的朋友可以參考下2019-06-06java實現(xiàn)二維數(shù)組轉(zhuǎn)json的方法示例
這篇文章主要介紹了java實現(xiàn)二維數(shù)組轉(zhuǎn)json的方法,涉及java數(shù)組遍歷及json格式數(shù)據(jù)構(gòu)造相關(guān)操作技巧,需要的朋友可以參考下2017-10-10淺談@mapper引入不到引入的是@MapperScan的問題
這篇文章主要介紹了淺談@mapper引入不到引入的是@MapperScan的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-10-10基于mybatis中<include>標(biāo)簽的作用說明
這篇文章主要介紹了基于mybatis中<include>標(biāo)簽的作用說明,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-02-02