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;
        }
    }
相关推荐
高级程序源5 小时前
springboot本地生活服务平台33399-计算机课程设计、毕业设计
vue.js·spring boot·python·mysql·小程序·flask·课程设计
高级程序源5 小时前
springboot社区旧物回收管理系统25381-计算机课程设计、毕业设计
vue.js·spring boot·python·django·课程设计
高级程序源5 小时前
springboot大学生体测系统72170-计算机课程设计、毕业设计
java·vue.js·spring boot·后端·python·django·课程设计
谢亮_vipxieliang5 小时前
JavaScript this 指向完全指南:调用位置决定一切
开发语言·javascript·ecmascript
denlaku6 小时前
JDK 13 新特性详解
java·开发语言
打工仔折腾 AI6 小时前
极空间用 Docker 部署 Photopea:网页版 PS 的私有化实践
运维·后端·python·docker·容器·langchain·ai agent 实战
Eiceblue6 小时前
使用 Python 实现 Excel 行列转置(保留单元格样式)
前端·python·excel
念越6 小时前
题库、练习与错题本代码阅读
开发语言·数据结构
WeiXin_DZbishe6 小时前
基于Node.js的马拉松赛事管理系统的设计与实现-计算机毕设 附源码【课程设计】90211
java·javascript·vue.js·python·php·课程设计
l1t6 小时前
几种数据库软件count(distinct)和count()from group by子查询性能测试
开发语言·数据库