一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達(dá)式|C/C++|IOS|C#|Swift|Android|VB|R語(yǔ)言|JavaScript|易語(yǔ)言|vb.net|

服務(wù)器之家 - 編程語(yǔ)言 - Java教程 - Springboot實(shí)現(xiàn)發(fā)送郵件

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

2022-02-24 00:58玖月夢(mèng)沉 Java教程

這篇文章主要為大家詳細(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實(shí)現(xiàn)發(fā)送郵件

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

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

Springboot實(shí)現(xiàn)發(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
[email protected]
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("[email protected]","這是一個(gè)簡(jiǎn)單的demo","哈哈哈,發(fā)送成功了!");
    }
}

啟動(dòng):

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

效果:

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

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("[email protected]","這是一封附件郵件","哈哈哈,附件郵件發(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:UsersyzyDesktop微信圖片_20210917201828.jpg";
        String rsc = "0001";
        mailService.sendPhotoMail("[email protected]","這是一封圖片郵件","哈哈哈,圖片郵件發(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("[email protected]","這是一封模板郵件",emailContent);
    }

效果:

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

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

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

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

原文鏈接:https://blog.csdn.net/m0_45025997/article/details/120841109

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 亚洲精品免费观看 | 蜜桃成熟3在线观看 | 欧美三级不卡视频 | 99久热只有精品视频免费观看17 | 欧美福利在线播放 | 欧美成人免费观看久久 | 我的家教老师 | 日韩成人免费aa在线看 | 国产成人a∨麻豆精品 | 女子张腿让男人桶免费 | 娇妻被朋友征服中文字幕 | 五月色综合婷婷综合俺来也 | 洗濯屋动漫在线观看 | 蜜汁肉桃全文免费阅读 | 狠狠撸在线影院 | 亚洲玖玖| 欧美三级一区 | 欧美一级视频免费观看 | 亚洲国产成人在线视频 | 男生和老师一起差差差 | 欧美不卡一区二区三区免 | 日本高清免费看 | 国产成人综合久久精品红 | 亚洲国产精品热久久 | 美女舒服好紧太爽了视频 | 日韩精品一区二区三区中文字幕 | 色姑娘久久 | 九九精品视频在线观看九九 | 2021国产麻豆剧传媒新片 | 国产精品久久亚洲一区二区 | 大学生按摩黄a级中文片 | 91在线精品国产 | 亚洲国产第一 | 国产日韩欧美精品在线 | 扒开斗罗美女了的胸罩和内裤漫画 | 阿 好深 快点 老师受不了 | 99免费视频 | 成3d漫二区三区四区 | 草莓香蕉绿巨人丝瓜榴莲18 | 亚久久伊人精品青青草原2020 | 精品国产乱码久久久人妻 |