如何通过两个步骤使java邮件类在邮件中对我进行身份验证

如何通过两个步骤使java邮件类在邮件中对我进行身份验证,java,email,two-factor-authentication,Java,Email,Two Factor Authentication,当我进入Gmail时,它会将我重定向到一个外部页面,在那里我必须输入密码和电子邮件,紧接着是一个Gmail登录窗口,在那里我必须输入凭据。添加代码中描述的属性我无法让它工作 错误是: javax.mail.MessagingException: Could not convert socket to TLS; nested exception is: javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorExce

当我进入Gmail时,它会将我重定向到一个外部页面,在那里我必须输入密码和电子邮件,紧接着是一个Gmail登录窗口,在那里我必须输入凭据。添加代码中描述的属性我无法让它工作

错误是:

javax.mail.MessagingException: Could not convert socket to TLS; nested exception is:
javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: 
PKIX path validation failed: 
Caused by: javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: 
PKIX path validation failed: java.security.cert.CertPathValidatorException: 
Usage constraint TLSServer check failed: 
守则:

    public static void main(String[] args) {

        final String username = "username@gmail.com";
        final String password = "password";

        Properties prop = new Properties();
        prop.put("mail.smtp.host", "smtp.gmail.com");
        prop.put("mail.smtp.port", "587");
        prop.put("mail.smtp.auth", "true");
        prop.put("mail.smtp.starttls.enable", "true"); //TLS

        Session session = Session.getInstance(prop,
                new javax.mail.Authenticator() {
                    protected PasswordAuthentication getPasswordAuthentication() {
                        return new PasswordAuthentication(username, password);
                    }
                });

        try {

            Message message = new MimeMessage(session);
            message.setFrom(new InternetAddress("from@gmail.com"));
            message.setRecipients(
                    Message.RecipientType.TO,
                    InternetAddress.parse("to_username_a@gmail.com, to_username_b@yahoo.com")
            );
            message.setSubject("Testing Gmail TLS");
            message.setText("Dear Mail Crawler,"
                    + "\n\n Please do not spam my email!");

            Transport.send(message);

            System.out.println("Done");

        } catch (MessagingException e) {
            e.printStackTrace();
        }
    }

}

```

PKIX路径验证失败:javax.net.ssl.SSLHandshakeException:sun.security.validator.validator异常:

您可能缺少一些需要测试的TLS连接属性

指以下任一项:

  • Gmail服务器要求您在进行TLS连接时拥有证书
  • 你正在使用一个证书,但它不是gmail想要的
  • 你正在使用证书,gmail喜欢它,但它已经过期了
如果您转到firefox,然后转到,您将看到它是https://connection,如果您在firefox中单击URL旁边的(密钥锁),您可以查找有关证书详细信息的更多信息

有一整页是关于如何使用Java使用gmail API的