我有一个java编译包与https服务器在网上说话。运行编译会出现以下异常:

javax.net.ssl.SSLException: Unrecognized SSL message, plaintext connection?
    at com.sun.net.ssl.internal.ssl.InputRecord.handleUnknownRecord(Unknown Source)
at com.sun.net.ssl.internal.ssl.InputRecord.read(Unknown Source)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.readRecord(Unknown Source)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.performInitialHandshake(Unknown Source)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.startHandshake(Unknown Source)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.startHandshake(Unknown Source)
at sun.net.www.protocol.https.HttpsClient.afterConnect(Unknown Source)
at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(Unknown Source)
at sun.net.www.protocol.https.HttpsURLConnectionImpl.connect(Unknown Source)

我认为这是因为与客户端机器建立的连接不安全。是否有任何方法配置本地机器或端口以连接到远程https服务器?


当前回答

我也遇到了同样的问题,通过在系统属性中设置“proxyUser”和“proxyPassword”来解决。

System.setProperty("http.proxyUser", PROXY_USER);
System.setProperty("http.proxyPassword", PROXY_PASSWORD);

连同“proxyHost”和“proxyPort”

System.setProperty("http.proxyHost", PROXY_ADDRESS);
System.setProperty("http.proxyPort", PROXY_PORT);

希望它能起作用。

其他回答

我认为这是由于联系 与客户端机器建立 不安全。

这是因为您正在与HTTP服务器通信,而不是HTTPS服务器。可能您没有使用正确的HTTPS端口号。

可能您的默认证书已经过期。通过管理控制台执行“安全>SSL证书和密钥管理>密钥存储和证书> NodeDefaultKeyStore >个人证书”选择“默认”别名,然后重新启动WAS后单击“更新”。

如果你在Java 6或更早的版本上从命令行运行Java进程,添加这个开关为我解决了上面的问题:

-Dhttps.protocols=“TLSv1”

它现在对我有效,我已经更改了我的谷歌账户的设置如下:

        System.out.println("Start");
        final String username = "myemail@gmail.com";
        final String password = "************";

        Properties props = new Properties();
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.host", "smtp.gmail.com");
        props.put("mail.smtp.port", "465");
        props.put("mail.transport.protocol", "smtp");
        props.put("mail.smtp.starttls.enable", "true");
        props.put("mail.smtp.starttls.enable", "true");
        props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");

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


        try {
            Transport transport=session.getTransport();
            Message message = new MimeMessage(session);
            message.setFrom(new InternetAddress("myemail@gmail.com"));//formBean.getString("fromEmail")
            message.setRecipients(Message.RecipientType.TO,InternetAddress.parse("myemail@gmail.com"));
            message.setSubject("subject");//formBean.getString(
            message.setText("mailBody");
            transport.connect();
            transport.send(message, InternetAddress.parse("myemail@gmail.com"));//(message);

            System.out.println("Done");

        } catch (MessagingException e) {
            System.out.println("e="+e);
            e.printStackTrace();
            throw new RuntimeException(e);

        }

虽然我在同一篇文章的这个链接中运行程序时启用了SSL和TSL。我花了很多时间,但我意识到并发现了这个联系。 并在谷歌中完成以下2步和设置控制。:

禁用两步验证(密码和OTP) 允许访问不安全的应用程序(允许不安全的应用程序: 上)。

现在我可以用上面的程序发送邮件了。

我在使用Gmail时遇到了这个异常。

为了使用Gmail,我不得不打开“允许不太安全的应用程序”。

这个Gmail设置可以在https://www.google.com/settings/security/lesssecureapps登录Gmail帐户后找到。