4.13日总结

javafx中实现发送qq邮箱验证码:

手动导入jar包方法:

第一步:开启QQ邮箱的 POP3/IMAP 或者 SMTP/IMAP 服务

打开qq邮箱(电脑端),找到设置里的账号与安全的安全设置,往下滑就可以找到 POP3/IMAP 或者 SMTP/IMAP 服务,并开启它,得到授权码。

第二步:网上下载java.mail.jar包,将它导到你项目里自己建的lib目录下,右击,添加为库。

第三步:在 pom.xml 中添加以下依赖配置,直接引用本地 JAR 文件

java 复制代码
<dependency>
    <groupId>javax.mail</groupId>
    <artifactId>mail</artifactId>
    <version>1.4.7</version>  <!-- 根据实际版本修改 -->
    <scope>system</scope>
    <systemPath>${project.basedir}/lib/javax.mail.jar</systemPath>
</dependency>

并确保依赖已添加到模块的类路径中

如果项目使用 module-info.java,需添加对 java.mail 模块的依赖:

java 复制代码
module your.module.name {
    requires java.mail;  // 添加此行
    // 其他模块声明...
}

最后:

代码如下:

java 复制代码
   // QQ邮箱SMTP配置
    private static final String SMTP_HOST = "smtp.qq.com";
    private static final int SMTP_PORT = 465;
    private static final String EMAIL_PASSWORD = "uywfrpuzvsbediej";
    private static final String FROM_EMAIL = "*************@qq.com";
  private void sendEmailAsync(String toEmail) {
        Task<Boolean> sendTask = new Task<>() {
            @Override
            protected Boolean call() throws Exception {
                return sendEmail(toEmail, verificationCode);
            }
        };

        sendTask.setOnSucceeded(e -> {
            if (sendTask.getValue()) {
                startCountdown();
            } else {
                Platform.runLater(() -> {
                    new Alert(Alert.AlertType.ERROR, "验证码发送失败,请检查邮箱地址!").show();
                    sendCodeBtn.setDisable(false);
                });
            }
        });

        sendTask.setOnFailed(e -> {
            Platform.runLater(() -> {
                new Alert(Alert.AlertType.ERROR, "邮件发送失败:" + sendTask.getException().getMessage()).show();
                sendCodeBtn.setDisable(false);
            });
        });

        new Thread(sendTask).start();
    }

    private boolean sendEmail(String toEmail, String code) {
        Properties props = new Properties();
        props.put("mail.smtp.host", SMTP_HOST);
        props.put("mail.smtp.port", SMTP_PORT);
        props.put("mail.smtp.ssl.enable", "true");
        props.put("mail.smtp.auth", "true");

        Session session = Session.getInstance(props, new Authenticator() {
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(FROM_EMAIL, EMAIL_PASSWORD);
            }
        });

        try {
            MimeMessage message = new MimeMessage(session);
            message.setFrom(new InternetAddress(FROM_EMAIL));
            message.setRecipient(Message.RecipientType.TO, new InternetAddress(toEmail));
            message.setSubject("【StudyXing】注册验证码");
            message.setText("您的验证码是:" + code + ",有效期1分钟。");
            Transport.send(message);
            return true;
        } catch (MessagingException e) {
            e.printStackTrace();
            return false;
        }
    }
相关推荐
我的xiaodoujiao20 分钟前
使用 Python 语言 从 0 到 1 搭建完整 Web UI自动化测试学习系列 38--Allure 测试报告
python·学习·测试工具·pytest
Boilermaker19926 小时前
[Java 并发编程] Synchronized 锁升级
java·开发语言
沈浩(种子思维作者)7 小时前
真的能精准医疗吗?癌症能提前发现吗?
人工智能·python·网络安全·健康医疗·量子计算
MM_MS7 小时前
Halcon变量控制类型、数据类型转换、字符串格式化、元组操作
开发语言·人工智能·深度学习·算法·目标检测·计算机视觉·视觉检测
꧁Q༒ོγ꧂7 小时前
LaTeX 语法入门指南
开发语言·latex
njsgcs7 小时前
ue python二次开发启动教程+ 导入fbx到指定文件夹
开发语言·python·unreal engine·ue
alonewolf_997 小时前
JDK17新特性全面解析:从语法革新到模块化革命
java·开发语言·jvm·jdk
io_T_T7 小时前
迭代器 iteration、iter 与 多线程 concurrent 交叉实践(详细)
python
古城小栈8 小时前
Rust 迭代器产出的引用层数——分水岭
开发语言·rust
华研前沿标杆游学8 小时前
2026年走进洛阳格力工厂参观游学
python