commit eb197492cfa559b75492b47a3ff4063f3f94335b Author: HP Date: Wed Mar 5 01:37:09 2025 +0800 Initial commit diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..13566b8 --- /dev/null +++ b/.idea/.gitignore @@ -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 diff --git a/.idea/ApifoxUploaderProjectSetting.xml b/.idea/ApifoxUploaderProjectSetting.xml new file mode 100644 index 0000000..2cb57cc --- /dev/null +++ b/.idea/ApifoxUploaderProjectSetting.xml @@ -0,0 +1,6 @@ + + + + + \ No newline at end of file diff --git a/.idea/compiler.xml b/.idea/compiler.xml new file mode 100644 index 0000000..936177e --- /dev/null +++ b/.idea/compiler.xml @@ -0,0 +1,18 @@ + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/encodings.xml b/.idea/encodings.xml new file mode 100644 index 0000000..63e9001 --- /dev/null +++ b/.idea/encodings.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/fastRequest/fastRequestCurrentProjectLocalConfig.xml b/.idea/fastRequest/fastRequestCurrentProjectLocalConfig.xml new file mode 100644 index 0000000..e62f560 --- /dev/null +++ b/.idea/fastRequest/fastRequestCurrentProjectLocalConfig.xml @@ -0,0 +1,6 @@ + + + + + \ No newline at end of file diff --git a/.idea/git_toolbox_blame.xml b/.idea/git_toolbox_blame.xml new file mode 100644 index 0000000..7dc1249 --- /dev/null +++ b/.idea/git_toolbox_blame.xml @@ -0,0 +1,6 @@ + + + + + \ No newline at end of file diff --git a/.idea/inspectionProfiles/Project_Default.xml b/.idea/inspectionProfiles/Project_Default.xml new file mode 100644 index 0000000..e8669f3 --- /dev/null +++ b/.idea/inspectionProfiles/Project_Default.xml @@ -0,0 +1,9 @@ + + + + \ No newline at end of file diff --git a/.idea/jarRepositories.xml b/.idea/jarRepositories.xml new file mode 100644 index 0000000..be5f5c6 --- /dev/null +++ b/.idea/jarRepositories.xml @@ -0,0 +1,20 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..030ca27 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,31 @@ + + + + + + + + + + + + Java + + + 性能Java + + + + + 用户定义 + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..d843f34 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..37ee8b7 --- /dev/null +++ b/pom.xml @@ -0,0 +1,50 @@ + + + 4.0.0 + + com.example + email-scheduler + 1.0-SNAPSHOT + + + org.springframework.boot + spring-boot-starter-parent + 2.7.0 + + + + 1.8 + + + + + + org.springframework.boot + spring-boot-starter-web + + + + + org.springframework.boot + spring-boot-starter-mail + + + + + org.projectlombok + lombok + true + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + \ No newline at end of file diff --git a/src/main/java/com/example/EmailSchedulerApplication.java b/src/main/java/com/example/EmailSchedulerApplication.java new file mode 100644 index 0000000..3e4cdfd --- /dev/null +++ b/src/main/java/com/example/EmailSchedulerApplication.java @@ -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); + } +} \ No newline at end of file diff --git a/src/main/java/com/example/controller/EmailTestController.java b/src/main/java/com/example/controller/EmailTestController.java new file mode 100644 index 0000000..5cc94d2 --- /dev/null +++ b/src/main/java/com/example/controller/EmailTestController.java @@ -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; + } +} \ No newline at end of file diff --git a/src/main/java/com/example/scheduler/EmailScheduler.java b/src/main/java/com/example/scheduler/EmailScheduler.java new file mode 100644 index 0000000..bdc6a96 --- /dev/null +++ b/src/main/java/com/example/scheduler/EmailScheduler.java @@ -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分钟发送一次的邮件" + ); + } +} \ No newline at end of file diff --git a/src/main/java/com/example/service/EmailService.java b/src/main/java/com/example/service/EmailService.java new file mode 100644 index 0000000..bb40380 --- /dev/null +++ b/src/main/java/com/example/service/EmailService.java @@ -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()); + } + } +} \ No newline at end of file diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml new file mode 100644 index 0000000..3828747 --- /dev/null +++ b/src/main/resources/application.yml @@ -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- # 定时任务线程名称前缀 \ No newline at end of file diff --git a/target/classes/application.yml b/target/classes/application.yml new file mode 100644 index 0000000..3828747 --- /dev/null +++ b/target/classes/application.yml @@ -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- # 定时任务线程名称前缀 \ No newline at end of file diff --git a/target/classes/com/example/EmailSchedulerApplication.class b/target/classes/com/example/EmailSchedulerApplication.class new file mode 100644 index 0000000..43faf84 Binary files /dev/null and b/target/classes/com/example/EmailSchedulerApplication.class differ diff --git a/target/classes/com/example/controller/EmailRequest.class b/target/classes/com/example/controller/EmailRequest.class new file mode 100644 index 0000000..86f2fa4 Binary files /dev/null and b/target/classes/com/example/controller/EmailRequest.class differ diff --git a/target/classes/com/example/controller/EmailTestController.class b/target/classes/com/example/controller/EmailTestController.class new file mode 100644 index 0000000..6ed5696 Binary files /dev/null and b/target/classes/com/example/controller/EmailTestController.class differ diff --git a/target/classes/com/example/scheduler/EmailScheduler.class b/target/classes/com/example/scheduler/EmailScheduler.class new file mode 100644 index 0000000..119b190 Binary files /dev/null and b/target/classes/com/example/scheduler/EmailScheduler.class differ diff --git a/target/classes/com/example/service/EmailService.class b/target/classes/com/example/service/EmailService.class new file mode 100644 index 0000000..cf7a342 Binary files /dev/null and b/target/classes/com/example/service/EmailService.class differ