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

基于Springboot實(shí)現(xiàn)定時(shí)發(fā)送郵件功能

 更新時(shí)間:2024年03月19日 15:59:44   作者:霧林小妖  
這篇文章主要為大家詳細(xì)介紹了基于Springboot實(shí)現(xiàn)定時(shí)發(fā)送郵件功能的相關(guān)知識(shí),文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下

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)過(guò)程

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:你想通過(guò)那個(gè)郵箱作為主體向其他郵箱發(fā)送郵件。

password:這個(gè)密碼不是登錄密碼,而是授權(quán)碼。

&password授權(quán)碼獲取方式

 【第一步:登錄qq郵箱,點(diǎn)擊設(shè)置】

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

【第三步:通過(guò)微信掃描,發(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ā)郵件給誰(shuí)。
      message.setSubject("值班通知");//設(shè)置郵件主題
      message.setText("這里寫的是郵件的內(nèi)容。");
      javaMailSender.send(message);
      System.out.print(“郵件發(fā)送成功!”);
    }
}

2.4、其他定時(shí)公式

https://cron.qqe2.com/

這個(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)文章

最新評(píng)論