国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

Spring Boot 整合郵件服務

這篇具有很好參考價值的文章主要介紹了Spring Boot 整合郵件服務。希望對大家有所幫助。如果存在錯誤或未考慮完全的地方,請大家不吝賜教,您也可以點擊"舉報違法"按鈕提交疑問。

參考教程

首先參考了 Spring Boot整合郵件配置,這篇文章寫的很好,按照上面的操作一步步走下去就行了。

遇到的問題

版本配置

然后因為反復配置版本很麻煩,所以參考了 如何統(tǒng)一引入 Spring Boot 版本?。

FreeMarker

在配置 FreeMarker 時,發(fā)現(xiàn)找不到 FreeMarkerConfigurer 類,參考了 springboot整合Freemark模板(詳盡版) 發(fā)現(xiàn)要添加 web 模塊。

測試注解

在使用測試類的時候,我只添加了 @SpringBootTest 注解,報空指針,參考了 測試類的@RunWith與@SpringBootTest注解 發(fā)現(xiàn)還要添加 @RunWith(SpringRunner.class) 注解。

實踐結(jié)果

代碼地址

完成的項目地址。文章來源地址http://www.zghlxwxcb.cn/news/detail-431511.html

核心代碼

pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>fun.seolas</groupId>
    <artifactId>spring-boot-mail-demo</artifactId>
    <version>1.0-SNAPSHOT</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.2.RELEASE</version>
    </parent>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-mail</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-freemarker</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>
application.yaml
spring:
  mail:
    host: smtp.qq.com #發(fā)送郵件服務器
    username: xxx@qq.com #QQ郵箱
    password: xxx #客戶端授權(quán)碼
    protocol: smtp #發(fā)送郵件協(xié)議
    properties.mail.smtp.auth: true
    properties.mail.smtp.port: 465 #端口號465或587
    properties.mail.display.sendmail: aaa #可以任意
    properties.mail.display.sendname: bbb #可以任意
    properties.mail.smtp.starttls.enable: true
    properties.mail.smtp.starttls.required: true
    properties.mail.smtp.ssl.enable: true #開啟SSL
    default-encoding: utf-8
  freemarker:
    cache: false # 緩存配置 開發(fā)階段應該配置為false 因為經(jīng)常會改
    suffix: .html # 模版后綴名 默認為ftl
    charset: UTF-8 # 文件編碼
    template-loader-path: classpath:/templates/  # 存放模板的文件夾,以resource文件夾為相對路徑

my:
  toemail: xx@xx.com
MailService.java
package fun.seolas;

import freemarker.template.Template;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;

import javax.annotation.Resource;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;
import java.util.HashMap;
import java.util.Map;

@Service
public class MailService {
    // Spring官方提供的集成郵件服務的實現(xiàn)類,目前是Java后端發(fā)送郵件和集成郵件服務的主流工具。
    @Resource
    private JavaMailSender mailSender;
    @Autowired
    private FreeMarkerConfigurer freeMarkerConfigurer;
    // 從配置文件中注入發(fā)件人的姓名
    @Value("${spring.mail.username}")
    private String fromEmail;

    /**
     * 發(fā)送文本郵件
     *
     * @param to      收件人
     * @param subject 標題
     * @param content 正文
     */
    public void sendSimpleMail(String to, String subject, String content) {
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom(fromEmail); // 發(fā)件人
        message.setTo(to);
        message.setSubject(subject);
        message.setText(content);
        mailSender.send(message);
    }

    /**
     * 發(fā)送html郵件
     */
    public void sendHtmlMail(String to, String subject, String content) throws MessagingException {
        //注意這里使用的是MimeMessage
        MimeMessage message = mailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(message, true);
        helper.setFrom(fromEmail);
        helper.setTo(to);
        helper.setSubject(subject);
        //第二個參數(shù):格式是否為html
        helper.setText(content, true);
        mailSender.send(message);
    }

    /**
     * 發(fā)送freemarker郵件
     */
    public void sendTemplateMail(String to, String subject, String templatehtml) throws Exception {
        // 獲得模板
        Template template = freeMarkerConfigurer.getConfiguration().getTemplate(templatehtml);
        // 使用Map作為數(shù)據(jù)模型,定義屬性和值
        Map<String, Object> model = new HashMap<>();
        model.put("myname", "Seolas");
        // 傳入數(shù)據(jù)模型到模板,替代模板中的占位符,并將模板轉(zhuǎn)化為html字符串
        String templateHtml = FreeMarkerTemplateUtils.processTemplateIntoString(template, model);
        // 該方法本質(zhì)上還是發(fā)送html郵件,調(diào)用之前發(fā)送html郵件的方法
        this.sendHtmlMail(to, subject, templateHtml);
    }

    /**
     * 發(fā)送帶附件的郵件
     */
    public void sendAttachmentsMail(String to, String subject, String content, String filePath) throws MessagingException {
        MimeMessage message = mailSender.createMimeMessage();
        //要帶附件第二個參數(shù)設(shè)為true
        MimeMessageHelper helper = new MimeMessageHelper(message, true);
        helper.setFrom(fromEmail);
        helper.setTo(to);
        helper.setSubject(subject);
        helper.setText(content, true);

        FileSystemResource file = new FileSystemResource(new File(filePath));
        String fileName = filePath.substring(filePath.lastIndexOf(File.separator));
        helper.addAttachment(fileName, file);

        mailSender.send(message);
    }
}

MailTest.java
package fun.seolas;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import javax.mail.MessagingException;

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

    @Autowired
    private MailService mailService;

    @Value("${my.toemail}")
    private String toemail;

    @Test
    public void test01() {
        mailService.sendSimpleMail(toemail, "普通文本郵件", "普通文本郵件內(nèi)容");
    }

    @Test
    public void test02() throws MessagingException {
        mailService.sendHtmlMail(toemail, "一封html測試郵件",
                "<div style=\"text-align: center;position: absolute;\" >\n"
                        + "<h3>\"一封html測試郵件\"</h3>\n"
                        + "<div>一封html測試郵件</div>\n"
                        + "</div>");
    }

    @Test
    public void test3() throws Exception {
        mailService.sendTemplateMail(toemail, "基于模板的html郵件", "freemarkertemp.html");
    }

    @Test
    public void test04() throws MessagingException {
        String filePath = "C:\\Users\\Julia\\Downloads\\測試.txt";
        mailService.sendAttachmentsMail(toemail, "帶附件的郵件", "郵件中有附件", filePath);
    }

}

到了這里,關(guān)于Spring Boot 整合郵件服務的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權(quán),不承擔相關(guān)法律責任。如若轉(zhuǎn)載,請注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實不符,請點擊違法舉報進行投訴反饋,一經(jīng)查實,立即刪除!

領(lǐng)支付寶紅包贊助服務器費用

相關(guān)文章

  • 論如何本地搭建個人hMailServer郵件服務遠程發(fā)送郵件無需域名公網(wǎng)服務器?

    論如何本地搭建個人hMailServer郵件服務遠程發(fā)送郵件無需域名公網(wǎng)服務器?

    ???? 博主貓頭虎(????)帶您 Go to New World??? ?? 博客首頁 ——????貓頭虎的博客?? ?? 《面試題大全專欄》 ?? 文章圖文并茂??生動形象??簡單易學!歡迎大家來踩踩~?? ?? 《IDEA開發(fā)秘籍專欄》 ?? 學會IDEA常用操作,工作效率翻倍~?? ?? 《100天精通Golang(基礎(chǔ)

    2024年01月24日
    瀏覽(121)
  • Mail 郵件服務

    Mail 郵件服務

    ~?Postfix ~? ?sdskill.com 的郵件發(fā)送服務器 ~~? ?支持smtps(465)協(xié)議連接,使用Rserver頒發(fā)的證書,證書路徑/CA/cacert.pem ~? ? 創(chuàng)建郵箱賬戶“user1~user99”(共99個用戶),密碼為Chinaskill20!; ~? ? Dovecot ~? ? sdskill.com 的郵件接收服務器; ~? ? 支持imaps(993)協(xié)議連接,使用Rserver頒發(fā)

    2023年04月26日
    瀏覽(23)
  • 一、Postfix[安裝與配置、smtp認證、Python發(fā)送郵件以及防垃圾郵件方法、使用騰訊云郵件服務]

    一、Postfix[安裝與配置、smtp認證、Python發(fā)送郵件以及防垃圾郵件方法、使用騰訊云郵件服務]

    Debian 11 apt install postfix 解釋:搭建真實的郵件服務器需要在DNS提供商那里配置下面的dns 配置A記錄 mail.www.com - 1.x.x.x 配置MX記錄 www.com - mail.www.com 解釋:按照上面的配置通常郵件格式就是 admin@www.com 其通過www.com的MX記錄找到mail.www.com再通過其A記錄來找到對應服務器完成通訊 解

    2024年02月15日
    瀏覽(25)
  • 【服務器】搭建hMailServer 服務實現(xiàn)遠程發(fā)送郵件

    【服務器】搭建hMailServer 服務實現(xiàn)遠程發(fā)送郵件

    hMailServer 是一個郵件服務器,通過它我們可以搭建自己的郵件服務,通過cpolar內(nèi)網(wǎng)映射工具即可實現(xiàn)遠程發(fā)送郵件,不需要使用公網(wǎng)服務器,不需要域名,而且郵件賬號名稱可以自定義. 下面以windows 10系統(tǒng)為環(huán)境,介紹使用方法: 1. 安裝hMailServer 進入官方下載:https://www.hmailserver.com/do

    2024年02月10日
    瀏覽(25)
  • 電子郵件服務器

    電子郵件服務器

    目錄 一、相關(guān)知識 二、郵件服務器種類 三、郵件傳輸協(xié)議 四、DNS中的MX記錄 五、電子郵件系統(tǒng)工作原理 六、配置文件相關(guān)參數(shù) 七、郵件服務器配置案例 7.1設(shè)置用戶別名郵箱 7.2空殼郵件服務器 一、相關(guān)知識 1、電子郵箱系統(tǒng)三個組成部分 MUA(telnet):郵件用戶代理。主要用

    2024年02月10日
    瀏覽(33)
  • 【服務端】CentOS Linux 7 搭建郵件服務器

    【服務端】CentOS Linux 7 搭建郵件服務器

    參考:CentOS7搭建簡單的郵件服務器 - 秋夜雨巷 - 博客園 (cnblogs.com) 在 CentOS7 中搭建郵件服務器,給QQ郵箱發(fā)郵件。簡單記錄一次搭建過程。 目錄 前言 一、基礎(chǔ)環(huán)境準備 二、配置域名解析 1. 登錄阿里云 三、安裝郵件服務 1. 登錄主機,配置yum源(配置阿里云yum源步驟略) 2

    2024年01月22日
    瀏覽(40)
  • 郵件服務器配置和管理

    郵件服務器配置和管理

    一臺安裝好的DNS服務器,ip為192.168.1.201 一臺郵件服務器,192.168.1.224 一臺客戶端,192.168.1.249,dnsIP為192.168.1.201 都是wmnet1,使其能互相ping通 1.打開DNS服務器,新建主機 把郵件服務器的主機添加上去,使得客戶端可以通過域名找到郵件服務器 1.用實體機進入官網(wǎng)https://www.winma

    2024年03月19日
    瀏覽(30)
  • Spring Schedule:Spring boot整合Spring Schedule實戰(zhàn)講解定時發(fā)送郵件的功能

    Spring Schedule:Spring boot整合Spring Schedule實戰(zhàn)講解定時發(fā)送郵件的功能

    ???? 歡迎光臨,終于等到你啦 ???? ??我是 蘇澤 ,一位對技術(shù)充滿熱情的探索者和分享者。???? ??持續(xù)更新的專欄 《Spring 狂野之旅:從入門到入魔》 ?? 本專欄帶你從Spring入門到入魔 ? 這是蘇澤的個人主頁可以看到我其他的內(nèi)容哦???? 努力的蘇澤 http://suzee.blog.

    2024年03月14日
    瀏覽(22)
  • 使用CentOS 7配置郵件服務-第四篇

    使用CentOS 7配置郵件服務-第四篇

    在上一章我們配置主和從服務器的DNS域名解析,這一章我們配置使用CentOS 7配置郵件服務 要求: 1、兩臺郵件服務器IP地址分別為192.168.1.學號及192.168.0.199 2、 兩臺郵件服務器域名為學生姓名。例如mail.zhangsan.com 及mail.zs.com。 3、在DNS服務器配置兩臺服務器的域名。 4、配置完成

    2024年02月12日
    瀏覽(23)
  • 快速備份郵件到新服務器

    需求場景 自建郵局后,遇到了意料中的問題:遷移郵件到新郵局,即把原來郵箱里的郵件全部復制到新郵箱。 工具介紹 imapsync是一款強大的工具,用于同步兩個郵件服務器之間的數(shù)據(jù)。它支持IMAP協(xié)議,可以輕松地將郵件、文件夾、郵件標記為已讀/已刪除等。使用imapsync,可

    2024年02月12日
    瀏覽(18)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包