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


当前回答

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

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

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

其他回答

以防你在跑步

思科AnyConnect安全移动代理 思科AnyConnect Web安全代理

尝试停止服务。

在我们的公司网络中,这是解决问题的方法。

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

-Dhttps.protocols=“TLSv1”

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

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

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

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

我在Jdevelopr 11.1.1.7 IDE中构建的Java应用程序也面临同样的问题。我通过取消对代理表单项目属性的使用来解决这个问题。

你可以在下面找到它: 项目属性->(从左边面板)运行/调试/配置文件->在右边面板单击(编辑)->工具设置从左边面板->取消(使用代理)选项。