javax.mail.MessagingException: JAVA 中无法将套接字转换为 TLS 异常

javax.mail.MessagingException: Could not convert socket to TLS exception in JAVA

我想通过单击 JFrame 中的按钮将邮件从我的电子邮件地址发送到另一个电子邮件地址 netbeans.Here 是代码,

import java.awt.HeadlessException;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.swing.JOptionPane;
import com.sun.mail.util.MailSSLSocketFactory;
import java.io.File;
import javax.mail.PasswordAuthentication;
import javax.mail.*;
import javax.swing.JFileChooser;

    private void btn_sendActionPerformed(java.awt.event.ActionEvent evt) {                                         
    final String From = txt_from.getText();
    final String password = txt_password.getText();
    String To = txt_to.getText();
    String subject = txt_sub.getText();
    String txtmessage = txt_body.getText();

    Properties props = new Properties();


    props.put("mail.smtp.host", "smtp.gmail.com");
    props.put("mail.smtp.socketFactory.port", "587");
    props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLsocketFactory");
    props.put("mail.smtp.auth", "true");
    props.put("mail.smtp.port", "587");
    props.put("mail.smtp.starttls.enable", "true"); 
    props.put("mail.smtp.ssl.trust", "smtp.gmail.com");
    props.put("mail.smtp.socketFactory.fallback", "true");
    props.put("mail.smtp.ssl.socketFactory", "true");
    props.put("mail.smtp.EnableSSL.enable","true");

    Session session = Session.getDefaultInstance(props,
            new javax.mail.Authenticator() {

                protected PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication(From, password);
                }
            }
    );


    try {
        Message message = new MimeMessage(session);
        message.setFrom(new InternetAddress(From));
        message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(To));
        message.setSubject(subject);

        MimeBodyPart messageBodyPart = new MimeBodyPart();
        messageBodyPart.setText(txtmessage);
        Multipart multiPart = new MimeMultipart();
        multiPart.addBodyPart(messageBodyPart);


        messageBodyPart = new MimeBodyPart();
        DataSource source = new FileDataSource(attachment_path);
        messageBodyPart.setDataHandler(new DataHandler(source));
        messageBodyPart.setFileName(txt_DocAttach.getText());
        multiPart.addBodyPart(messageBodyPart);

        message.setContent(multiPart);

        Transport.send(message);
        JOptionPane.showMessageDialog(rootPane, "Message is sent");
    } catch (MessagingException | HeadlessException e) {
        JOptionPane.showMessageDialog(rootPane, e);
    }

}

但它给了我以下错误,

javax.mail.MessagingException: 无法将套接字转换为 TLS;嵌套异常是:java.io.lOException:使用 SSL 套接字工厂 class 的 startTLS 异常:主机、端口 smtp.gmail.com、587;异常:java.lang.ClassNotFoundException:javax.netssl.SSLsocketFactory

试了很多,还是不知道怎么解决?请帮忙

尝试更改 属性 mail.smtp.socketFactory.class 的值 javax.net.ssl.SSLSocketFactory 而不是 javax.net.ssl.SSLsocketFactory, class 名称区分大小写。

有关连接属性的更多信息,您可以查看: Where to find all available Java mail properties?

正如其他人所指出的,您的套接字出厂设置有误。

更重要的是,you never needed to set them at all!

如果您的 JavaMail 库(mail.jar 或 javax.mail.jar)太旧,也会弹出此错误。 从这里下载最新版本:https://javaee.github.io/javamail/