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

Springboot實現(xiàn)發(fā)送郵件

 更新時間:2021年10月21日 11:26:46   作者:玖月夢沉  
這篇文章主要為大家詳細介紹了Springboot實現(xiàn)發(fā)送郵件功能,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了Springboot實現(xiàn)發(fā)送郵件功能的具體代碼,供大家參考,具體內容如下

第一章 背景介紹

1.1 使用場景

1、注冊驗證;
2、網站營銷;
3、安全的最后一道防線;
4、提醒、監(jiān)控警告;
5、觸發(fā)機制。

1.2 郵件發(fā)送原理

1.郵件傳輸協(xié)議:SMTP協(xié)議和POP3協(xié)議
2.內容不斷發(fā)展:IMAP和Mme協(xié)議

1.3 郵件發(fā)送流程

第二章 使用SpringBoot完成郵件發(fā)送

2.1 開發(fā)流程

2.2 開發(fā)簡單文本郵件

2.2.1 引入相關jar包

在pom.xml中添加依賴

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

2.2.2 配置郵箱參數(shù)

在配置文件里面配置:這里的密碼是授權碼,不是網頁上的密碼

spring.mail.host=smtp.163.com
spring.mail.username=XXX@163.com
spring.mail.password=XXX
spring.mail.default-encoding=utf-8

2.2.3 封裝SimpleMailMessage

SimpleMailMessage message = new SimpleMailMessage();

2.2.4 JavaMailSender進行發(fā)送

@Autowired
private JavaMailSender mailSender;
//使用JavaMailSender發(fā)送郵件
mailSender.send(message);

具體的實現(xiàn):

/**
 * @Description: 發(fā)送郵件
 * @Author: yzy
 * @Date:  2021/10/19 14:01
 **/
@Service
public class MailService {

    @Value("${spring.mail.username}")
    private String sendPeople;

    @Autowired
    private JavaMailSender mailSender;

    /**
     * @Description:  發(fā)送文本文件
     * @author:       yzy
     * @date:         2021/10/19 14:01
     * @Param:
     * @return:
     */
     public void sendSimpleMail(String to,String subject,String content) {
         SimpleMailMessage message = new SimpleMailMessage();
         //接收方
         message.setTo(to);
         //發(fā)送郵件的主題
         message.setSubject(subject);
         //發(fā)送郵件內容
         message.setText(content);
         //發(fā)送人
         message.setFrom(sendPeople);
         //使用JavaMailSender發(fā)送郵件
         mailSender.send(message);

     }

}

測試:

package com.yzy.restaurant.mapper;

import com.yzy.restaurant.MailService;
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.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest
public class MailTest {

    @Autowired
    private MailService mailService;


    @Test
    public void sendSimpleMailTest() {
        mailService.sendSimpleMail("yzy20162362@163.com","這是一個簡單的demo","哈哈哈,發(fā)送成功了!");
    }
}

啟動:

效果:

2.3 開發(fā)HTML郵件

上代碼,在MailService 和MailTest 加

/**
      * @Description:  發(fā)送html郵寄
      * @author:       yzy
      * @date:         2021/10/19 14:58
      * @Param:
      * @return:
      */
     public void sendMailHtml(String to,String subject,String content) throws MessagingException {
         MimeMessage message = mailSender.createMimeMessage();
         MimeMessageHelper helper = new MimeMessageHelper(message,true);
         helper.setTo(to);
         helper.setSubject(subject);
         helper.setText(content,true);
         helper.setFrom(sendPeople);
         mailSender.send(message);
     }
/**
      * @Description:  發(fā)送html郵寄
      * @author:       yzy
      * @date:         2021/10/19 14:58
      * @Param:
      * @return:
      */
     public void sendMailHtml(String to,String subject,String content) throws MessagingException {
         MimeMessage message = mailSender.createMimeMessage();
         MimeMessageHelper helper = new MimeMessageHelper(message,true);
         helper.setTo(to);
         helper.setSubject(subject);
         helper.setText(content,true);
         helper.setFrom(sendPeople);
         mailSender.send(message);
     }

2.3 開發(fā)附件郵件

上代碼,在MailService 和MailTest 加

  /**
      * @Description:  發(fā)送附件郵件
      * @author:       yzy
      * @date:         2021/10/19 15:12
      * @Param:
      * @return:
      */
     public void sendAttachmentsMail(String to,String subject,String content,String filePath) throws MessagingException {
         MimeMessage message = mailSender.createMimeMessage();
         MimeMessageHelper helper = new MimeMessageHelper(message,true);
         helper.setTo(to);
         helper.setSubject(subject);
         helper.setText(content,true);
         helper.setFrom(sendPeople);

         //讀取
         FileSystemResource file = new FileSystemResource(new File(filePath));
         //獲取文件名
         String filename = file.getFilename();
         //設置附件
         helper.addAttachment(filename,file);
         //發(fā)送
         mailSender.send(message);

     }
 @Test
    public void sendAttachmentsMailTest() throws MessagingException {
        String filePath = "D:/玖佳智能 2020年3月第3周周工作匯總(3月16-3月20日)(1).xlsx";
        mailService.sendAttachmentsMail("yzy20162362@163.com","這是一封附件郵件","哈哈哈,附件郵件發(fā)送成功了",filePath);
    }

2.4 圖片郵件

上代碼,在MailService 和MailTest 加

/**
      * @Description:  帶圖片郵件
      * @author:       yzy
      * @date:         2021/10/19 15:35
      * @Param:
      * @return:
      */
    public void sendPhotoMail(String to,String subject,String content,String rscPath, String rscId) throws MessagingException {
        MimeMessage message = mailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(message,true);
        helper.setTo(to);
        helper.setSubject(subject);
        helper.setText(content,true);
        helper.setFrom(sendPeople);

        //讀取
        FileSystemResource rec = new FileSystemResource(new File(rscPath));
        helper.addInline(rscId,rec);
        //發(fā)送
        mailSender.send(message);


}
@Test
    public void sendPhotoMailTest () throws MessagingException {
        String imgPath = "C:\\Users\\yzy\\Desktop\\微信圖片_20210917201828.jpg";
        String rsc = "0001";
        mailService.sendPhotoMail("yzy20162362@163.com","這是一封圖片郵件","哈哈哈,圖片郵件發(fā)送成功了",imgPath,rsc);
    }

2.5 郵件模板

模板郵件特別適用于:

1.用戶注冊的郵件;2.忘記密碼的郵件

我用的是thymeleaf,前提是themleaf已經配置好,上代碼
新建emailTemplate的頁面:

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>郵件模板</title>
</head>
<body>
    你好,感謝您的注冊,這是一封驗證郵件,請點擊下面的連接完成注冊,感謝您的支持!<br>
    <a rel="#" th:href="@{https://mail.163.com}" rel="external nofollow" >激活賬戶</a>>
</body>
</html>

測試代碼:

@Test
    public void sendTemplateMailTest () throws MessagingException {
        Context content = new Context();
        content.setVariable("id","111");
        String emailContent = templateEngine.process("emailTemplate", content);
        mailService.sendMailHtml("yzy20162362@163.com","這是一封模板郵件",emailContent);
    }

效果:

常見錯誤:

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

  • SpringCloud中NacosNamingService的作用詳解

    SpringCloud中NacosNamingService的作用詳解

    這篇文章主要介紹了SpringCloud中NacosNamingService的作用詳解,NacosNamingService類完成服務實例注冊,撤銷與獲取服務實例操作,NacosNamingService初始化采用單例模式,使用反射生成,需要的朋友可以參考下
    2023-11-11
  • 詳解Spring整合Quartz實現(xiàn)動態(tài)定時任務

    詳解Spring整合Quartz實現(xiàn)動態(tài)定時任務

    本篇文章主要介紹了詳解Spring整合Quartz實現(xiàn)動態(tài)定時任務,具有一定的參考價值,感興趣的小伙伴們可以參考一下。
    2017-03-03
  • 在SpringBoot中如何利用Redis實現(xiàn)互斥鎖

    在SpringBoot中如何利用Redis實現(xiàn)互斥鎖

    當我們利用Redis存儲熱點數(shù)據(jù)時,突然就過期失效或者被刪除了,導致大量請求同時訪問數(shù)據(jù)庫,增加了數(shù)據(jù)庫的負載,為減輕數(shù)據(jù)庫的負載我們利用互斥鎖,本文重點介紹在SpringBoot中如何利用Redis實現(xiàn)互斥鎖,感興趣的朋友一起看看吧
    2023-09-09
  • Java????????HashMap遍歷方法匯總

    Java????????HashMap遍歷方法匯總

    這篇文章主要介紹了Java????????HashMap遍歷方法匯總,HashMap?的遍歷方法有很多種,不同的?JDK?版本有不同的寫法,下文關于其遍歷方法總結需要的小伙伴可以參考一下
    2022-05-05
  • 關于Java虛擬機HotSpot

    關于Java虛擬機HotSpot

    這篇文章主要介紹了關于Java虛擬機HotSpot,在Java類中的一些方法會被由C/C++編寫的HotSpot虛擬機的C/C++函數(shù)調用,不過由于Java方法與C/C++函數(shù)的調用約定不同,所以并不能直接調用,需要JavaCalls::call()這個函數(shù)輔助調用,下面我們來看看文章對內容的具體介紹
    2021-11-11
  • Spring中的@RestController注解詳細解析

    Spring中的@RestController注解詳細解析

    這篇文章主要介紹了Spring中的@RestController注解詳細解析,@RestController 是 Spring Framework 中的一個注解,用于標識一個類為 RESTful Web 服務的控制器(Controller),處理 HTTP 請求并返回相應的數(shù)據(jù),
    2024-01-01
  • java編程實現(xiàn)國際象棋棋盤

    java編程實現(xiàn)國際象棋棋盤

    這篇文章主要為大家詳細介紹了java編程實現(xiàn)國際象棋棋盤,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-05-05
  • Java中的forEach循環(huán)詳細解讀

    Java中的forEach循環(huán)詳細解讀

    這篇文章主要介紹了Java中的forEach循環(huán)詳細解讀,不要再foreach循環(huán)里面進行元素的add和remove,如果你非要進行remove元素,那么請使用Iterator方式,如果存在并發(fā),那么你一定要選擇加鎖,需要的朋友可以參考下
    2023-12-12
  • Kotlin基礎教程之伴生對象,getter,setter,內部,局部,匿名類,可變參數(shù)

    Kotlin基礎教程之伴生對象,getter,setter,內部,局部,匿名類,可變參數(shù)

    這篇文章主要介紹了Kotlin基礎教程之伴生對象,getter,setter,內部,局部,匿名類,可變參數(shù)的相關資料,需要的朋友可以參考下
    2017-05-05
  • 詳解如何使用tldb數(shù)據(jù)庫的java客戶端

    詳解如何使用tldb數(shù)據(jù)庫的java客戶端

    這篇文章主要為大家介紹了如何使用tldb數(shù)據(jù)庫的java客戶端過程示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-09-09

最新評論