当前位置: 首页 > news >正文

4.13日总结

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

手动导入jar包方法:

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

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

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

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

<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 模块的依赖:

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

 最后:

代码如下:

   // 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;
        }
    }

 

相关文章:

  • 【RL系列】DAPO: An Open-Source LLM Reinforcement Learning System at Scale
  • 【HTTP】:应用层协议HTTP(1)
  • 银河麒麟服务器操作系统V10安装Nvidia显卡驱动和CUDA(L40)并安装ollama运行DeepSeek【开荒存档版】
  • 【中间件】nginx反向代理实操
  • 洛谷刷题小结
  • 编译uboot的Makefile编写
  • system V 共享内存
  • React 记账本项目实战:多页面路由、Context 全局
  • Dolphinscheduler3.2.1运行Java Jar路径重复的BUG修复问题
  • MySQL 用 limit 影响性能的优化方案
  • 深入学习OpenCV:第一章简介
  • (二十二)安卓开发中的数据存储之SQLite简单使用
  • 《轨道力学导论》——第一讲:轨道力学概述
  • 案例驱动的 IT 团队管理:创新与突破之路: 第四章 危机应对:从风险预见到创新破局-4.1.2债务评估模型与优先级排序
  • 阻塞与非阻塞等待非阻塞轮询
  • 代码,Java Maven项目打包遇到的环境问题
  • 针对OPPO A5(PBAM00)在锁屏界面屏幕无法滑动的问题.
  • STM32 HAL库 HC-05蓝牙通信实现
  • 《嵌入式系统原理》一些题目
  • Mysql5.7配置文件
  • 回望乡土:对媒介化社会的反思
  • 美国政府信用卡被设1美元限额,10美元采购花一两小时填表
  • 涨知识|没想到吧,体育老师强调的运动恢复方法是错的?
  • 市自规局公告收回新校区建设用地,宿迁学院:需变更建设主体
  • 新买宝马竟是“维修车”,男子发视频维权被4S店索赔100万
  • 暴利之下:宠物殡葬行业的冰与火之歌