1. 程式人生 > 其它 >SpringBoot-Mail傳送郵件

SpringBoot-Mail傳送郵件

springboot如何傳送郵件

------------恢復內容開始------------

1.pom檔案新增mail相關的starter

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-mail</artifactId>
        </dependency>

2. application.properties檔案或者application.yaml檔案中配置郵箱

#mail
spring.mail.default-encoding=utf-8
spring.mail.host=smtp.sina.com
spring.mail.port=465
spring.mail.protocol=smtps
[email protected]
spring.mail.password=授權碼

3. MailClient 郵件代理類

package wht.spring.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import
org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.mail.javamail.JavaMailSender; import org.springframework.mail.javamail.MimeMessageHelper; import org.springframework.stereotype.Component; import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage; @Component public class MailClient { private static final Logger logger= LoggerFactory.getLogger(MailClient.class); @Autowired JavaMailSender mailSender; //傳送郵件的類,SpringBoot自動構造該類物件並注入 @Value("${spring.mail.username}") String from; public void sendMail(String to,String subject,String context){ try{ //訊息類,表示郵件內容 MimeMessage message=mailSender.createMimeMessage(); //MimeMessageHelper輔助訊息類設定郵件內容 MimeMessageHelper helper=new MimeMessageHelper(message); helper.setFrom(from); helper.setTo(to); helper.setSubject(subject); //true表示傳送的內容是html格式 helper.setText(context,true); mailSender.send(helper.getMimeMessage()); } catch (MessagingException e) { logger.error("傳送郵件失敗"+e.getMessage()); } } }

4. 傳送郵件測試

package wht.spring;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import wht.spring.utils.MailClient;

@SpringBootTest
public class MailTest {
    @Autowired
    MailClient mailClient;
    @Test
    public void mailTest(){
        String to="[email protected]";
        String subject="mailTest";
        String context="你好Nils";
        mailClient.sendMail(to,subject,context);
    }
}

5. 模板引擎製作郵件內容

package wht.spring;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;
import wht.spring.utils.MailClient;

@SpringBootTest
public class MailTest {
    @Autowired
    MailClient mailClient;
    @Autowired
    TemplateEngine templateEngine;  //thymeleaf starter中的類,springboot自動建立該類物件並注入
    @Test
    public void testHtmlMail() {
        //thymeleaf依賴中的Context類,作用相當於Controller返回的Model,用於渲染模板
        Context context = new Context();
        context.setVariable("username", "sunday");

        String content = templateEngine.process("/mail/demo", context);
        System.out.println(content);

        mailClient.sendMail("[email protected]", "HTML", content);
    }
}

------------恢復內容結束------------