基于 Spring Boot 博客系统开发(十一)

基于 Spring Boot 博客系统开发(十一)

本系统是简易的个人博客系统开发,为了更加熟练地掌握 SprIng Boot 框架及相关技术的使用。🌿🌿🌿
基于 Spring Boot 博客系统开发(十)👈👈

定时邮件发送实现

邮件服务依赖

xml 复制代码
 <!-- mail邮件服务启动器 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-mail</artifactId>
        </dependency>

邮件服务配置

bash 复制代码
# 邮箱邮件发送服务配置
spring.mail.host=smtp.163.com

# 配置个人邮件账户和密码(密码是加密后的授权码)
spring.mail.username=xxxxxx@163.com
spring.mail.password=xxxxxx

163邮箱授权码获取:

登录163邮箱账号,进入设置开启 IMAP/SMTP服务和POP3/SMTP服务,然后点击新增授权码获取授权码。

邮件服务工具类

java 复制代码
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.stereotype.Component;

@Component
public class MailUtils {
    @Autowired
    private JavaMailSenderImpl mailSender;
    @Value("${spring.mail.username}")
    private String mailfrom;

    // 发送简单邮件
    public void sendSimpleEmail(String mailto, String title, String content) {
        //  定制邮件发送内容
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom(mailfrom);
        message.setTo(mailto);
        message.setSubject(title);
        message.setText(content);
        // 发送邮件
        mailSender.send(message);
    }
}

发送邮件测试

java 复制代码
@SpringBootTest
class BlogSystem01ApplicationTests {

    @Autowired
    private MailUtils mailUtils;
    @Value("${spring.mail.username}")
    private String mailto;

    @Test
    void contextLoads() {
        StringBuffer content = new StringBuffer();
        content.append("博客系统总访问量为:1人次").append("\n");
        content.append("博客系统总评论量为:1人次").append("\n");
        mailUtils.sendSimpleEmail(mailto,"个人博客系统流量统计情况",content.toString());
    }

}

定时器实现

编写定时器类

java 复制代码
@Component
public class ScheduleTask {

    @Autowired
    private MailUtils mailUtils;

    @Value("${spring.mail.username}")
    private String mailto;

    @Autowired
    private StatisticMapper statisticMapper;

    /**
     * cron 一共可以有7个参数 以空格分开 其中年不是必须参数
     * [秒] [分] [小时] [日] [月] [周] [年],
     */
    @Scheduled(cron = "0 0 12 1 * ?")//定时邮件发送任务,每月1日中午12点整发送邮件
    //@Scheduled(cron = "0 */3 * * * ? ")//定时邮件发送任务,每三分钟执行一次
    public void sendEmail(){
        long totalvisit = statisticMapper.getTotalVisit();
        long totalComment = statisticMapper.getTotalComment();
        StringBuffer content = new StringBuffer();
        content.append("博客系统总访问量为:"+totalvisit+"人次").append("\n");
        content.append("博客系统总评论量为:"+totalComment+"人次").append("\n");
        mailUtils.sendSimpleEmail(mailto,"个人博客系统流量统计情况",content.toString());
    }
}

应用入口函数添加配置注解

java 复制代码
@EnableScheduling
@SpringBootApplication
public class BlogSystem01Application {

    public static void main(String[] args) {
        SpringApplication.run(BlogSystem01Application.class, args);
    }
    
}

其中cron表达式格式:

{秒数} {分钟} {小时} {日期} {月份} {星期} {年份(可为空)}

*:表示所有值 比如用在日 表示每一天。

?:表示不指定值 比如周配置 表示不指定星期几执行。

/:表示递增触发 比如 用在分 5/20 从第五分钟开始 每增加20分钟执行一次。

-:表示区间 比如用在 1-6 表示一月到六月执行。

统计博客文章总访问量/总评论量,使用注解自定义SQL,StatisticMapper.java

java 复制代码
@Mapper
public interface StatisticMapper extends BaseMapper<Statistic> {

	// 统计博客文章总访问量
    @Select("SELECT SUM(hits) FROM t_statistic")
    public long getTotalVisit();

    // 统计博客文章总评论量
    @Select("SELECT SUM(comments_num) FROM t_statistic")
    public long getTotalComment();

}

实现效果,每月1日中午12点整定时发送邮件

相关推荐
大阿明10 小时前
Spring Boot(快速上手)
java·spring boot·后端
哆啦A梦158811 小时前
Springboot整合MyBatis实现数据库操作
数据库·spring boot·mybatis
星轨zb13 小时前
通过实际demo掌握SpringSecurity+MP中的基本框架搭建
数据库·spring boot·spring security·mp
没有bug.的程序员15 小时前
Serverless 弹性扩容引发的全线熔断:Spring Boot 启动耗时从 1s 压缩至 0.3s 的物理级绞杀
java·spring boot·kubernetes·serverless·扩容·线上
luom010217 小时前
SpringBoot - Cookie & Session 用户登录及登录状态保持功能实现
java·spring boot·后端
希望永不加班17 小时前
SpringBoot 核心配置文件:application.yml 与 application.properties
java·spring boot·后端·spring
毕设源码-朱学姐18 小时前
【开题答辩全过程】以 基于SpringBoot+Vue的百货商品进出货平台为例,包含答辩的问题和答案
java·spring boot·后端
夜空下的星19 小时前
springboot实现Minio大文件分片下载
java·spring boot·后端
程序员老乔1 天前
Java 新纪元 — JDK 25 + Spring Boot 4 全栈实战(三):虚拟线程2.0,电商秒杀场景下的并发革命
java·开发语言·spring boot
于慨1 天前
spring boot
java·数据库·spring boot