Springboot定时界面发送邮件

添加mail依赖:

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-mail</artifactId>
        </dependency>

配置全局文件:

# 发件人邮服务器相关配置
spring.mail.host=smtp.qq.com
spring.mail.port=587
# 配置个人QQ账户和密码(密码是加密后的授权码)
spring.mail.username=xxxxxx@qq.com
spring.mail.password=xxxxxxxx
spring.mail.default-encoding=UTF-8

HTML文件:

</head>
<body>
<div>
    <form action="/SEmail" method="get">
        <h1>发送定时邮件</h1>
        <div th:if="${uploadStatus}" style="color: red" th:text="${uploadStatus}"></div>
        <label for="recipient">收件人:</label><br>
        <input type="text" id="recipient" name="recipient"><br><br>
        <label for="text">内容:</label><br>
        <textarea id="text" name="text" rows="4" cols="50"></textarea><br><br>
        <label for="sentDate">发送时间:</label><br>
        <input type="datetime-local" id="sentDate" name="sentDate" οnchange="showTime(this)"><br><br>
        <div id="time"></div>

        <script>
            function showTime(input) {
                var time = document.getElementById("time");
                time.innerHTML = "时间: " + input.value;
            }
        </script>

        <input type="submit" value="发送邮件">
    </form>
</div>
<br><br>
<button οnclick="window.history.back()"
        style="background-color: #5f9ea0; color: white; border: none; padding: 10px 20px;font-size: 14px;">返回上一步
</button>
<a href="/"
   style="background-color: #5f9ea0; color: white; border: none; padding: 10px 20px; font-size: 14px; text-decoration: none;">返回首页</a>

</body>
</html>

后端控制代码

@Controller
public class scheduleEmail {
    private JavaMailSender emailSender;
    @Autowired
    private JavaMailSenderImpl mailSender; // 自动注入邮件发送器
    @Value("${spring.mail.username}")
    private String form; // 从配置文件中读取发件人邮箱地址

    @ResponseBody
    @GetMapping("/SEmail")
    public Object addStudentDispose(@RequestParam String recipient, @RequestParam String text, @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd'T'HH:mm") LocalDateTime sentDate) {

        System.out.println(recipient);
        System.out.println(sentDate);
        System.out.println(text);
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom(form); // 设置发件人
        message.setTo(recipient); // 设置收件人
        message.setText(text); // 设置邮件内容

        // 设置邮件发送时间
        Date date = toDate(sentDate);
        ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor();
        executorService.schedule(() -> {
            mailSender.send(message); // 发送邮件
        }, date.getTime() - System.currentTimeMillis(), TimeUnit.MILLISECONDS);


        try {
            System.out.println("定时邮件发送成功");
            ModelAndView mav = new ModelAndView("email/scheduleEmail");  //假设upload是你的视图名称
            mav.addObject("uploadStatus", "邮件发送成功,你将在时间收到邮件"+sentDate);  //你可以在视图中使用这些数据
            return mav;
        } catch (MailException e) {
            System.out.println("纯文本邮件发送失败 " + e.getMessage());
            e.printStackTrace();
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("邮件发送失败:" + e.getMessage()); // 返回错误信息给前端
        }
    }

    // 判断邮箱格式是否正确
    private boolean isValidEmail(String email) {
        String regex = "^[\\w-]+(\\.[\\w-]+)*@[\\w-]+(\\.[\\w-]+)+$";
        return email.matches(regex);
    }
    private Date toDate(LocalDateTime dateTime) {
        return Date.from(dateTime.atZone(ZoneId.systemDefault()).toInstant());
    }
}
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

yyyxx20222

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值