欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

SpringBoot QQ郵箱發(fā)送郵件實例代碼

 更新時間:2021年12月31日 16:16:27   作者:Silly,,  
大家好,本篇文章主要講的是SpringBoot QQ郵箱發(fā)送郵件實例代碼,感興趣的同學(xué)趕快來看一看吧,對你有幫助的話記得收藏一下,方便下次瀏覽

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)文章

最新評論