『博客开发日记』之发送评论会给个人发送邮箱通知功能

本文最后更新于 2026年2月8日 下午

发送评论会给个人发送邮箱通知功能


邮箱收取评论功能

当有人发评论或者回复评论时会通过邮箱通知别人

要根据评论者是在哪个地方发表的评论(文章内,友链区,留言板)来发送不同标题的邮箱

根据新评论还是回复别人的评论来发送邮箱,如果是回复的评论要显示回复的哪条评论

如果是博主自己发表的评论或者是自己回复自己的评论就不要发送邮箱给本人了

着其实和发邮箱验证码功能相似都是通过Spring Boot Mail 邮件服务来实现

现在EmailServiceImpl中添加sendCommentNotificationByEmail方法实现邮箱通知功能

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237

//处理评论邮件通知(统一入口)
public void sendCommentNotificationByEmail(Comment comment)
{
try {
String articleTitle = getArticleTitle(comment);
String articleUrl = buildArticleUrl(comment);

// 判断是否为回复评论
if (comment.getRootId() != null && comment.getRootId() != -1L)
{
// 这是一条回复,需要通知被回复的人
Comment parentComment = commentMapper.selectById(comment.getReplyToCommentId());
if (parentComment != null && StringUtils.hasText(parentComment.getEmail()))
{
// 不要给自己发邮件(如果回复者和被回复者是同一个人)
if (!comment.getEmail().equalsIgnoreCase(parentComment.getEmail()))
{
sendCommentReplyNotificationByEmail(
parentComment.getEmail(),
articleTitle,
comment.getNickname(),
comment.getContent(),
parentComment.getContent(),
articleUrl
);
}
}
}

// 如果不是博主自己评论,通知博主有新评论
if (!comment.getEmail().equalsIgnoreCase(SystemConstants.BLOGGER_EMAIL))
{
sendNewCommentNotification(
articleTitle,
comment.getNickname(),
comment.getContent(),
articleUrl
);
}
} catch (Exception e) {
// 邮件发送失败不影响评论功能
e.printStackTrace();
}
}

//获取文章标题
private String getArticleTitle(Comment comment)
{
String type = comment.getType();
if (SystemConstants.COMMENT_TYPE_ARTICLE.equals(type) && comment.getArticleId() != null) {
Article article = articleMapper.selectById(comment.getArticleId());
return article != null ? article.getTitle() : "未知文章";
} else if (SystemConstants.COMMENT_TYPE_LINK.equals(type)) {
return "友链评论";
} else if (SystemConstants.COMMENT_TYPE_MESSAGE.equals(type)) {
return "留言板";
}
return "评论";
}

//构建文章URL
private String buildArticleUrl(Comment comment)
{
String type = comment.getType();
if (SystemConstants.COMMENT_TYPE_ARTICLE.equals(type) && comment.getArticleId() != null) {
return frontendUrl + "/post/" + comment.getArticleId();
} else if (SystemConstants.COMMENT_TYPE_LINK.equals(type)) {
return frontendUrl + "/links";
} else if (SystemConstants.COMMENT_TYPE_MESSAGE.equals(type)) {
return frontendUrl + "/messages";
}
return frontendUrl;
}

//发送新评论通知邮件(有人评论了你的文章)
public void sendNewCommentNotification(String articleTitle, String commenterNickname, String commentContent, String articleUrl)
{
try {
MimeMessage message = mailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(message, true, "UTF-8");

helper.setFrom(from);
helper.setTo(from); // 发送给博主自己
helper.setSubject("【云梦泽的个人博客】您收到了新评论");

String htmlContent = buildCommentNotificationHtml(articleTitle, commenterNickname, commentContent, articleUrl, "新评论");
helper.setText(htmlContent, true);

mailSender.send(message);
} catch (MessagingException e) {
// 邮件发送失败不影响评论功能,只记录日志
e.printStackTrace();
}
}

//发送评论回复通知邮件(有人回复了你的评论)
public void sendCommentReplyNotificationByEmail(String toEmail, String articleTitle, String replierNickname, String replyContent, String originalComment, String articleUrl)
{
try {
MimeMessage message = mailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(message, true, "UTF-8");

helper.setFrom(from);
helper.setTo(toEmail);
helper.setSubject("【云梦泽的个人博客】您的评论收到了新回复");

String htmlContent = buildReplyNotificationHtml(articleTitle, replierNickname, replyContent, originalComment, articleUrl);
helper.setText(htmlContent, true);

mailSender.send(message);
} catch (MessagingException e) {
// 邮件发送失败不影响评论功能,只记录日志
e.printStackTrace();
}
}

//构建评论通知邮件HTML内容
private String buildCommentNotificationHtml(String articleTitle, String commenterNickname, String commentContent, String articleUrl, String notificationType)
{
return "<!DOCTYPE html>" +
"<html>" +
"<head>" +
" <meta charset=\"UTF-8\">" +
" <meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">" +
"</head>" +
"<body style=\"margin: 0; padding: 0; background: linear-gradient(135deg, #a5dff9 0%, #008c9e 100%); font-family: 'Segoe UI', Arial, sans-serif;\">" +
" <table width=\"100%\" cellpadding=\"0\" cellspacing=\"0\" style=\"padding: 40px 20px;\">" +
" <tr>" +
" <td align=\"center\">" +
" <table width=\"600\" cellpadding=\"0\" cellspacing=\"0\" style=\"background-color: #f5fffa; border-radius: 12px; box-shadow: 0 12px 15px 0 rgba(0,0,0,0.24), 0 17px 50px 0 rgba(0,0,0,0.19); overflow: hidden;\">" +
" <!-- 头部 -->" +
" <tr>" +
" <td style=\"background: rgb(125, 182, 191); padding: 40px 30px; text-align: center;\">" +
" <h1 style=\"color: #2c3e50; margin: 0; font-size: 28px; font-weight: 600; letter-spacing: 1px;\">云梦泽的个人博客</h1>" +
" <p style=\"color: #546e7a; margin: 10px 0 0 0; font-size: 14px;\">评论通知</p>" +
" </td>" +
" </tr>" +
" <!-- 内容 -->" +
" <tr>" +
" <td style=\"padding: 50px 40px;\">" +
" <h2 style=\"color: #3c4858; margin: 0 0 20px 0; font-size: 22px; font-weight: 600;\">您收到了" + notificationType + "</h2>" +
" <p style=\"color: #718096; line-height: 1.8; margin: 0 0 30px 0; font-size: 15px;\">" +
" <strong>" + commenterNickname + "</strong> 在文章《<strong>" + articleTitle + "</strong>》中发表了评论。" +
" </p>" +
" <!-- 评论内容区域 -->" +
" <div style=\"background: linear-gradient(135deg, #f8f9fa 0%, #e8f4f8 100%); border-left: 4px solid #30a9de; border-radius: 8px; padding: 25px; margin: 30px 0; box-shadow: 0 2px 8px rgba(48,169,222,0.1);\">" +
" <p style=\"color: #718096; margin: 0 0 10px 0; font-size: 13px; text-transform: uppercase; letter-spacing: 1px;\">评论内容</p>" +
" <p style=\"color: #3c4858; font-size: 15px; line-height: 1.8; margin: 0; word-wrap: break-word;\">" + commentContent + "</p>" +
" </div>" +
" <!-- 查看按钮 -->" +
" <div style=\"text-align: center; margin: 30px 0;\">" +
" <a href=\"" + articleUrl + "\" style=\"display: inline-block; background: linear-gradient(135deg, #30a9de 0%, #2196f3 100%); color: #ffffff; text-decoration: none; padding: 14px 40px; border-radius: 25px; font-size: 15px; font-weight: 600; box-shadow: 0 4px 15px rgba(48,169,222,0.3); transition: all 0.3s;\">查看评论</a>" +
" </div>" +
" </td>" +
" </tr>" +
" <!-- 底部 -->" +
" <tr>" +
" <td style=\"background: linear-gradient(135deg, #f8f9fa 0%, #eaecef 100%); padding: 30px; text-align: center; border-top: 1px solid #eaecef;\">" +
" <p style=\"color: #718096; margin: 0 0 10px 0; font-size: 13px;\">" +
" 此邮件由系统自动发送,请勿直接回复" +
" </p>" +
" <p style=\"color: #a7a9ad; margin: 0; font-size: 12px;\">" +
" © 2026 云梦泽的个人博客 · All rights reserved" +
" </p>" +
" </td>" +
" </tr>" +
" </table>" +
" </td>" +
" </tr>" +
" </table>" +
"</body>" +
"</html>";
}

//构建回复通知邮件HTML内容
private String buildReplyNotificationHtml(String articleTitle, String replierNickname, String replyContent, String originalComment, String articleUrl)
{
return "<!DOCTYPE html>" +
"<html>" +
"<head>" +
" <meta charset=\"UTF-8\">" +
" <meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">" +
"</head>" +
"<body style=\"margin: 0; padding: 0; background: linear-gradient(135deg, #a5dff9 0%, #008c9e 100%); font-family: 'Segoe UI', Arial, sans-serif;\">" +
" <table width=\"100%\" cellpadding=\"0\" cellspacing=\"0\" style=\"padding: 40px 20px;\">" +
" <tr>" +
" <td align=\"center\">" +
" <table width=\"600\" cellpadding=\"0\" cellspacing=\"0\" style=\"background-color: #f5fffa; border-radius: 12px; box-shadow: 0 12px 15px 0 rgba(0,0,0,0.24), 0 17px 50px 0 rgba(0,0,0,0.19); overflow: hidden;\">" +
" <!-- 头部 -->" +
" <tr>" +
" <td style=\"background: rgb(125, 182, 191); padding: 40px 30px; text-align: center;\">" +
" <h1 style=\"color: #2c3e50; margin: 0; font-size: 28px; font-weight: 600; letter-spacing: 1px;\">云梦泽的个人博客</h1>" +
" <p style=\"color: #546e7a; margin: 10px 0 0 0; font-size: 14px;\">回复通知</p>" +
" </td>" +
" </tr>" +
" <!-- 内容 -->" +
" <tr>" +
" <td style=\"padding: 50px 40px;\">" +
" <h2 style=\"color: #3c4858; margin: 0 0 20px 0; font-size: 22px; font-weight: 600;\">您的评论收到了新回复</h2>" +
" <p style=\"color: #718096; line-height: 1.8; margin: 0 0 30px 0; font-size: 15px;\">" +
" <strong>" + replierNickname + "</strong> 在文章《<strong>" + articleTitle + "</strong>》中回复了您的评论。" +
" </p>" +
" <!-- 原评论 -->" +
" <div style=\"background: #f8f9fa; border-left: 4px solid #cbd5e0; border-radius: 8px; padding: 20px; margin: 20px 0;\">" +
" <p style=\"color: #718096; margin: 0 0 10px 0; font-size: 12px;\">您的评论</p>" +
" <p style=\"color: #718096; font-size: 14px; line-height: 1.6; margin: 0; word-wrap: break-word;\">" + originalComment + "</p>" +
" </div>" +
" <!-- 回复内容 -->" +
" <div style=\"background: linear-gradient(135deg, #f8f9fa 0%, #e8f4f8 100%); border-left: 4px solid #52c41a; border-radius: 8px; padding: 25px; margin: 20px 0; box-shadow: 0 2px 8px rgba(82,196,26,0.1);\">" +
" <p style=\"color: #718096; margin: 0 0 10px 0; font-size: 13px; text-transform: uppercase; letter-spacing: 1px;\">回复内容</p>" +
" <p style=\"color: #3c4858; font-size: 15px; line-height: 1.8; margin: 0; word-wrap: break-word;\">" + replyContent + "</p>" +
" </div>" +
" <!-- 查看按钮 -->" +
" <div style=\"text-align: center; margin: 30px 0;\">" +
" <a href=\"" + articleUrl + "\" style=\"display: inline-block; background: linear-gradient(135deg, #52c41a 0%, #73d13d 100%); color: #ffffff; text-decoration: none; padding: 14px 40px; border-radius: 25px; font-size: 15px; font-weight: 600; box-shadow: 0 4px 15px rgba(82,196,26,0.3); transition: all 0.3s;\">查看回复</a>" +
" </div>" +
" </td>" +
" </tr>" +
" <!-- 底部 -->" +
" <tr>" +
" <td style=\"background: linear-gradient(135deg, #f8f9fa 0%, #eaecef 100%); padding: 30px; text-align: center; border-top: 1px solid #eaecef;\">" +
" <p style=\"color: #718096; margin: 0 0 10px 0; font-size: 13px;\">" +
" 此邮件由系统自动发送,请勿直接回复" +
" </p>" +
" <p style=\"color: #a7a9ad; margin: 0; font-size: 12px;\">" +
" © 2026 云梦泽的个人博客 · All rights reserved" +
" </p>" +
" </td>" +
" </tr>" +
" </table>" +
" </td>" +
" </tr>" +
" </table>" +
"</body>" +
"</html>";
}

然后在CommentServiceImpl的addComment方法里调用sendCommentNotificationByEmail方法



PS:该系列只做为作者学习开发项目做的笔记用

不一定符合读者来学习,仅供参考


预告

后续会记录博客的开发过程

每次学习会做一份笔记来进行发表

“一花一世界,一叶一菩提”


版权所有 © 2025 云梦泽
欢迎访问我的个人网站:https://hgt12.github.io/


『博客开发日记』之发送评论会给个人发送邮箱通知功能
http://example.com/2026/01/24/『博客开发日记』之发送评论会给个人发送邮箱通知功能/
作者
云梦泽
发布于
2026年1月24日
更新于
2026年2月8日
许可协议