我有一个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服务器?


当前回答

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

-Dhttps.protocols=“TLSv1”

其他回答

添加这个作为答案,因为它可能会帮助到其他人。

我不得不强制jvm使用IPv4堆栈来解决错误。我的应用程序过去在公司网络内工作,但从家里连接时,它给出了同样的异常。不涉及代理。增加了jvm参数 -Djava.net.preferIPv4Stack=true,所有https请求行为正常。

我解决了我的问题使用端口25和后续道具

mailSender.javaMailProperties.putAll([
                "mail.smtp.auth": "true",
                "mail.smtp.starttls.enable": "false",
                "mail.smtp.ssl.enable": "false",
                "mail.smtp.socketFactory.fallback": "true",
        ]);

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

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

        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) 允许访问不安全的应用程序(允许不安全的应用程序: 上)。

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

javax.net.ssl.SSLException: Unrecognized SSL message, plaintext connection?

您应该有一个本地SMTP域名,该域名将与邮件服务器联系并建立一个新的连接,您还应该在下面的编程中更改SSL属性

javax.net.ssl.SSLException: Unrecognized SSL message, plaintext connection

 props.put("mail.smtp.socketFactory.fallback", "true"); // Should be true