Initial commit
This commit is contained in:
commit
eb197492cf
8
.idea/.gitignore
generated
vendored
Normal file
8
.idea/.gitignore
generated
vendored
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
# Default ignored files
|
||||||
|
/shelf/
|
||||||
|
/workspace.xml
|
||||||
|
# Editor-based HTTP Client requests
|
||||||
|
/httpRequests/
|
||||||
|
# Datasource local storage ignored files
|
||||||
|
/dataSources/
|
||||||
|
/dataSources.local.xml
|
||||||
6
.idea/ApifoxUploaderProjectSetting.xml
generated
Normal file
6
.idea/ApifoxUploaderProjectSetting.xml
generated
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="ApifoxUploaderProjectSetting">
|
||||||
|
<option name="apiAccessToken" value="APS-kTi1iLYRpD79UlTNmiJWNiXwzFXKRg6w" />
|
||||||
|
</component>
|
||||||
|
</project>
|
||||||
18
.idea/compiler.xml
generated
Normal file
18
.idea/compiler.xml
generated
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="CompilerConfiguration">
|
||||||
|
<annotationProcessing>
|
||||||
|
<profile name="Maven default annotation processors profile" enabled="true">
|
||||||
|
<sourceOutputDir name="target/generated-sources/annotations" />
|
||||||
|
<sourceTestOutputDir name="target/generated-test-sources/test-annotations" />
|
||||||
|
<outputRelativeToContentRoot value="true" />
|
||||||
|
<module name="email-scheduler" />
|
||||||
|
</profile>
|
||||||
|
</annotationProcessing>
|
||||||
|
</component>
|
||||||
|
<component name="JavacSettings">
|
||||||
|
<option name="ADDITIONAL_OPTIONS_OVERRIDE">
|
||||||
|
<module name="email-scheduler" options="-parameters" />
|
||||||
|
</option>
|
||||||
|
</component>
|
||||||
|
</project>
|
||||||
6
.idea/encodings.xml
generated
Normal file
6
.idea/encodings.xml
generated
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="Encoding">
|
||||||
|
<file url="file://$PROJECT_DIR$/src/main/java" charset="UTF-8" />
|
||||||
|
</component>
|
||||||
|
</project>
|
||||||
6
.idea/fastRequest/fastRequestCurrentProjectLocalConfig.xml
generated
Normal file
6
.idea/fastRequest/fastRequestCurrentProjectLocalConfig.xml
generated
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="fastRequestCurrentProjectLocalConfig">
|
||||||
|
<option name="initFlag" value="1" />
|
||||||
|
</component>
|
||||||
|
</project>
|
||||||
6
.idea/git_toolbox_blame.xml
generated
Normal file
6
.idea/git_toolbox_blame.xml
generated
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="GitToolBoxBlameSettings">
|
||||||
|
<option name="version" value="2" />
|
||||||
|
</component>
|
||||||
|
</project>
|
||||||
9
.idea/inspectionProfiles/Project_Default.xml
generated
Normal file
9
.idea/inspectionProfiles/Project_Default.xml
generated
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
<component name="InspectionProjectProfileManager">
|
||||||
|
<profile version="1.0">
|
||||||
|
<option name="myName" value="Project Default" />
|
||||||
|
<inspection_tool class="JavadocDeclaration" enabled="true" level="WARNING" enabled_by_default="true">
|
||||||
|
<option name="ADDITIONAL_TAGS" value="TableName" />
|
||||||
|
</inspection_tool>
|
||||||
|
<inspection_tool class="SerializableHasSerialVersionUIDField" enabled="true" level="WARNING" enabled_by_default="true" />
|
||||||
|
</profile>
|
||||||
|
</component>
|
||||||
20
.idea/jarRepositories.xml
generated
Normal file
20
.idea/jarRepositories.xml
generated
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="RemoteRepositoriesConfiguration">
|
||||||
|
<remote-repository>
|
||||||
|
<option name="id" value="central" />
|
||||||
|
<option name="name" value="Central Repository" />
|
||||||
|
<option name="url" value="http://maven.aliyun.com/nexus/content/repositories/central/" />
|
||||||
|
</remote-repository>
|
||||||
|
<remote-repository>
|
||||||
|
<option name="id" value="central" />
|
||||||
|
<option name="name" value="Maven Central repository" />
|
||||||
|
<option name="url" value="https://repo1.maven.org/maven2" />
|
||||||
|
</remote-repository>
|
||||||
|
<remote-repository>
|
||||||
|
<option name="id" value="jboss.community" />
|
||||||
|
<option name="name" value="JBoss Community repository" />
|
||||||
|
<option name="url" value="https://repository.jboss.org/nexus/content/repositories/public/" />
|
||||||
|
</remote-repository>
|
||||||
|
</component>
|
||||||
|
</project>
|
||||||
31
.idea/misc.xml
generated
Normal file
31
.idea/misc.xml
generated
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="ExternalStorageConfigurationManager" enabled="true" />
|
||||||
|
<component name="MavenProjectsManager">
|
||||||
|
<option name="originalFiles">
|
||||||
|
<list>
|
||||||
|
<option value="$PROJECT_DIR$/pom.xml" />
|
||||||
|
</list>
|
||||||
|
</option>
|
||||||
|
</component>
|
||||||
|
<component name="ProjectInspectionProfilesVisibleTreeState">
|
||||||
|
<entry key="Project Default">
|
||||||
|
<profile-state>
|
||||||
|
<expanded-state>
|
||||||
|
<State>
|
||||||
|
<id>Java</id>
|
||||||
|
</State>
|
||||||
|
<State>
|
||||||
|
<id>性能Java</id>
|
||||||
|
</State>
|
||||||
|
</expanded-state>
|
||||||
|
<selected-state>
|
||||||
|
<State>
|
||||||
|
<id>用户定义</id>
|
||||||
|
</State>
|
||||||
|
</selected-state>
|
||||||
|
</profile-state>
|
||||||
|
</entry>
|
||||||
|
</component>
|
||||||
|
<component name="ProjectRootManager" version="2" languageLevel="JDK_17" default="true" project-jdk-name="17" project-jdk-type="JavaSDK" />
|
||||||
|
</project>
|
||||||
4
.idea/vcs.xml
generated
Normal file
4
.idea/vcs.xml
generated
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="VcsDirectoryMappings" defaultProject="true" />
|
||||||
|
</project>
|
||||||
50
pom.xml
Normal file
50
pom.xml
Normal file
@ -0,0 +1,50 @@
|
|||||||
|
<?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>com.example</groupId>
|
||||||
|
<artifactId>email-scheduler</artifactId>
|
||||||
|
<version>1.0-SNAPSHOT</version>
|
||||||
|
|
||||||
|
<parent>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-parent</artifactId>
|
||||||
|
<version>2.7.0</version>
|
||||||
|
</parent>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<java.version>1.8</java.version>
|
||||||
|
</properties>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<!-- Spring Boot Starter -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-web</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<!-- Spring Boot Mail Starter -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-mail</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<!-- Lombok -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.projectlombok</groupId>
|
||||||
|
<artifactId>lombok</artifactId>
|
||||||
|
<optional>true</optional>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
<build>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
</project>
|
||||||
13
src/main/java/com/example/EmailSchedulerApplication.java
Normal file
13
src/main/java/com/example/EmailSchedulerApplication.java
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
package com.example;
|
||||||
|
|
||||||
|
import org.springframework.boot.SpringApplication;
|
||||||
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
|
import org.springframework.scheduling.annotation.EnableScheduling;
|
||||||
|
|
||||||
|
@SpringBootApplication
|
||||||
|
@EnableScheduling // 启用定时任务
|
||||||
|
public class EmailSchedulerApplication {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
SpringApplication.run(EmailSchedulerApplication.class, args);
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,76 @@
|
|||||||
|
package com.example.controller;
|
||||||
|
|
||||||
|
import com.example.service.EmailService;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
|
||||||
|
@Slf4j
|
||||||
|
@RestController
|
||||||
|
@RequestMapping("/api/email")
|
||||||
|
public class EmailTestController {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private EmailService emailService;
|
||||||
|
|
||||||
|
@GetMapping("/send")
|
||||||
|
public String sendTestEmail(@RequestParam(defaultValue = "test@example.com") String to) {
|
||||||
|
try {
|
||||||
|
emailService.sendSimpleEmail(
|
||||||
|
to,
|
||||||
|
"测试邮件",
|
||||||
|
"这是一封测试邮件,发送时间:" + java.time.LocalDateTime.now()
|
||||||
|
);
|
||||||
|
return "邮件发送成功!";
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.error("发送测试邮件失败", e);
|
||||||
|
return "邮件发送失败:" + e.getMessage();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@PostMapping("/send-custom")
|
||||||
|
public String sendCustomEmail(@RequestBody EmailRequest request) {
|
||||||
|
try {
|
||||||
|
emailService.sendSimpleEmail(
|
||||||
|
request.getTo(),
|
||||||
|
request.getSubject(),
|
||||||
|
request.getContent()
|
||||||
|
);
|
||||||
|
return "自定义邮件发送成功!";
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.error("发送自定义邮件失败", e);
|
||||||
|
return "邮件发送失败:" + e.getMessage();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class EmailRequest {
|
||||||
|
private String to;
|
||||||
|
private String subject;
|
||||||
|
private String content;
|
||||||
|
|
||||||
|
// Getters and Setters
|
||||||
|
public String getTo() {
|
||||||
|
return to;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setTo(String to) {
|
||||||
|
this.to = to;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getSubject() {
|
||||||
|
return subject;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setSubject(String subject) {
|
||||||
|
this.subject = subject;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getContent() {
|
||||||
|
return content;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setContent(String content) {
|
||||||
|
this.content = content;
|
||||||
|
}
|
||||||
|
}
|
||||||
64
src/main/java/com/example/scheduler/EmailScheduler.java
Normal file
64
src/main/java/com/example/scheduler/EmailScheduler.java
Normal file
@ -0,0 +1,64 @@
|
|||||||
|
package com.example.scheduler;
|
||||||
|
|
||||||
|
import com.example.service.EmailService;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.scheduling.annotation.Scheduled;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
import java.time.format.DateTimeFormatter;
|
||||||
|
|
||||||
|
@Slf4j
|
||||||
|
@Component
|
||||||
|
public class EmailScheduler {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private EmailService emailService;
|
||||||
|
|
||||||
|
// 方式1:使用cron表达式(每天上午10:15执行)
|
||||||
|
@Scheduled(cron = "0 15 10 * * ?")
|
||||||
|
public void scheduleDailyEmail() {
|
||||||
|
log.info("执行每日定时邮件发送任务");
|
||||||
|
emailService.sendSimpleEmail(
|
||||||
|
"recipient@example.com",
|
||||||
|
"每日报告",
|
||||||
|
"这是每天上午10:15发送的定时邮件"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 方式2:固定速率执行(每5分钟执行一次)
|
||||||
|
@Scheduled(fixedRate = 300000) // 300000毫秒 = 5分钟
|
||||||
|
public void scheduleFixedRateEmail() {
|
||||||
|
log.info("执行固定速率邮件发送任务");
|
||||||
|
String currentTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
|
||||||
|
emailService.sendSimpleEmail(
|
||||||
|
"recipient@example.com",
|
||||||
|
"固定速率邮件",
|
||||||
|
"这是每5分钟发送一次的邮件,当前时间:" + currentTime
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 方式3:固定延迟执行(上一次执行完成后延迟1小时执行)
|
||||||
|
@Scheduled(fixedDelay = 3600000) // 3600000毫秒 = 1小时
|
||||||
|
public void scheduleFixedDelayEmail() {
|
||||||
|
log.info("执行固定延迟邮件发送任务");
|
||||||
|
emailService.sendSimpleEmail(
|
||||||
|
"recipient@example.com",
|
||||||
|
"固定延迟邮件",
|
||||||
|
"这是上一次执行完成后延迟1小时发送的邮件"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 方式4:组合使用initialDelay和fixedRate
|
||||||
|
// 程序启动后等待1分钟,之后每30分钟执行一次
|
||||||
|
@Scheduled(initialDelay = 60000, fixedRate = 1800000)
|
||||||
|
public void scheduleWithInitialDelayEmail() {
|
||||||
|
log.info("执行初始延迟后的定时邮件发送任务");
|
||||||
|
emailService.sendSimpleEmail(
|
||||||
|
"recipient@example.com",
|
||||||
|
"定时邮件",
|
||||||
|
"这是启动后延迟1分钟,之后每30分钟发送一次的邮件"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
34
src/main/java/com/example/service/EmailService.java
Normal file
34
src/main/java/com/example/service/EmailService.java
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
package com.example.service;
|
||||||
|
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
|
import org.springframework.mail.SimpleMailMessage;
|
||||||
|
import org.springframework.mail.javamail.JavaMailSender;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
@Slf4j
|
||||||
|
@Service
|
||||||
|
public class EmailService {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private JavaMailSender mailSender;
|
||||||
|
|
||||||
|
@Value("${spring.mail.username}")
|
||||||
|
private String fromEmail;
|
||||||
|
|
||||||
|
public void sendSimpleEmail(String to, String subject, String content) {
|
||||||
|
SimpleMailMessage message = new SimpleMailMessage();
|
||||||
|
message.setFrom(fromEmail); // 使用配置文件中的邮箱地址
|
||||||
|
message.setTo(to);
|
||||||
|
message.setSubject(subject);
|
||||||
|
message.setText(content);
|
||||||
|
|
||||||
|
try {
|
||||||
|
mailSender.send(message);
|
||||||
|
log.info("邮件发送成功");
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.error("邮件发送失败: " + e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
21
src/main/resources/application.yml
Normal file
21
src/main/resources/application.yml
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
spring:
|
||||||
|
# 邮件配置
|
||||||
|
mail:
|
||||||
|
host: us1.workspace.org # 邮件服务器地址
|
||||||
|
port: 587 # 端口号
|
||||||
|
username: nnbwo7zjhg5sg@orinme.com
|
||||||
|
password: nnbwo7zjhg5sg
|
||||||
|
properties:
|
||||||
|
mail:
|
||||||
|
smtp:
|
||||||
|
auth: true
|
||||||
|
starttls:
|
||||||
|
enable: true
|
||||||
|
required: true
|
||||||
|
|
||||||
|
# 定时任务配置
|
||||||
|
task:
|
||||||
|
scheduling:
|
||||||
|
pool:
|
||||||
|
size: 5 # 定时任务线程池大小
|
||||||
|
thread-name-prefix: scheduled-task- # 定时任务线程名称前缀
|
||||||
21
target/classes/application.yml
Normal file
21
target/classes/application.yml
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
spring:
|
||||||
|
# 邮件配置
|
||||||
|
mail:
|
||||||
|
host: us1.workspace.org # 邮件服务器地址
|
||||||
|
port: 587 # 端口号
|
||||||
|
username: nnbwo7zjhg5sg@orinme.com
|
||||||
|
password: nnbwo7zjhg5sg
|
||||||
|
properties:
|
||||||
|
mail:
|
||||||
|
smtp:
|
||||||
|
auth: true
|
||||||
|
starttls:
|
||||||
|
enable: true
|
||||||
|
required: true
|
||||||
|
|
||||||
|
# 定时任务配置
|
||||||
|
task:
|
||||||
|
scheduling:
|
||||||
|
pool:
|
||||||
|
size: 5 # 定时任务线程池大小
|
||||||
|
thread-name-prefix: scheduled-task- # 定时任务线程名称前缀
|
||||||
BIN
target/classes/com/example/EmailSchedulerApplication.class
Normal file
BIN
target/classes/com/example/EmailSchedulerApplication.class
Normal file
Binary file not shown.
BIN
target/classes/com/example/controller/EmailRequest.class
Normal file
BIN
target/classes/com/example/controller/EmailRequest.class
Normal file
Binary file not shown.
BIN
target/classes/com/example/controller/EmailTestController.class
Normal file
BIN
target/classes/com/example/controller/EmailTestController.class
Normal file
Binary file not shown.
BIN
target/classes/com/example/scheduler/EmailScheduler.class
Normal file
BIN
target/classes/com/example/scheduler/EmailScheduler.class
Normal file
Binary file not shown.
BIN
target/classes/com/example/service/EmailService.class
Normal file
BIN
target/classes/com/example/service/EmailService.class
Normal file
Binary file not shown.
Loading…
x
Reference in New Issue
Block a user