【问题标题】:javax.mail.MessagingException: Could not convert socket to TLS exception in JAVAjavax.mail.MessagingException:无法在 JAVA 中将套接字转换为 TLS 异常
【发布时间】:2020-12-07 22:02:53
【问题描述】:

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

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 套接字工厂类 null 的 startTLS 中的异常:主机,端口 smtp.gmail.com,587;异常:java.lang.ClassNotFoundException:javax.netssl.SSLsocketFactory

尝试了很多但不知道该怎么做才能解决它?请帮忙。

【问题讨论】:

标签: java sockets jakarta-mail


【解决方案1】:

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

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

【讨论】:

  • Error- javax.mail.AuthenticationFailedException: 534-5.7.14 accounts.google.com/… 534-5.7.14 DGIvIgYXkeOlV79U3q10CAZVgGssj71-61zalOYanX7y17JD-RgjEP2IrygyJvocK8nnni_Dal 534-5.7.14 JB1Y1z2rnFlellFykigZeHsb0AurEkRhyKOmF6-N54SAw54m5GhdO_UTb6Lab-pvco0HU3 534-5.7.14 Y-qu_XRInhyL1zLivixTgBS240PYWOHdlh8OH6utgMDqwgHB2K0guCwCoODOD-17.H3Zy_Li0 534-5.7.14 alNihYoo8JGwy02u7AgtaX0Pa4hKs> 请通过您的网络浏览器和 534-5.7.14 登录,然后重试。 534-5.7.14 了解更多信息,请致电 534 5.7.14 supporlgoogle.comimail/answer/78754 t6sm11250727pgo.42 - gsmtp
  • 这是一个不同的问题,也许这个链接中的答案可以帮助你stackoverflow.com/questions/20337040/…如果上面的链接对你没有帮助,我建议你为此打开新问题
  • 很高兴听到,接受未来的人的答案 :)
【解决方案2】:

正如其他人指出的那样,您的套接字出厂设置错误。

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

【讨论】:

    【解决方案3】:

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

    【讨论】:

      猜你喜欢
      • 2023-03-08
      • 1970-01-01
      • 2021-10-06
      • 2019-11-10
      • 2012-09-26
      • 2013-06-22
      • 2013-04-13
      • 2016-11-13
      • 1970-01-01
      相关资源
      最近更新 更多