zqy
2025-10-14 4502f650f4816e55b5c7bf751ffce3aa0dce72dd
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
package com.ruoyi.system.service.impl;
 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
 
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
 
@Service
public class EmailService {
    @Autowired
    private JavaMailSender mailSender;
 
    @Value("${spring.mail.username}")
    private String fromEmail;
 
    public void sendVerificationCode(String toEmail, String verificationCode) {
        try {
            MimeMessage message = mailSender.createMimeMessage();
            MimeMessageHelper helper = new MimeMessageHelper(message, true, "UTF-8");
 
            helper.setFrom(new InternetAddress(fromEmail, "本嘟嘟家网"));
            helper.setTo(toEmail);
            helper.setSubject("本嘟嘟家网 - 登录验证码");
 
            // 构建HTML内容
            String htmlContent = buildVerificationEmail(verificationCode);
            helper.setText(htmlContent, true); // true表示内容为HTML
 
            mailSender.send(message);
        } catch (Exception e) {
            throw new RuntimeException("邮件发送失败", e);
        }
    }
 
    private String buildVerificationEmail(String verificationCode) {
        return "<!DOCTYPE html>\n" +
                "<html>\n" +
                "<head>\n" +
                "    <meta charset=\"UTF-8\">\n" +
                "    <meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">\n" +
                "    <title>本嘟嘟家网 - 安全验证码</title>\n" +
                "    <style>\n" +
                "        body {\n" +
                "            font-family: 'PingFang SC', 'Microsoft YaHei', sans-serif;\n" +
                "            line-height: 1.6;\n" +
                "            color: #333333;\n" +
                "            background-color: #f5f7fa;\n" +
                "            margin: 0;\n" +
                "            padding: 0;\n" +
                "        }\n" +
                "        .email-container {\n" +
                "            max-width: 600px;\n" +
                "            margin: 20px auto;\n" +
                "            background-color: #ffffff;\n" +
                "            border-radius: 8px;\n" +
                "            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.08);\n" +
                "            overflow: hidden;\n" +
                "        }\n" +
                "        .header {\n" +
                "            background: linear-gradient(135deg, #1e88e5, #0d47a1);\n" +
                "            padding: 25px 30px;\n" +
                "            text-align: center;\n" +
                "            color: white;\n" +
                "        }\n" +
                "        .header h1 {\n" +
                "            margin: 0;\n" +
                "            font-size: 22px;\n" +
                "            font-weight: 600;\n" +
                "            letter-spacing: 0.5px;\n" +
                "        }\n" +
                "        .content {\n" +
                "            padding: 30px;\n" +
                "        }\n" +
                "        .security-notice {\n" +
                "            background-color: #fff8e1;\n" +
                "            border-left: 4px solid #ffc107;\n" +
                "            padding: 15px;\n" +
                "            margin-bottom: 25px;\n" +
                "            border-radius: 4px;\n" +
                "            font-size: 14px;\n" +
                "            color: #5d4037;\n" +
                "        }\n" +
                "        .verification-section {\n" +
                "            text-align: center;\n" +
                "            margin: 30px 0;\n" +
                "            padding: 20px;\n" +
                "            background-color: #f0f7ff;\n" +
                "            border-radius: 8px;\n" +
                "        }\n" +
                "        .code-label {\n" +
                "            font-size: 16px;\n" +
                "            color: #1565c0;\n" +
                "            margin-bottom: 15px;\n" +
                "        }\n" +
                "        .code-display {\n" +
                "            display: inline-block;\n" +
                "            background: linear-gradient(to right, #2196f3, #21cbf3);\n" +
                "            color: white;\n" +
                "            font-size: 36px;\n" +
                "            font-weight: 700;\n" +
                "            padding: 15px 40px;\n" +
                "            border-radius: 8px;\n" +
                "            letter-spacing: 8px;\n" +
                "            box-shadow: 0 4px 15px rgba(33, 150, 243, 0.3);\n" +
                "            margin: 15px 0;\n" +
                "            text-decoration: none;\n" +
                "        }\n" +
                "        .instructions {\n" +
                "            font-size: 15px;\n" +
                "            color: #555555;\n" +
                "            margin-bottom: 25px;\n" +
                "            line-height: 1.7;\n" +
                "        }\n" +
                "        .validity {\n" +
                "            background-color: #e3f2fd;\n" +
                "            padding: 15px;\n" +
                "            border-radius: 6px;\n" +
                "            text-align: center;\n" +
                "            font-size: 14px;\n" +
                "            color: #0d47a1;\n" +
                "            margin-top: 25px;\n" +
                "        }\n" +
                "        .footer {\n" +
                "            text-align: center;\n" +
                "            padding: 20px;\n" +
                "            background-color: #f5f7fa;\n" +
                "            color: #78909c;\n" +
                "            font-size: 13px;\n" +
                "            border-top: 1px solid #e0e0e0;\n" +
                "        }\n" +
                "        .footer p {\n" +
                "            margin: 5px 0;\n" +
                "        }\n" +
                "        .highlight {\n" +
                "            color: #d32f2f;\n" +
                "            font-weight: 600;\n" +
                "        }\n" +
                "    </style>\n" +
                "</head>\n" +
                "<body>\n" +
                "    <div class=\"email-container\">\n" +
                "        <div class=\"header\">\n" +
                "            <h1>本嘟嘟家网 - 安全验证</h1>\n" +
                "        </div>\n" +
                "        \n" +
                "        <div class=\"content\">\n" +
                "            <div class=\"security-notice\">\n" +
                "                <strong>安全提示:</strong> 您正在登录本嘟嘟家网,验证码请勿告知他人,泄露会导致账号被盗,使家族及个人信息泄露。\n" +
                "            </div>\n" +
                "            \n" +
                "            <p class=\"instructions\">请使用以下验证码完成登录验证:</p>\n" +
                "            \n" +
                "            <div class=\"verification-section\">\n" +
                "                <div class=\"code-label\">您的验证码</div>\n" +
                "                <div class=\"code-display\">" + verificationCode + "</div>\n" +
                "                <p class=\"instructions\">请将上述验证码输入到登录页面的验证码输入框中</p>\n" +
                "            </div>\n" +
                "            \n" +
                "            <div class=\"validity\">\n" +
                "                <strong>请注意:</strong> 此验证码将在 <strong>5分钟</strong> 后失效,请尽快使用。\n" +
                "            </div>\n" +
                "        </div>\n" +
                "        \n" +
                "        <div class=\"footer\">\n" +
                "            <p>此为系统自动发送邮件,请勿直接回复</p>\n" +
                "            <p>如果您未进行此操作,请忽略此邮件或联系客服</p>\n" +
                "            <p>© 2023 本嘟嘟家网 版权所有</p>\n" +
                "        </div>\n" +
                "    </div>\n" +
                "</body>\n" +
                "</html>";
    }
}