From 81eae9159ea0adc570f20792c61ffd75c5ca0966 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=90=8C=E7=8B=BC=E8=93=9D=E5=A4=A9?= Date: Wed, 9 Oct 2024 16:17:48 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=20=E9=82=AE=E4=BB=B6?= =?UTF-8?q?=E5=8F=91=E9=80=81=E9=AA=8C=E8=AF=81=E7=A0=81=E6=A8=A1=E6=9D=BF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../harmony/utils/UtilEmailSent.java | 27 ++++++++++++++++--- .../utils/VerificationCodeGenerator.java | 27 +++++++++++++++++++ .../HarmonyLifeServerApplicationTests.java | 7 ++--- 3 files changed, 53 insertions(+), 8 deletions(-) create mode 100644 src/main/java/com/guaiguailang/harmony/utils/VerificationCodeGenerator.java diff --git a/src/main/java/com/guaiguailang/harmony/utils/UtilEmailSent.java b/src/main/java/com/guaiguailang/harmony/utils/UtilEmailSent.java index 8e4f7da..c2abc81 100644 --- a/src/main/java/com/guaiguailang/harmony/utils/UtilEmailSent.java +++ b/src/main/java/com/guaiguailang/harmony/utils/UtilEmailSent.java @@ -16,10 +16,10 @@ import org.springframework.stereotype.Component; public class UtilEmailSent { @Autowired MailConfig mailConfig; - public void sendEmail(String to, String subject, String content){ - sendEmail(to,subject,content, mailConfig.getNickname()); + public int sendEmail(String to, String subject, String content){ + return sendEmail(to,subject,content, mailConfig.getNickname()); } - public void sendEmail(String to, String subject, String content,String nickname) { + public int sendEmail(String to, String subject, String content,String nickname) { JavaMailSender mailSender = mailConfig.getJavaMailSender(); MimeMessage mimeMessage = mailSender.createMimeMessage(); String from = mailConfig.getUsername(); @@ -36,6 +36,27 @@ public class UtilEmailSent { mailSender.send(mimeMessage); }catch (Exception e){ log.error("(配置邮件失败){}",e.getMessage()); + return -1; } + return 1; } + + public int sendCode(String to){ + String code = VerificationCodeGenerator.generateFourDigitCode(); + + String title="【"+mailConfig.getNickname()+"】验证码 "+code; + String context="
\n" + + "

尊敬的用户:

\n" + + "

\n" + + "您正在乖乖狼科技旗下软件产品进行操作,您的验证码为\n" + + ""+code+"\n" + + ",有效期为十分钟,请及时填写验证码。\n" + + "

\n" + + "

如果这不是您本人操作,请忽略此邮件

\n" + + "

乖乖狼科技    

\n" + + "
"; + + return sendEmail(to,title,context); + } + } \ No newline at end of file diff --git a/src/main/java/com/guaiguailang/harmony/utils/VerificationCodeGenerator.java b/src/main/java/com/guaiguailang/harmony/utils/VerificationCodeGenerator.java new file mode 100644 index 0000000..c0496ed --- /dev/null +++ b/src/main/java/com/guaiguailang/harmony/utils/VerificationCodeGenerator.java @@ -0,0 +1,27 @@ +package com.guaiguailang.harmony.utils; + +import java.util.Random; + +public class VerificationCodeGenerator { + + public static void main(String[] args) { + String verificationCode = generateFourDigitCode(); + System.out.println("Generated Verification Code: " + verificationCode); + } + + /** + * 生成一个四位数的验证码 + * @return 四位数的验证码字符串 + */ + public static String generateFourDigitCode() { + Random random = new Random(); + StringBuilder code = new StringBuilder(4); + + for (int i = 0; i < 4; i++) { + int digit = random.nextInt(10); // 生成一个0到9之间的随机整数 + code.append(digit); + } + + return code.toString(); + } +} \ No newline at end of file diff --git a/src/test/java/com/guaiguailang/harmony/HarmonyLifeServerApplicationTests.java b/src/test/java/com/guaiguailang/harmony/HarmonyLifeServerApplicationTests.java index 25fa180..4d390a3 100644 --- a/src/test/java/com/guaiguailang/harmony/HarmonyLifeServerApplicationTests.java +++ b/src/test/java/com/guaiguailang/harmony/HarmonyLifeServerApplicationTests.java @@ -1,7 +1,4 @@ package com.guaiguailang.harmony; - -import com.guaiguailang.harmony.config.MailConfig; -import com.guaiguailang.harmony.service.EmailService; import com.guaiguailang.harmony.utils.UtilEmailSent; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; @@ -11,8 +8,8 @@ class HarmonyLifeServerApplicationTests { @Autowired UtilEmailSent utilEmailSent; @Test - void contextLoads() { - utilEmailSent.sendEmail("2952458479@qq.com","测试邮件标题","测试邮件内容"); + void testEmailSentUtil() { + utilEmailSent.sendCode("2952458479@qq.com"); } }