当前位置: 首页 > 面试题库 >

无法使用SMTP发送电子邮件(获取javax.mail.MessagingException:无法将套接字转换为TLS;)

魏勇军
2023-03-14
问题内容

我编写了以下代码,用于通过SMTP作为TLS使用javamail
API通过SMTP发送电子邮件,因为不支持SSL,但最终出现以下异常。请在下面查看我的代码。我使用了调试模式,在代码下面也可以找到异常。

import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class SendMailTLS {

    public static void main(String[] args) {

        final String username = "---------@mydomain.com";
        final String password = "***********";

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

        Session session = Session.getInstance(props,
          new javax.mail.Authenticator() {
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(username, password);
            }
          });
        session.setDebug(true);

        try {

            Message message = new MimeMessage(session);
            message.setFrom(new 
                  InternetAddress("---------@mydomain.com"));
            message.setRecipients(Message.RecipientType.TO,
            InternetAddress.parse("---------@mydomain.com"));
            message.setSubject("Testing Subject");
            message.setText("Dear Mail Crawler,"
                + "\n\n No spam to my email, please!");

            Transport.send(message);

            System.out.println("Done");

        } catch (MessagingException e) {
            throw new RuntimeException(e);
        }
    }
}

异常跟踪

DEBUG: setDebug: JavaMail version 1.4.5
DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc]
DEBUG SMTP: useEhlo true, useAuth true
DEBUG SMTP: useEhlo true, useAuth true
DEBUG SMTP: trying to connect to host "mail.mydomain.com", port 587, isSSL false
220-cpanel35.per.syra.net.au ESMTP Exim 4.80 #2 Fri, 05 Oct 2012 17:28:56 +0800 
220-We do not authorize the use of this system to transport unsolicited, 
220 and/or bulk e-mail.
DEBUG SMTP: connected to host "mail.mydomain.com", port: 587

EHLO xxxxxx.xxxxx.com
250-cpanel35.per.syra.net.au Hello xxxx.xxxxx.com [xx.xx.xx.xxx]i
250-SIZE 52428800
250-8BITMIME
250-PIPELINING
250-AUTH PLAIN LOGIN
250-STARTTLS
250 HELP
DEBUG SMTP: Found extension "SIZE", arg "52428800"
DEBUG SMTP: Found extension "8BITMIME", arg ""
DEBUG SMTP: Found extension "PIPELINING", arg ""
DEBUG SMTP: Found extension "AUTH", arg "PLAIN LOGIN"
DEBUG SMTP: Found extension "STARTTLS", arg ""
DEBUG SMTP: Found extension "HELP", arg ""
STARTTLS
220 TLS go ahead
Exception in thread "main" java.lang.RuntimeException: javax.mail.MessagingException: Could not convert socket to TLS;
  nested exception is:
    javax.net.ssl.SSLException: java.lang.RuntimeException: Could not generate DH keypair
    at SendMailTLS.main(SendMailTLS.java:52)
Caused by: javax.mail.MessagingException: Could not convert socket to TLS;
  nested exception is:
    javax.net.ssl.SSLException: java.lang.RuntimeException: Could not generate DH keypair
    at com.sun.mail.smtp.SMTPTransport.startTLS(SMTPTransport.java:1918)
    at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:652)
    at javax.mail.Service.connect(Service.java:317)
    at javax.mail.Service.connect(Service.java:176)
    at javax.mail.Service.connect(Service.java:125)
    at javax.mail.Transport.send0(Transport.java:194)
    at javax.mail.Transport.send(Transport.java:124)
    at SendMailTLS.main(SendMailTLS.java:47)
Caused by: javax.net.ssl.SSLException: java.lang.RuntimeException: Could not generate DH keypair
    at sun.security.ssl.Alerts.getSSLException(Alerts.java:208)
    at sun.security.ssl.SSLSocketImpl.fatal(SSLSocketImpl.java:1868)
    at sun.security.ssl.SSLSocketImpl.fatal(SSLSocketImpl.java:1826)
    at sun.security.ssl.SSLSocketImpl.handleException(SSLSocketImpl.java:1809)
    at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1328)
    at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1305)
    at com.sun.mail.util.SocketFetcher.configureSSLSocket(SocketFetcher.java:548)
    at com.sun.mail.util.SocketFetcher.startTLS(SocketFetcher.java:485)
    at com.sun.mail.smtp.SMTPTransport.startTLS(SMTPTransport.java:1913)
    ... 7 more
Caused by: java.lang.RuntimeException: Could not generate DH keypair
    at sun.security.ssl.DHCrypt.<init>(DHCrypt.java:123)
    at sun.security.ssl.ClientHandshaker.serverKeyExchange(ClientHandshaker.java:618)
    at sun.security.ssl.ClientHandshaker.processMessage(ClientHandshaker.java:202)
    at sun.security.ssl.Handshaker.processLoop(Handshaker.java:868)
    at sun.security.ssl.Handshaker.process_record(Handshaker.java:804)
    at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:998)
    at sun.security.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1294)
    at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1321)
    ... 11 more
Caused by: java.security.InvalidAlgorithmParameterException: Prime size must be multiple of 64, and can only range from 512 to 1024 (inclusive)
    at com.sun.crypto.provider.DHKeyPairGenerator.initialize(DHKeyPairGenerator.java:120)
    at java.security.KeyPairGenerator$Delegate.initialize(KeyPairGenerator.java:658)
    at sun.security.ssl.DHCrypt.<init>(DHCrypt.java:117)
    ... 18 more

谁能帮我调试一下吗?提前致谢!


问题答案:

我通过评论以下属性解决了这个问题

props.put("mail.smtp.starttls.enable", "true");

并且代码执行时没有错误或警告,或者只是从上述源代码中删除了这一行。到现在为止,它就像是一种魅力。



 类似资料:
  • 我编写了以下代码,用于通过SMTP使用javamail API发送电子邮件,因为SSL不受支持,但最终出现以下异常。请看下面我的代码。我使用了调试模式,在代码下面你也可以找到异常。 异常跟踪 有人能帮我调试一下吗?提前感谢!

  • 使用JavaMail通过SMTP(STARTTLS)发送消息时,会引发以下异常: 这里(关于stackoverflow)和其他地方的多个帖子表明邮件传输会话的一些属性,即 在解决这个问题中发挥作用,但我尝试了所有这些参数的组合(存在/不存在),没有一个能解决问题。 有人知道如何解决这个问题吗? 其他信息/详细信息: Java版本:1.6.0 JavaMail版本:1.5.2 邮件传输会话的属性:

  • 问题内容: 我目前正在从事一个涉及创建.jar文件的项目。要实现以下目标,我应该导出/读取朋友注册表的路径,一旦完成,我应该通过电子邮件将结果取回。整个概念是创建jar文件,一旦单击它,由于实际上是通过电子邮件发送的,因此我会通过电子邮件获取结果。 (我希望这是有道理的) 因此,首先,我结合了以下代码来实际读取注册表并显示键(我从堆栈溢出的流行帖子中获取了读取/写入注册表),因此读取过程运行正常,

  • 我想通过单击JFrame netbeans中的按钮将邮件从我的电子邮件地址发送到另一个电子邮件地址。这是代码, 但它给了我以下错误, javax.mail.MessagingException:无法将套接字转换为TLS;嵌套异常是:java.io.lOException:使用SSL套接字工厂类启动TLS中的异常null: host, portsmtp.gmail.com,587;异常:java.l

  • responseerror:禁止在d:\node-projects\ecom\node_modules@sendgrid\client\src\类\client.js:146:29在processTicksAndRejections(internal/process/task_queues.js:93:5){ 代码:403,response:{headers:{server:'nginx',dat

  • 我的: 但是邮件不会发送到该地址,在控制台中,当我单击时,正在打印,但它不会发送任何电子邮件。 我用的是Django 1.3.7和Python 2.6。 不知道是版本的问题还是什么逻辑问题。