Spring-Boot 综合项目实战——个人博客系统-简单邮件管理功能

目录

学习目标

一.了解博客系统的邮件功能和文件组织结构

二.熟悉博客系统的邮件发送逻辑和页面编写

三.熟悉邮件管理上的一些限制条件设计

四.掌握邮件管理功能的实现

系统开发及运行环境

全局配置文件编写

邮件管理功能

前提

1.邮件管理首页

2.发送纯文本邮件功能

3.发送图片邮件功能

4.发送附件邮件功能

5.发送模板邮件功能


学习目标

一.了解博客系统的邮件功能和文件组织结构

什么是博客系统?

一个典型的博客系统主要包含以下功能模块:

1、用户注册和登录模块

此模块的功能包括新用户的注册,已注册用户的登录。用户需要登录博客系统才能进行

相关操作,否则只能浏览和评论。如果不是博客系统的用户,需要先行注册。

2、博客主页面显示模块

功能是根据用户的设定将博客内容显示给用户,这些内容包括用户的文章及相关评论,

川户的个性化信息以及其他信息导航栏日。

3、文章管理模块

此模块功能包括新增(保存)文章,修改(保存)功能、删除文章功能等。

4、页面显示定制模块

功能包括显示风格定制和显示栏目定制。页面显示风格包括页面版式布局,背景、主题

风格等。显示栏目定制是指设在页面中的显示栏目,如最近文章列表,Logo 等及它们的位

置。

5、用户信息维护模块

修改维护用户的个性化信息,如昵称、签名等。

二.熟悉博客系统的邮件发送逻辑和页面编写

首先我们先了解邮件的协议

1.SMTP协议:全称为 Simple Mail Transfer Protocol,简单邮件传输协议。它定义了邮件客户端软件和SMTP邮件服务器之间,以及两台SMTP邮件服务器之间的通信规则。
2.POP3协议:全称为 Post Office Protocol,邮局协议。它定义了邮件客户端软件和POP3邮件服务器的通信规则。
3.IMAP协议:全称为 Internet Message Access Protocol,Internet消息访问协议,它是对POP3协议的一种扩展,也是定义了邮件客户端软件和IMAP邮件服务器的通信规则。

三.熟悉邮件管理上的一些限制条件设计

如邮箱格式正确与否;邮件的主题是否填写;字数是否超额;附件格式正确与否等等一系列规则。

四.掌握邮件管理功能的实现

系统开发及运行环境

操作系统:windows

Java开发包:JDK8

项目管理工具:Maven 3.9.2

项目开发工具:IntelliJ IDEA

数据库:MySQL

缓存管理工具:Redis

全局配置文件编写

我们需要把该准备的全局配置文件配置好

application.yml

application-jdbc.properties

application-mail.properties 

application-redis.properties 

邮件管理功能

前提

要想实现邮件的管理与发送,我们还需准备以下几个控制类才能完成

EmailMapper

package com.itheima.dao;

import com.itheima.model.domain.ScheduleEmail;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * 邮件管理
 */
@Mapper
public interface EmailMapper {

    // 创建定时邮件
    @Insert("INSERT INTO t_schedule_email(toaddress, schedule, subject,content,status,error,create_time) VALUES(#{toaddress}, #{schedule}, #{subject},#{content},#{status},#{error},#{createTime})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    public int insertScheduledEmail(ScheduleEmail email);


    @Select("select * from t_schedule_email where status='0' and schedule>=#{schedule}")
    public List<ScheduleEmail> queryScheduledEmail(@Param("schedule") int schedule);

    @Update("update t_schedule_email set status=#{status} where id=#{id}")
    public void updateScheduledEmailStatus(@Param("id") int id,@Param("status") String status);

    @Update("update t_schedule_email set status=#{status},error=#{error} where id=#{id}")
    public void updateScheduledEmailStatusWithError(@Param("id") int id,@Param("status") String status, @Param("error") String error);
}

ScheduleEmail

package com.itheima.model.domain;

import java.util.Date;


public class ScheduleEmail {
    private int id;
    private String toaddress;
    private int schedule;
    private String subject;
    private String content;
    private String status;
    private String error;
    private Date createTime;
    public ScheduleEmail(){}
    public ScheduleEmail(String toaddress, int schedule, String subject, String content, String status, String error, Date createTime) {
        this.toaddress = toaddress;
        this.schedule = schedule;
        this.subject = subject;
        this.content = content;
        this.status = status;
        this.error = error;
        this.createTime = createTime;
    }
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getToaddress() {
        return toaddress;
    }
    public void setToaddress(String toaddress) {
        this.toaddress = toaddress;
    }
    public int getSchedule() {
        return schedule;
    }
    public void setSchedule(int schedule) {
        this.schedule = schedule;
    }
    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;
    }
    public String getStatus() {
        return status;
    }
    public void setStatus(String status) {
        this.status = status;
    }
    public String getError() {
        return error;
    }
    public void setError(String error) {
        this.error = error;
    }
    public Date getCreateTime() {
        return createTime;
    }
    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }
}

EmailServiceImpl

package com.itheima.service.impl;

import com.itheima.dao.EmailMapper;
import com.itheima.dao.StatisticMapper;
import com.itheima.model.domain.ScheduleEmail;


import com.itheima.service.IEmailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.MailException;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.scheduling.support.CronTrigger;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.PostConstruc
  • 6
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
本篇文章是 Spring Boot 实践之十三 9 Spring Boot综合项目实战——个人博客系统管理模块 的续篇,本次将介绍如何实现个人博客系统的拓展模块。 在实际开发中,我们常常需要对系统进行扩展,添加一些新的功能模块。为了不影响原有代码的结构和功能,我们可以将这些新功能模块独立成为一个子模块,然后通过配置文件等方式将其与原有系统进行整合。 本文将以一个个人博客系统为例,介绍如何实现博客的拓展模块,具体包括以下几个方面: 1. 拓展模块的设计和实现 2. 拓展模块的集成和配置 3. 拓展模块的使用示例 ## 1. 拓展模块的设计和实现 在本例中,我们将实现一个博客系统的拓展模块,该模块主要提供以下两个功能: 1. 统计博客文章的阅读量并显示 2. 在博客页面添加底部的版权声明 ### 1.1 统计博客文章的阅读量并显示 首先,我们需要在数据库中添加一个字段来存储博客文章的阅读量。在本例中,我们在 `blog` 表中添加 `read_count` 字段来存储阅读量。 ```sql ALTER TABLE `blog` ADD COLUMN `read_count` INT NOT NULL DEFAULT 0 COMMENT '阅读量' AFTER `update_time`; ``` 接下来,在博客文章页面中添加一个阅读量的显示。我们可以在博客文章详情页面的右侧添加一个阅读量的区域,显示该文章的阅读量。具体的实现方式为: 1. 在博客文章详情页面中添加一个阅读量的区域。 2. 在加载博客文章详情页面时,通过 AJAX 请求统计该文章的阅读量,并更新阅读量区域的显示。 具体的代码实现如下: 在博客文章详情页面中添加一个阅读量的区域: ```html <div class="blog-sidebar-item"> <div class="blog-sidebar-title">阅读量</div> <div class="blog-sidebar-content"> <span id="read-count">0</span> </div> </div> ``` 在加载博客文章详情页面时,通过 AJAX 请求统计该文章的阅读量,并更新阅读量区域的显示。具体的实现方式为: ```javascript $(function () { // 统计阅读量 var blogId = $("#blogId").val(); $.ajax({ url: "/blog/read/" + blogId, type: "POST", success: function (result) { if (result && result.success) { $("#read-count").text(result.data); } else { $("#read-count").text(0); } } }); }); ``` 在服务器端,我们需要实现一个接口来统计博客文章的阅读量,并将其保存到数据库中。具体的实现方式为: ```java @RestController @RequestMapping("/blog") public class BlogController { ... /** * 统计博客文章的阅读量 * * @param blogId 博客文章ID * @return 统计结果 */ @PostMapping("/read/{blogId}") public Result<Integer> readBlog(@PathVariable("blogId") Long blogId) { int readCount = blogService.readBlog(blogId); return Result.success(readCount); } ... } ``` 在 `BlogService` 中实现 `readBlog` 方法: ```java @Service public class BlogServiceImpl implements BlogService { ... /** * 统计博客文章的阅读量 * * @param blogId 博客文章ID * @return 统计结果 */ @Override public int readBlog(Long blogId) { Blog blog = blogMapper.selectByPrimaryKey(blogId); if (blog != null) { int readCount = blog.getReadCount() + 1; blog.setReadCount(readCount); blogMapper.updateByPrimaryKeySelective(blog); return readCount; } return 0; } ... } ``` ### 1.2 在博客页面添加底部的版权声明 接下来,我们将在博客页面底部添加一个版权声明。具体的实现方式为: 1. 在博客页面底部添加一个版权声明的区域。 2. 在加载博客页面时,通过 AJAX 请求获取版权声明的内容,并更新版权声明区域的显示。 具体的代码实现如下: 在博客页面底部添加一个版权声明的区域: ```html <div class="blog-footer"> <div><span id="copyright"> 版权声明:本博客所有文章均为作者原创或转载,未经授权禁止转载。 </span></div> </div> ``` 在加载博客页面时,通过 AJAX 请求获取版权声明的内容,并更新版权声明区域的显示。具体的实现方式为: ```javascript $(function () { // 加载版权声明 $.ajax({ url: "/blog/copyright", type: "GET", success: function (result) { if (result && result.success) { $("#copyright") .html("版权声明:" + result.data); } } }); }); ``` 在服务器端,我们需要实现一个接口来获取版权声明的内容。具体的实现方式为: ```java @RestController @RequestMapping("/blog") public class BlogController { ... /** * 获取版权声明的内容 * * @return 版权声明的内容 */ @GetMapping("/copyright") public Result<String> getCopyright() { String content = "本博客所有文章均为作者原创或转载,未经授权禁止转载。"; return Result.success(content); } ... } ``` ## 2. 拓展模块的集成和配置 在上一篇文章中,我们已经将博客系统的所有模块都整合到了一个工程中,因此我们可以通过添加一个 Maven 模块来实现拓展模块的开发,并将其整合到原有工程中。 具体的步骤如下: 1. 在项目根目录下创建一个新的 Maven 模块,命名为 `blog-ext`,并将其添加到工程中。 2. 在 `blog-ext` 模块中添加 `pom.xml` 文件,并添加依赖关系。 3. 在 `blog-ext` 模块中添加 Spring Boot 的配置文件 `application.yml`,并添加相关配置。 4. 在 `blog-ext` 模块中添加拓展模块的代码和资源文件。 ### 2.1 添加 Maven 模块 在项目根目录下创建一个新的 Maven 模块,命名为 `blog-ext`,并将其添加到工程中。具体的步骤如下: 1. 在项目根目录下创建一个新的 Maven 模块,命名为 `blog-ext`。 ```bash $ cd ~/workspace/springboot-blog $ mvn archetype:generate -DgroupId=com.waylau.spring.boot.blog \ -DartifactId=blog-ext -DarchetypeArtifactId=maven-archetype-quickstart \ -DinteractiveMode=false ``` 2. 将 `blog-ext` 模块添加到工程中。 ```xml <modules> <module>blog-api</module> <module>blog-service</module> <module>blog-web</module> <module>blog-ext</module> </modules> ``` ### 2.2 添加依赖关系 在 `blog-ext` 模块中添加 `pom.xml` 文件,并添加依赖关系。具体的依赖关系如下: ```xml <dependencies> <dependency> <groupId>com.waylau.spring.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>${spring.boot.version}</version> <type>pom</type> <scope>import</scope> </dependency> <!-- 添加 Spring Web MVC 的依赖 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- 添加 MyBatis 的依赖 --> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>${mybatis.boot.version}</version> </dependency> </dependencies> ``` ### 2.3 添加配置文件 在 `blog-ext` 模块中添加 Spring Boot 的配置文件 `application.yml`,并添加相关配置。具体的配置如下: ```yaml spring: datasource: driver-class-name: com.mysql.jdbc.Driver url: jdbc:mysql://localhost:3306/blog?useSSL=false&useUnicode=true&characterEncoding=utf8 username: root password: root mvc: view: prefix: /templates/ suffix: .html resources: static-locations: classpath:/static/ ``` ### 2.4 添加拓展模块的代码和资源文件 在 `blog-ext` 模块中添加拓展模块的代码和资源文件。具体的步骤如下: 1. 在 `blog-ext` 模块中添加 `com.waylau.spring.boot.blog.ext` 包,并在该包下添加 `BlogExtApplication` 类。 ```java @SpringBootApplication(scanBasePackages = "com.waylau.spring.boot.blog.ext") public class BlogExtApplication { public static void main(String[] args) { SpringApplication.run(BlogExtApplication.class, args); } } ``` 2. 在 `blog-ext` 模块中添加 `resources` 目录,并在该目录下添加 `templates` 和 `static` 目录。 3. 在 `templates` 目录中添加 `read-count.html` 和 `copyright.html`。 ```html <!-- read-count.html --> <div class="blog-sidebar-item"> <div class="blog-sidebar-title">阅读量</div> <div class="blog-sidebar-content"> <span id="read-count">0</span> </div> </div> ``` ```html <!-- copyright.html --> <div class="blog-footer"> <div><span id="copyright"> 版权声明:本博客所有文章均为作者原创或转载,未经授权禁止转载。 </span></div> </div> ``` 4. 在 `static` 目录中添加 `js` 目录,并在该目录下添加 `read-count.js` 和 `copyright.js`。 ```javascript // read-count.js $(function () { // 统计阅读量 var blogId = $("#blogId").val(); $.ajax({ url: "/blog/read/" + blogId, type: "POST", success: function (result) { if (result && result.success) { $("#read-count").text(result.data); } else { $("#read-count").text(0); } } }); }); ``` ```javascript // copyright.js $(function () { // 加载版权声明 $.ajax({ url: "/blog/copyright", type: "GET", success: function (result) { if (result && result.success) { $("#copyright") .html("版权声明:" + result.data); } } }); }); ``` ## 3. 拓展模块的使用示例 在完成了拓展模块的开发和配置之后,我们需要将其与原有系统进行整合。具体的步骤如下: 1. 在原有系统中添加对拓展模块的依赖关系。 在 `blog-web` 模块的 `pom.xml` 文件中添加对 `blog-ext` 模块的依赖关系: ```xml <dependencies> ... <!-- 添加 blog-ext 的依赖 --> <dependency> <groupId>com.waylau.spring.boot.blog</groupId> <artifactId>blog-ext</artifactId> <version>${project.version}</version> </dependency> </dependencies> ``` 2. 在原有系统中添加拓展模块的使用示例。 在博客文章详情页面中添加一个阅读量的区域: ```html <!-- 添加阅读量的区域 --> <div th:replace="blog-ext :: read-count"></div> ``` 在博客页面底部添加一个版权声明的区域: ```html <!-- 添加版权声明的区域 --> <div th:replace="blog-ext :: copyright"></div> ``` 经过以上的步骤,我们就已经成功地将博客系统的拓展模块整合到了原有系统中。 ## 总结 本文介绍了如何实现 Spring Boot 的拓展模块,并将其与原有系统进行整合。在实际开发中,我们可以根据具体的需求来实现不同的拓展模块,并通过配置文件等方式将其整合到原有系统中。这种方式既提高了代码的可维护性,又方便了模块的扩展和升级。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值