javax.net.ssl.SSL握手异常:没有适当的协议(协议已禁用或密码套件不适当)

12

我知道有关于这个错误的几个问题,但对我来说都没有什么帮助。我有一个方法可以在gmail服务器上发送带附件的电子邮件,并且它工作正常。昨天我买了一台新的 Mac mini M1。我尝试使用此方法发送电子邮件,但是它会抛出此错误

 public static void sentEmail(String report){  
    BaseTest base = new BaseTest();
    
      
      String to = base.getProps().getProperty("emailTO"); ;//change accordingly  
      final String user = base.getProps().getProperty("emailFROM");//change accordingly  
      final String password = base.getProps().getProperty("emailPassword");//change accordingly  
       
      //1) get the session object     
      Properties properties = System.getProperties();  
      properties.setProperty("mail.smtp.host", "smtp.gmail.com");  
      properties.put("mail.smtp.auth", "true"); 
      properties.put("mail.smtp.port", "587");  
      properties.put("mail.smtp.starttls.enable", "true");
      
      
      Session session = Session.getDefaultInstance(properties,  
       new javax.mail.Authenticator() {  
       protected PasswordAuthentication getPasswordAuthentication() {  
       return new PasswordAuthentication(user,password);  
       }  
      });  
         
      //2) compose message     
      try{  
        MimeMessage message = new MimeMessage(session);  
        message.setFrom(new InternetAddress(user));  
        message.addRecipient(Message.RecipientType.TO,new InternetAddress(to));  
        message.setSubject("From Mobile Automation Project ");  
          
        //3) create MimeBodyPart object and set your message text     
        BodyPart messageBodyPart1 = new MimeBodyPart();  
        messageBodyPart1.setText("Report");  
          
        //4) create new MimeBodyPart object and set DataHandler object to this object      
        MimeBodyPart messageBodyPart2 = new MimeBodyPart();  
      
        String filename = report;//change accordingly  
        DataSource source = new FileDataSource(filename);  
        messageBodyPart2.setDataHandler(new DataHandler(source));  
        messageBodyPart2.setFileName(filename);  
         
         
        //5) create Multipart object and add MimeBodyPart objects to this object      
        Multipart multipart = new MimeMultipart();  
        multipart.addBodyPart(messageBodyPart1);  
        multipart.addBodyPart(messageBodyPart2);  
      
        //6) set the multiplart object to the message object  
        message.setContent(multipart );  
         
        //7) send message  
        Transport.send(message);  
       
       System.out.println("Report has been sent to: "+ to);  
       }catch (MessagingException ex) {ex.printStackTrace();}  
}

异常

    javax.mail.MessagingException: Could not convert socket to TLS;
  nested exception is:
    javax.net.ssl.SSLHandshakeException: No appropriate protocol (protocol is disabled or cipher suites are inappropriate)
    at com.sun.mail.smtp.SMTPTransport.startTLS(SMTPTransport.java:1907)
    at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:666)
    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 com.qa.utils.TestUtils.SendEmailTLS(TestUtils.java:228)
    at com.qa.BaseTest.afterSuite(BaseTest.java:153)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:134)
    at org.testng.internal.MethodInvocationHelper.invokeMethodConsideringTimeout(MethodInvocationHelper.java:63)
    at org.testng.internal.ConfigInvoker.invokeConfigurationMethod(ConfigInvoker.java:348)
    at org.testng.internal.ConfigInvoker.invokeConfigurations(ConfigInvoker.java:302)
    at org.testng.SuiteRunner.privateRun(SuiteRunner.java:351)
    at org.testng.SuiteRunner.run(SuiteRunner.java:286)
    at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:53)
    at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:96)
    at org.testng.TestNG.runSuitesSequentially(TestNG.java:1187)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1109)
    at org.testng.TestNG.runSuites(TestNG.java:1039)
    at org.testng.TestNG.run(TestNG.java:1007)
    at org.apache.maven.surefire.testng.TestNGExecutor.run(TestNGExecutor.java:284)
    at org.apache.maven.surefire.testng.TestNGXmlTestSuite.execute(TestNGXmlTestSuite.java:75)
    at org.apache.maven.surefire.testng.TestNGProvider.invoke(TestNGProvider.java:119)
    at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:428)
    at org.apache.maven.surefire.booter.ForkedBooter.execute(ForkedBooter.java:162)
    at org.apache.maven.surefire.booter.ForkedBooter.run(ForkedBooter.java:562)
    at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:548)
Caused by: javax.net.ssl.SSLHandshakeException: No appropriate protocol (protocol is disabled or cipher suites are inappropriate)
    at sun.security.ssl.HandshakeContext.<init>(HandshakeContext.java:171)
    at sun.security.ssl.ClientHandshakeContext.<init>(ClientHandshakeContext.java:101)
    at sun.security.ssl.TransportContext.kickstart(TransportContext.java:238)
    at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:394)
    at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:373)
    at com.sun.mail.util.SocketFetcher.configureSSLSocket(SocketFetcher.java:549)
    at com.sun.mail.util.SocketFetcher.startTLS(SocketFetcher.java:486)
    at com.sun.mail.smtp.SMTPTransport.startTLS(SMTPTransport.java:1902)
    ... 31 more 

我改变了这个参数

properties.setProperty("mail.smtp.host", "smtp.gmail.com");

对于这个问题

properties.setProperty("mail.smtp.ssl.trust", "smtp.gmail.com");

但是我遇到了这个异常

javax.mail.MessagingException: Could not connect to SMTP host: localhost, port: 587;
  nested exception is:
    java.net.ConnectException: Connection refused (Connection refused)
    at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1961)
    at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:654)
    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 com.qa.utils.TestUtils.sentEmail(TestUtils.java:189)
    at com.qa.BaseTest.afterSuite(BaseTest.java:152)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:134)
    at org.testng.internal.MethodInvocationHelper.invokeMethodConsideringTimeout(MethodInvocationHelper.java:63)
    at org.testng.internal.ConfigInvoker.invokeConfigurationMethod(ConfigInvoker.java:348)
    at org.testng.internal.ConfigInvoker.invokeConfigurations(ConfigInvoker.java:302)
    at org.testng.SuiteRunner.privateRun(SuiteRunner.java:351)
    at org.testng.SuiteRunner.run(SuiteRunner.java:286)
    at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:53)
    at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:96)
    at org.testng.TestNG.runSuitesSequentially(TestNG.java:1187)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1109)
    at org.testng.TestNG.runSuites(TestNG.java:1039)
    at org.testng.TestNG.run(TestNG.java:1007)
    at org.apache.maven.surefire.testng.TestNGExecutor.run(TestNGExecutor.java:284)
    at org.apache.maven.surefire.testng.TestNGXmlTestSuite.execute(TestNGXmlTestSuite.java:75)
    at org.apache.maven.surefire.testng.TestNGProvider.invoke(TestNGProvider.java:119)
    at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:428)
    at org.apache.maven.surefire.booter.ForkedBooter.execute(ForkedBooter.java:162)
    at org.apache.maven.surefire.booter.ForkedBooter.run(ForkedBooter.java:562)
    at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:548)
Caused by: java.net.ConnectException: Connection refused (Connection refused)
    at java.net.PlainSocketImpl.socketConnect(Native Method)
    at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:476)
    at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:218)
    at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:200)
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:394)
    at java.net.Socket.connect(Socket.java:606)
    at java.net.Socket.connect(Socket.java:555)
    at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:321)
    at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:237)
    at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1927)
    ... 31 more

你使用的是哪个客户端库? - Aerials
1
我正在使用javax.mail。 - misterios
在旧的 Mac 上,这段代码是可以工作的,我又检查了一遍 :/ 我认为在 Mac mini 上有些东西阻止了 SMTP 协议。 - misterios
6个回答

24

我遇到了类似的问题,似乎与 Deepak的回答相关。遵循这些说明解决了问题。

好像需要显式设置required标志和协议:

添加以下设置对我有用:

properties.put("mail.smtp.starttls.required", "true");
properties.put("mail.smtp.ssl.protocols", "TLSv1.2");

我必须补充说明一下,我在465端口进行了测试,完整的配置如下所示

properies.put("mail.smtp.host", "smtp.gmail.com");
properies.put("mail.smtp.port", "465");
properies.put("mail.smtp.auth", "true");
properies.put("mail.smtp.starttls.enable", "true");
properies.put("mail.smtp.starttls.required", "true");
properies.put("mail.smtp.ssl.protocols", "TLSv1.2");
properies.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");

1
谢谢,它起作用了并解决了我的问题。 - user11936657
这些代码应该放在哪里? - Mohit Tomar
@MohitTomar,你可以在初始化会话时设置这些属性(javax.mail.Session.getInstance(Properties))。或者,它们可以从系统属性中读取。此外,请参考此答案以获取属性列表:https://dev59.com/GGQn5IYBdhLWcg3wvpYV#16595037。 - Kariem
我使用Zoho邮件,properties.put("mail.smtp.ssl.protocols", "TLSv1.2")非常重要! - cpliu338

4

警告:启用tlsv1和tlsv1.1存在风险,请自行承担:

  1. 找到java.security文件保存的目录。 在Ubuntu中的情况下:

    /usr/lib/jvm/java-8-openjdk-amd64/jre/lib/security

  2. 找到jdk.tls.disabledAlgorithms

  3. 删除TLSv1,TLSv1.1

  4. 重新启动服务器并尝试再次发送邮件。


2
我将我的javax.mail升级到1.6.2并解决了问题。

您的回答可以通过添加更多支持信息来改进。请[编辑]以添加进一步的细节,例如引用或文档,以便他人可以确认您的答案是正确的。您可以在帮助中心中找到有关撰写良好答案的更多信息。 - Community
将javax.mail.jar升级到1.6.2版本也解决了我的问题。在此处找到:https://javaee.github.io/javamail/#Download_JavaMail_Release - Erich Kitzmueller

0

要解决这个问题,只需升级用于发送邮件的Java库即可。 这对我起作用了。


0
我的解决方案是启用安全端口和TLSv1.2。
mail.smtp.port=587
mail.smtp.ssl.protocols=TLSv1.2
mail.smtp.starttls.enable=true 

-2

我遇到了这个问题,因为我使用的是旧版本的mysql-connector-java-5.1.40-bin.jar 升级到5.1.49版本解决了我的问题


为什么选择MySQL?这个问题与javax.mail无关。 - Kendar

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接