如何修复Java中的“无法将套接字转换为TLS”错误

时间:2019-08-05 19:59:21

标签: java smtp javamail

我正在使用JavaMail API发送包含文本文件的电子邮件。我上周编写的代码运行得非常好,但是今天我登录并运行该程序,并收到我在底部附加的图像中的错误。我更新到了Java 8的最新版本,但仍然收到此错误。 (我必须在项目中使用Java 8才能工作)我查看了此更新的Java 8补丁说明,并看到删除了根CA证书,但不确定该怎么做。我发布此问题是因为在其他有关此问题的其他问题中找不到任何解决方案。

我正在运行Java 8(jre 1.8.0_221),以前正在运行jre 1.8.0_211。我不知道发生了什么变化,因为它今天在v.211中停止工作,并且在我更新到v.221的最新版本的Java 8之后也无法工作。我尝试了其他各种属性,但均未成功。我不确定这是否是Java删除的特定内容(我对此表示怀疑),或者它是否与防火墙权限有关,我可能拥有或可能没有的任何smtp权限。无论如何,该代码位于电子邮件发送部分的下方,并且代码在Transport.send(message);处失败,并显示错误“无法将套接字转换为TLS”

                Properties props = new Properties();
                props.put("mail.smtp.auth", "true");
                props.put("mail.smtp.starttls.enable", "true");
                props.put("mail.smtp.host", host);
                props.put("mail.smtp.port", "25");
                props.put("mail.smtp.debug", "true");

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

                try {
                    Message message = new MimeMessage(session);
                    message.setFrom(new InternetAddress(from));
                    message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to));
                    message.setSubject(prop.getProperty("Message"));

                    BodyPart messageBodyPart = new MimeBodyPart();
                    messageBodyPart.setText(prop.getProperty("Message"));

                    Multipart multipart = new MimeMultipart();
                    multipart.addBodyPart(messageBodyPart);

                    messageBodyPart = new MimeBodyPart();
                    DataSource source = new FileDataSource(prop.getProperty("text") + ".txt");
                    messageBodyPart.setDataHandler(new DataHandler(source));
                    messageBodyPart.setFileName(prop.getProperty("filename"));
                    multipart.addBodyPart(messageBodyPart);

                    message.setContent(multipart);

                    Transport.send(message);
                    System.out.println("Sent successfully.\n");

Error Message我收到了。希望我已经充分解释了。这只是我在这里问的第二个问题。谢谢您的帮助。

1 个答案:

答案 0 :(得分:0)

我不是专家,但是...

在2019年4月或在您的环境中应用根证书时,有意不信任某些根证书可能会影响您。(p:https://blogs.oracle.com/java-platform-group/jdk-distrusting-symantec-tls-certificates)。

相关问题