基于Springboot實(shí)現(xiàn)定時(shí)發(fā)送郵件功能
1、功能概述
1、在企業(yè)中有很多需要定時(shí)提醒的任務(wù):如每天下午四點(diǎn)鐘給第二天的值班人員發(fā)送值班消息?如提前一天給參與第二天會(huì)議的人員發(fā)送參會(huì)消息等。
2、這種定時(shí)提醒有很多方式如短信提醒、站內(nèi)提醒等郵件提醒是其中較為方便且廉價(jià)的方式。
3、本案例中主要使用基于Springboot工程和JavaMail技術(shù)及spring中的定時(shí)任務(wù)實(shí)現(xiàn),當(dāng)前這里的定時(shí)任務(wù)也可以換成分布式的定時(shí)任務(wù)如quartz等,本案例中不在此闡述。
2、定時(shí)郵件任務(wù)具體實(shí)現(xiàn)過程
2.1、創(chuàng)建springboot工程導(dǎo)入相關(guān)jar包信息
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.9</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.hazq</groupId>
<artifactId>hazqoasystem</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>hazqoasystem</name>
<description>hazqoasystem</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>2.2、在application.yml文件中添加配置信息

host:表使用的郵件服務(wù)器,我們這里選擇使用的是qq的郵件服務(wù)器。
username:你想通過那個(gè)郵箱作為主體向其他郵箱發(fā)送郵件。
password:這個(gè)密碼不是登錄密碼,而是授權(quán)碼。
&:password授權(quán)碼獲取方式
【第一步:登錄qq郵箱,點(diǎn)擊設(shè)置】

【第二步:點(diǎn)擊賬號(hào),開啟服務(wù)】

【第三步:通過微信掃描,發(fā)送短信】

【第四步:開啟服務(wù)成功】
紅框的位置就是我們需要的password

2.3、創(chuàng)建定時(shí)任務(wù)發(fā)送郵件
@Slf4j
//加載類型開啟類中,加載啟動(dòng)類上,開啟整個(gè)項(xiàng)目
@EnableScheduling //是否開啟
@Component
public class DutyScheduledConfig {
@Autowired
private JavaMailSender javaMailSender;//郵件發(fā)送引擎
@Value("${spring.mail.username}")
private String setFromEmail;
//每天下午16:30分發(fā)送郵件消息。
@Scheduled(cron="0 30 16 * * ?")
public void process(){
SimpleMailMessage message = new SimpleMailMessage();
message.setFrom(setFromEmail);
message.setTo(“123456@qq.com”);//你想發(fā)郵件給誰。
message.setSubject("值班通知");//設(shè)置郵件主題
message.setText("這里寫的是郵件的內(nèi)容。");
javaMailSender.send(message);
System.out.print(“郵件發(fā)送成功!”);
}
}2.4、其他定時(shí)公式
這個(gè)網(wǎng)站可以自定義設(shè)置執(zhí)行的時(shí)間,網(wǎng)站中還提供了大量的現(xiàn)成案例。
到此這篇關(guān)于基于Springboot實(shí)現(xiàn)定時(shí)發(fā)送郵件功能的文章就介紹到這了,更多相關(guān)Springboot定時(shí)發(fā)送郵件內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Spring?Boot?實(shí)現(xiàn)Redis分布式鎖原理
這篇文章主要介紹了Spring?Boot實(shí)現(xiàn)Redis分布式鎖原理,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的朋友可以參考一下2022-08-08
sftp和ftp 根據(jù)配置遠(yuǎn)程服務(wù)器地址下載文件到當(dāng)前服務(wù)
這篇文章主要介紹了sftp和ftp 根據(jù)配置遠(yuǎn)程服務(wù)器地址下載文件到當(dāng)前服務(wù)的相關(guān)資料本文給大家介紹的非常詳細(xì),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-10-10
springMVC如何將controller中Model數(shù)據(jù)傳遞到j(luò)sp頁面
本篇文章主要介紹了springMVC如何將controller中Model數(shù)據(jù)傳遞到j(luò)sp頁面,具有一定的參考價(jià)值,有興趣的可以了解一下2017-07-07
Spring AI與DeepSeek實(shí)戰(zhàn)一之快速打造智能對(duì)話應(yīng)用
本文詳細(xì)介紹了如何通過SpringAI框架集成DeepSeek大模型,實(shí)現(xiàn)普通對(duì)話和流式對(duì)話功能,步驟包括申請(qǐng)API-KEY、項(xiàng)目搭建、配置API-KEY、創(chuàng)建ChatClient對(duì)象、創(chuàng)建對(duì)話接口、切換模型、使用prompt模板、流式對(duì)話等,感興趣的朋友一起看看吧2025-03-03

