javax.mail.MessagingException:无法将套接字转换为TLS

时间:2013-10-18 13:45:55

标签: java sockets email ssl

我有点转换套接字异常。 Stackoverflow将我与相关答案联系在一起,我完成了人们在那里建议的所有事情。

这里有异常描述:

  

线程“main”中的异常javax.mail.MessagingException:无法将套接字转换为TLS;     嵌套异常是:       java.net.SocketException:java.security.NoSuchAlgorithmException:构造错误            实现(算法:默认,提供者:SunJSSE,类:
          sun.security.ssl.SSLContextImpl $ DefaultSSLContext)       at com.sun.mail.smtp.SMTPTransport.startTLS(SMTPTransport.java:1880)       at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:648)       在javax.mail.Service.connect(Service.java:295)       在XMailMessenger.MailMessenger.sendMail(MailMessenger.java:414)       在alfa.runner.main(runner.java:38)           引起:java.net.SocketException:java.security.NoSuchAlgorithmException:错误
           构造实现(算法:默认,提供者:SunJSSE,类:           sun.security.ssl.SSLContextImpl $ DefaultSSLContext)       在javax.net.ssl.DefaultSSLSocketFactory.throwException(未知来源)       在javax.net.ssl.DefaultSSLSocketFactory.createSocket(未知来源)       在com.sun.mail.util.SocketFetcher.startTLS(SocketFetcher.java:432)       在com.sun.mail.smtp.SMTPTransport.startTLS(SMTPTransport.java:1875)       ......还有4个           引起:java.security.NoSuchAlgorithmException:构造实现时出错           (算法:默认,提供者:SunJSSE,班级:
          sun.security.ssl.SSLContextImpl $ DefaultSSLContext)       at java.security.Provider $ Service.newInstance(Unknown Source)       at sun.security.jca.GetInstance.getInstance(Unknown Source)       at sun.security.jca.GetInstance.getInstance(Unknown Source)       在javax.net.ssl.SSLContext.getInstance(未知来源)       在javax.net.ssl.SSLContext.getDefault(未知来源)       在javax.net.ssl.SSLSocketFactory.getDefault(未知来源)       在com.sun.mail.util.SocketFetcher.startTLS(SocketFetcher.java:427)       ......还有5个          引起:java.lang.NullPointerException       at sun.security.ssl.KeyManagerFactoryImpl $ SunX509.engineInit(Unknown Source)       在javax.net.ssl.KeyManagerFactory.init(未知来源)       at sun.security.ssl.SSLContextImpl $ DefaultSSLContext.getDefaultKeyManager(Unknown
          源)
      at sun.security.ssl.SSLContextImpl $ DefaultSSLContext。(Unknown Source)       at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)       at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)       at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)       at java.lang.reflect.Constructor.newInstance(Unknown Source)       在java.lang.Class.newInstance(未知来源)       ......还有12个

所以我决定再写2个课程:

      package XMailMessenger;

     import javax.net.ssl.X509TrustManager;
     import java.security.cert.X509Certificate;
     /**
      * @author Raminderjeet Singh
      */
      public class DummyTrustManager implements X509TrustManager {

      public void checkClientTrusted(X509Certificate[] cert, String authType) {
    // everything is trusted


   }

    public void checkServerTrusted(X509Certificate[] cert, String authType) {
   // everything is trusted
   }

   public X509Certificate[] getAcceptedIssuers() {
      return new X509Certificate[0];
   }
   }

和第二个:

 package XMailMessenger;

  import java.io.IOException;
  import java.net.InetAddress;
  import java.net.Socket;

  import javax.net.SocketFactory;
   import javax.net.ssl.*;

  /**
  * @author Raminderjeet Singh
 */
  public class DummySSLSocketFactory extends SSLSocketFactory {
   private SSLSocketFactory factory;

    public DummySSLSocketFactory() {
try {
    SSLContext sslcontext = SSLContext.getInstance("TLS");
    sslcontext.init(null,
             new TrustManager[] { new DummyTrustManager()},
             null);
    factory = (SSLSocketFactory)sslcontext.getSocketFactory();
} catch(Exception ex) {
    // ignore
}
}

public static SocketFactory getDefault() {
return new DummySSLSocketFactory();
}

public Socket createSocket() throws IOException {
return factory.createSocket();
}

public Socket createSocket(Socket socket, String s, int i, boolean flag)
            throws IOException {
return factory.createSocket(socket, s, i, flag);
}

   public Socket createSocket(InetAddress inaddr, int i,
            InetAddress inaddr1, int j) throws IOException {
return factory.createSocket(inaddr, i, inaddr1, j);
    }

   public Socket createSocket(InetAddress inaddr, int i)
            throws IOException {
 return factory.createSocket(inaddr, i);
   }

  public Socket createSocket(String s, int i, InetAddress inaddr, int j)
            throws IOException {
  return factory.createSocket(s, i, inaddr, j);
   }

  public Socket createSocket(String s, int i) throws IOException {
  return factory.createSocket(s, i);
  }

  public String[] getDefaultCipherSuites() {
  return factory.getDefaultCipherSuites();
  }

    public String[] getSupportedCipherSuites() {
        return factory.getSupportedCipherSuites();
   }
  }

最后我们有发送邮件的方法:

        Properties props = new Properties();
    props.put("mail.smtp.host", currentSettingsDocument.getSMTPHostServer());
    props.put("mail.smtp.user", currentSettingsDocument.getLogin());
    props.put("mail.smtp.password", currentSettingsDocument.getPassword());
    props.put("mail.smtp.port", "465");
    props.put("mail.smtp.auth", "true");
    props.put("mail.smtp.timeout" , "3000");
    props.put("mail.smtp.starttls.enable", "false");
    props.put("mail.smtp.socketFactory.port", "465");

    props.put("mail.smtp.socketFactory.fallback", "false");
    props.put("mail.smtp.ssl.trust", "smtp.gmail.com");
    props.put("javax.net.debug","ssl,session");

    // Required to avoid security exception.
        Authenticator authentication = new Authenticator() {
          protected PasswordAuthentication getPasswordAuthentication() {
             return new PasswordAuthentication(currentSettingsDocument.getLogin() , 
            currentSettingsDocument.getPassword());  
          }
         };
    Session session = Session.getInstance(props, authentication);
    // create some properties and get the default Session

    // create a message
    MimeMessage msg = new MimeMessage(session);

    // set the from and to address
    InternetAddress addressFrom = new    


       InternetAddress(currentSettingsDocument.getSenderMail());
   msg.setFrom(addressFrom);


       for(InternetAddress addr: recepients )
        msg.addRecipient(Message.RecipientType.TO, addr );

    // Optional : You can also set your custom headers in the Email if you
    // 
    // msg.addHeader("MyHeaderName", "myHeaderValue");

    // Setting the Subject and Content Type
    msg.setContent("1111", "text/plain");
    Transport transport = session.getTransport("smtp");
            Document.getSMTPHostServer(), Document.getPassword());
    transport.connect(currentSettingsDocument.getSMTPHostServer(), 465,                                 
    transport.sendMessage(msg, msg.getAllRecipients());
    transport.close();

调用 sendMessage 后,我得到例外:

  

окт18,20135:36:18 PM XMailMessenger.MailMessenger sendMail   严重:空   javax.mail.MessagingException:异常读取响应;     嵌套异常是:       java.net.SocketTimeoutException:读取超时

PS Google邮件正常运行

1 个答案:

答案 0 :(得分:0)

异常是由旧Windows(XP,Vista)上不受支持的 TLS 引起的,并且由类SocketFetcher(包com.sun.mail.util;)在行

引发
MailSSLSocketFactory msf = new MailSSLSocketFactory();

和MailSSLSocketFactory(同一个包)/** * Initializes a new MailSSLSocketFactory. * * @throws GeneralSecurityException */ public MailSSLSocketFactory() throws GeneralSecurityException { this("TLS"); }