亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

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

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

1、功能概述

1、在企業(yè)中有很多需要定時提醒的任務:如每天下午四點鐘給第二天的值班人員發(fā)送值班消息?如提前一天給參與第二天會議的人員發(fā)送參會消息等。

2、這種定時提醒有很多方式如短信提醒、站內(nèi)提醒等郵件提醒是其中較為方便且廉價的方式。

3、本案例中主要使用基于Springboot工程和JavaMail技術及spring中的定時任務實現(xiàn),當前這里的定時任務也可以換成分布式的定時任務如quartz等,本案例中不在此闡述。

2、定時郵件任務具體實現(xiàn)過程

2.1、創(chuàng)建springboot工程導入相關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:表使用的郵件服務器,我們這里選擇使用的是qq的郵件服務器。

username:你想通過那個郵箱作為主體向其他郵箱發(fā)送郵件。

password:這個密碼不是登錄密碼,而是授權碼。

&password授權碼獲取方式

 【第一步:登錄qq郵箱,點擊設置】

【第二步:點擊賬號,開啟服務】

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

【第四步:開啟服務成功】

紅框的位置就是我們需要的password

2.3、創(chuàng)建定時任務發(fā)送郵件

@Slf4j
//加載類型開啟類中,加載啟動類上,開啟整個項目
@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("值班通知");//設置郵件主題
      message.setText("這里寫的是郵件的內(nèi)容。");
      javaMailSender.send(message);
      System.out.print(“郵件發(fā)送成功!”);
    }
}

2.4、其他定時公式

https://cron.qqe2.com/

這個網(wǎng)站可以自定義設置執(zhí)行的時間,網(wǎng)站中還提供了大量的現(xiàn)成案例。

到此這篇關于基于Springboot實現(xiàn)定時發(fā)送郵件功能的文章就介紹到這了,更多相關Springboot定時發(fā)送郵件內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

最新評論