基于 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点整定时发送邮件

相关推荐
Java开发的小李6 小时前
SpringBoot + Redis 实现分布式 Session 共享(解决多实例登录状态丢失问题)
spring boot·redis·分布式
阿丰资源10 小时前
SpringBoot+Vue实战:打造企业级在线文档管理系统
vue.js·spring boot·后端
0xDevNull10 小时前
Spring Boot 自动装配:从原理到实践
java·spring boot·后端
a8a30212 小时前
Laravel9.x新特性全解析
运维·spring boot·nginx
aLTttY15 小时前
Spring Boot + Redis 实现接口防抖与限流实战指南
spring boot·redis·junit
V+zmm1013416 小时前
毕业设计:基于neo4j的知识图谱的智能问答系统(源码)
spring boot·毕业设计·知识图谱·课程设计·neo4j·智能问答·毕设
直奔標竿16 小时前
Java开发者AI转型第二十三课!Spring AI个人知识库实战(二):异步ETL流水线搭建与避坑指南
java·人工智能·spring boot·后端·spring
浮尘笔记17 小时前
在Snowy后台无需编码实现自动化生成CRUD操作流程
java·开发语言·经验分享·spring boot·后端·程序人生·mybatis
JAVA面经实录91717 小时前
Spring Boot + Spring AI 一体化实战全文档
java·人工智能·spring boot·spring
希望永不加班17 小时前
SpringBoot 接口签名验证(AppKey/Secret)
java·spring boot·后端·spring