最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

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

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

第一章 背景介紹

1.1 使用場(chǎng)景

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

1.2 郵件發(fā)送原理

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

1.3 郵件發(fā)送流程

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

2.1 開(kāi)發(fā)流程

2.2 開(kāi)發(fā)簡(jiǎn)單文本郵件

2.2.1 引入相關(guān)jar包

在pom.xml中添加依賴

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

2.2.2 配置郵箱參數(shù)

在配置文件里面配置:這里的密碼是授權(quán)碼,不是網(wǎng)頁(yè)上的密碼

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進(jìn)行發(fā)送

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

具體的實(shí)現(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ā)送郵件內(nèi)容
         message.setText(content);
         //發(fā)送人
         message.setFrom(sendPeople);
         //使用JavaMailSender發(fā)送郵件
         mailSender.send(message);

     }

}

測(cè)試:

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","這是一個(gè)簡(jiǎn)單的demo","哈哈哈,發(fā)送成功了!");
    }
}

啟動(dòng):

效果:

2.3 開(kāi)發(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 開(kāi)發(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();
         //設(shè)置附件
         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.用戶注冊(cè)的郵件;2.忘記密碼的郵件

我用的是thymeleaf,前提是themleaf已經(jīng)配置好,上代碼
新建emailTemplate的頁(yè)面:

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

測(cè)試代碼:

@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);
    }

效果:

常見(jiàn)錯(cuò)誤:

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Java定時(shí)任務(wù)實(shí)現(xiàn)優(yōu)惠碼的示例代碼

    Java定時(shí)任務(wù)實(shí)現(xiàn)優(yōu)惠碼的示例代碼

    在Java中實(shí)現(xiàn)定時(shí)任務(wù)來(lái)發(fā)放優(yōu)惠碼,我們可以使用多種方法,比如使用java.util.Timer類、ScheduledExecutorService接口,或者更高級(jí)的框架如Spring的@Scheduled注解,這篇文章主要介紹了Java定時(shí)任務(wù)實(shí)現(xiàn)優(yōu)惠碼的實(shí)例,需要的朋友可以參考下
    2024-07-07
  • 詳解Java中的ThreadLocal

    詳解Java中的ThreadLocal

    ThreadLocal是JDK包提供的,它提供線程本地變量,如果創(chuàng)建一個(gè)ThreadLocal變量,那么訪問(wèn)這個(gè)變量的每個(gè)線程都會(huì)有這個(gè)變量的一個(gè)副本,在實(shí)際多線程操作的時(shí)候,操作的是自己本地內(nèi)存中的變量,從而規(guī)避了線程安全問(wèn)題
    2021-06-06
  • Maven?項(xiàng)目用Assembly打包可執(zhí)行jar包的方法

    Maven?項(xiàng)目用Assembly打包可執(zhí)行jar包的方法

    這篇文章主要介紹了Maven?項(xiàng)目用Assembly打包可執(zhí)行jar包的方法,該方法只可打包非spring項(xiàng)目的可執(zhí)行jar包,需要的朋友可以參考下
    2023-03-03
  • Java讀文件修改默認(rèn)換行符的實(shí)現(xiàn)

    Java讀文件修改默認(rèn)換行符的實(shí)現(xiàn)

    這篇文章主要介紹了Java讀文件修改默認(rèn)換行符的實(shí)現(xiàn)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-12-12
  • spring依賴注入知識(shí)點(diǎn)分享

    spring依賴注入知識(shí)點(diǎn)分享

    在本篇文章里小編給大家整理的是關(guān)于spring依賴注入知識(shí)點(diǎn)以及相關(guān)代碼內(nèi)容,需要的朋友們學(xué)習(xí)下。
    2019-11-11
  • Java 類與對(duì)象超基礎(chǔ)講解

    Java 類與對(duì)象超基礎(chǔ)講解

    類(class)和對(duì)象(object)是兩種以計(jì)算機(jī)為載體的計(jì)算機(jī)語(yǔ)言的合稱。對(duì)象是對(duì)客觀事物的抽象,類是對(duì)對(duì)象的抽象。類是一種抽象的數(shù)據(jù)類型
    2022-03-03
  • 解決idea配置Tomcat Deployment沒(méi)有artifact選項(xiàng)的問(wèn)題

    解決idea配置Tomcat Deployment沒(méi)有artifact選項(xiàng)的問(wèn)題

    今天在配置的時(shí)候tomcat deployment中卻找不到artifact,沒(méi)有artifact就不能打成war包上傳到服務(wù)器了,那么怎么解決沒(méi)有artifact選項(xiàng)的問(wèn)題呢,今天通過(guò)本文給大家分享idea配置Tomcat Deployment沒(méi)有artifact選項(xiàng)的解決方案,一起看看吧
    2023-10-10
  • 詳解Maven optional關(guān)鍵字透徹圖解

    詳解Maven optional關(guān)鍵字透徹圖解

    這篇文章主要介紹了詳解Maven optional關(guān)鍵字透徹圖解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-11-11
  • 一文帶你搞懂SpringBoot中自動(dòng)裝配原理

    一文帶你搞懂SpringBoot中自動(dòng)裝配原理

    這篇文章主要為大家詳細(xì)介紹了SpringBoot中自動(dòng)裝配原理的相關(guān)知識(shí),文中的示例代碼講解詳細(xì),具有一定的借鑒價(jià)值,有需要的小伙伴可以參考下
    2025-01-01
  • Groovy動(dòng)態(tài)語(yǔ)言使用教程簡(jiǎn)介

    Groovy動(dòng)態(tài)語(yǔ)言使用教程簡(jiǎn)介

    這篇文章主要為大家介紹了Groovy動(dòng)態(tài)語(yǔ)言使用教程簡(jiǎn)介,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-09-09

最新評(píng)論

赣榆县| 论坛| 布尔津县| 马关县| 兰西县| 蒙城县| 浦江县| 宜宾县| 新邵县| 峡江县| 阳曲县| 晋中市| 湟中县| 渝北区| 临海市| 屏东县| 永兴县| 禹州市| 晋中市| 南雄市| 延吉市| 元谋县| 宁波市| 晋宁县| 太仆寺旗| 富蕴县| 临清市| 上虞市| 宜兰市| 永川市| 家居| 郸城县| 伽师县| 农安县| 舞阳县| 杂多县| 建湖县| 武城县| 盐池县| 手游| 任丘市|