Class MailSSLSocketFactory


  • public class MailSSLSocketFactory
    extends javax.net.ssl.SSLSocketFactory
    An SSL socket factory that makes it easier to specify trust. This socket factory can be configured to trust all hosts or trust a specific set of hosts, in which case the server's certificate isn't verified. Alternatively, a custom TrustManager can be supplied.

    An instance of this factory can be set as the value of the mail.<protocol>.ssl.socketFactory property.

    Since:
    JavaMail 1.4.2
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private javax.net.ssl.SSLSocketFactory adapteeFactory
      Holds a SSLSocketFactory to pass all API-method-calls to
      private javax.net.ssl.KeyManager[] keyManagers
      Holds the KeyManager array to use
      private java.security.SecureRandom secureRandom
      Holds the SecureRandom to use
      private javax.net.ssl.SSLContext sslcontext
      Holds a SSLContext to get SSLSocketFactories from
      private boolean trustAllHosts
      Should all hosts be trusted?
      private java.lang.String[] trustedHosts
      String-array of trusted hosts
      private javax.net.ssl.TrustManager[] trustManagers
      Holds the TrustManager array to use
    • Constructor Summary

      Constructors 
      Constructor Description
      MailSSLSocketFactory()
      Initializes a new MailSSLSocketFactory.
      MailSSLSocketFactory​(java.lang.String protocol)
      Initializes a new MailSSLSocketFactory with a given protocol.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      java.net.Socket createSocket()  
      java.net.Socket createSocket​(java.lang.String s, int i)  
      java.net.Socket createSocket​(java.lang.String s, int i, java.net.InetAddress inetaddress, int j)  
      java.net.Socket createSocket​(java.net.InetAddress inetaddress, int i)  
      java.net.Socket createSocket​(java.net.InetAddress inetaddress, int i, java.net.InetAddress inetaddress1, int j)  
      java.net.Socket createSocket​(java.net.Socket socket, java.lang.String s, int i, boolean flag)  
      java.lang.String[] getDefaultCipherSuites()  
      javax.net.ssl.KeyManager[] getKeyManagers()  
      java.security.SecureRandom getSecureRandom()  
      java.lang.String[] getSupportedCipherSuites()  
      java.lang.String[] getTrustedHosts()  
      javax.net.ssl.TrustManager[] getTrustManagers()  
      boolean isServerTrusted​(java.lang.String server, javax.net.ssl.SSLSocket sslSocket)
      After a successful conection to the server, this method is called to ensure that the server should be trusted.
      boolean isTrustAllHosts()  
      private void newAdapteeFactory()
      Gets an SSLSocketFactory based on the given (or default) KeyManager array, TrustManager array and SecureRandom and sets it to the instance var adapteeFactory.
      void setKeyManagers​(javax.net.ssl.KeyManager[] keyManagers)  
      void setSecureRandom​(java.security.SecureRandom secureRandom)  
      void setTrustAllHosts​(boolean trustAllHosts)  
      void setTrustedHosts​(java.lang.String[] trustedHosts)  
      void setTrustManagers​(javax.net.ssl.TrustManager[] trustManagers)  
      • Methods inherited from class javax.net.ssl.SSLSocketFactory

        createSocket, getDefault
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • trustAllHosts

        private boolean trustAllHosts
        Should all hosts be trusted?
      • trustedHosts

        private java.lang.String[] trustedHosts
        String-array of trusted hosts
      • sslcontext

        private javax.net.ssl.SSLContext sslcontext
        Holds a SSLContext to get SSLSocketFactories from
      • keyManagers

        private javax.net.ssl.KeyManager[] keyManagers
        Holds the KeyManager array to use
      • trustManagers

        private javax.net.ssl.TrustManager[] trustManagers
        Holds the TrustManager array to use
      • secureRandom

        private java.security.SecureRandom secureRandom
        Holds the SecureRandom to use
      • adapteeFactory

        private javax.net.ssl.SSLSocketFactory adapteeFactory
        Holds a SSLSocketFactory to pass all API-method-calls to
    • Constructor Detail

      • MailSSLSocketFactory

        public MailSSLSocketFactory()
                             throws java.security.GeneralSecurityException
        Initializes a new MailSSLSocketFactory.
        Throws:
        java.security.GeneralSecurityException - for security errors
      • MailSSLSocketFactory

        public MailSSLSocketFactory​(java.lang.String protocol)
                             throws java.security.GeneralSecurityException
        Initializes a new MailSSLSocketFactory with a given protocol. Normally the protocol will be specified as "TLS".
        Parameters:
        protocol - The protocol to use
        Throws:
        java.security.NoSuchAlgorithmException - if given protocol is not supported
        java.security.GeneralSecurityException - for security errors
    • Method Detail

      • newAdapteeFactory

        private void newAdapteeFactory()
                                throws java.security.KeyManagementException
        Gets an SSLSocketFactory based on the given (or default) KeyManager array, TrustManager array and SecureRandom and sets it to the instance var adapteeFactory.
        Throws:
        java.security.KeyManagementException - for key manager errors
      • getKeyManagers

        public javax.net.ssl.KeyManager[] getKeyManagers()
        Returns:
        the keyManagers
      • setKeyManagers

        public void setKeyManagers​(javax.net.ssl.KeyManager[] keyManagers)
                            throws java.security.GeneralSecurityException
        Parameters:
        keyManagers - the keyManagers to set
        Throws:
        java.security.GeneralSecurityException - for security errors
      • getSecureRandom

        public java.security.SecureRandom getSecureRandom()
        Returns:
        the secureRandom
      • setSecureRandom

        public void setSecureRandom​(java.security.SecureRandom secureRandom)
                             throws java.security.GeneralSecurityException
        Parameters:
        secureRandom - the secureRandom to set
        Throws:
        java.security.GeneralSecurityException - for security errors
      • getTrustManagers

        public javax.net.ssl.TrustManager[] getTrustManagers()
        Returns:
        the trustManagers
      • setTrustManagers

        public void setTrustManagers​(javax.net.ssl.TrustManager[] trustManagers)
                              throws java.security.GeneralSecurityException
        Parameters:
        trustManagers - the trustManagers to set
        Throws:
        java.security.GeneralSecurityException - for security errors
      • isTrustAllHosts

        public boolean isTrustAllHosts()
        Returns:
        true if all hosts should be trusted
      • setTrustAllHosts

        public void setTrustAllHosts​(boolean trustAllHosts)
        Parameters:
        trustAllHosts - should all hosts be trusted?
      • getTrustedHosts

        public java.lang.String[] getTrustedHosts()
        Returns:
        the trusted hosts
      • setTrustedHosts

        public void setTrustedHosts​(java.lang.String[] trustedHosts)
        Parameters:
        trustedHosts - the hosts to trust
      • isServerTrusted

        public boolean isServerTrusted​(java.lang.String server,
                                       javax.net.ssl.SSLSocket sslSocket)
        After a successful conection to the server, this method is called to ensure that the server should be trusted.
        Parameters:
        server - name of the server we connected to
        sslSocket - SSLSocket connected to the server
        Returns:
        true if "trustAllHosts" is set to true OR the server is contained in the "trustedHosts" array;
      • createSocket

        public java.net.Socket createSocket​(java.net.Socket socket,
                                            java.lang.String s,
                                            int i,
                                            boolean flag)
                                     throws java.io.IOException
        Specified by:
        createSocket in class javax.net.ssl.SSLSocketFactory
        Throws:
        java.io.IOException
      • getDefaultCipherSuites

        public java.lang.String[] getDefaultCipherSuites()
        Specified by:
        getDefaultCipherSuites in class javax.net.ssl.SSLSocketFactory
      • getSupportedCipherSuites

        public java.lang.String[] getSupportedCipherSuites()
        Specified by:
        getSupportedCipherSuites in class javax.net.ssl.SSLSocketFactory
      • createSocket

        public java.net.Socket createSocket()
                                     throws java.io.IOException
        Overrides:
        createSocket in class javax.net.SocketFactory
        Throws:
        java.io.IOException
      • createSocket

        public java.net.Socket createSocket​(java.net.InetAddress inetaddress,
                                            int i,
                                            java.net.InetAddress inetaddress1,
                                            int j)
                                     throws java.io.IOException
        Specified by:
        createSocket in class javax.net.SocketFactory
        Throws:
        java.io.IOException
      • createSocket

        public java.net.Socket createSocket​(java.net.InetAddress inetaddress,
                                            int i)
                                     throws java.io.IOException
        Specified by:
        createSocket in class javax.net.SocketFactory
        Throws:
        java.io.IOException
      • createSocket

        public java.net.Socket createSocket​(java.lang.String s,
                                            int i,
                                            java.net.InetAddress inetaddress,
                                            int j)
                                     throws java.io.IOException,
                                            java.net.UnknownHostException
        Specified by:
        createSocket in class javax.net.SocketFactory
        Throws:
        java.io.IOException
        java.net.UnknownHostException
      • createSocket

        public java.net.Socket createSocket​(java.lang.String s,
                                            int i)
                                     throws java.io.IOException,
                                            java.net.UnknownHostException
        Specified by:
        createSocket in class javax.net.SocketFactory
        Throws:
        java.io.IOException
        java.net.UnknownHostException