I got imaps to work - here is how

Help
Anonymous
2003-01-29
2013-04-09
  • Anonymous - 2003-01-29

    In JwmaSession.java, the initMailSession(String password) method needs to add support for imaps:

    change the line:

                m_MailSession = Session.getInstance(System.getProperties(), null);

    to:

                 Properties props = System.getProperties();
                 Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
                 props.setProperty("mail.imap.socketFactory.class",
                                         "dtw.webmail.DummySSLSocketFactory");
                 props.setProperty("mail.imap.socketFactory.fallback", "false");
                 props.setProperty("mail.imap.socketFactory.port", "993");
                 props.setProperty("mail.imap.port", "993");
                 Security.setProperty("ssl.SocketFactory.provider",
                                      "dtw.webmail.DummySSLSocketFactory");

                m_MailSession = Session.getInstance(props, null);

    and add two files :

    1) dtw.webmail.DummySSLSocketFactory
    2) dtw.webmail.DummyTrustManager

    here they are (a bit long, sorry)

    ---------------------------

    package dtw.webmail;

    import java.io.IOException;

    import java.net.InetAddress;
    import java.net.Socket;

    import javax.net.SocketFactory;
    import javax.net.ssl.SSLContext;
    import javax.net.ssl.SSLSocketFactory;
    import javax.net.ssl.TrustManager;

    /**
    * A custom SSLSocketFactory for generating SSL sockets
    * used by JavaMail for access to secure IMAP stores.
    *
    * Taken from
    * http://www.javaworld.com/javatips/jw-javatip115.html
    * Java Tip 115: Secure JavaMail with JSSE
    * Add secure, SSL-based connections to JavaMail
    * By Eugen Kuleshov and Dmitry I. Platonoff
    *
    */
    public class DummySSLSocketFactory extends SSLSocketFactory {
        private SSLSocketFactory factory;

        public DummySSLSocketFactory() {
            System.out.println("DummySocketFactory instantiated");

            try {
                SSLContext sslcontext = SSLContext.getInstance("TLS");
                sslcontext.init(null, // No KeyManager required
                                new TrustManager[] { new DummyTrustManager() },
                                new java.security.SecureRandom());
                factory = (SSLSocketFactory) sslcontext.getSocketFactory();
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }

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

        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.getSupportedCipherSuites();
        }

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

    ---------------------------

    package dtw.webmail;

    import java.security.cert.X509Certificate;

    import javax.net.ssl.X509TrustManager;

    /**
    * A simple TrustManager implementation that accepts
    * all certificates without validation.
    *
    * Taken from
    * http://www.javaworld.com/javatips/jw-javatip115.html
    * Java Tip 115: Secure JavaMail with JSSE
    * Add secure, SSL-based connections to JavaMail
    * By Eugen Kuleshov and Dmitry I. Platonoff
    *
    */
    public class DummyTrustManager implements X509TrustManager {
        public boolean isClientTrusted(X509Certificate[] cert) {
            return true;
        }

        public boolean isServerTrusted(X509Certificate[] cert) {
            return true;
        }

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

        public void checkClientTrusted(X509Certificate[] p0, String p1)
                                throws java.security.cert.CertificateException {
        }

        public void checkServerTrusted(X509Certificate[] p0, String p1)
                                throws java.security.cert.CertificateException {
        }
    }

    ------------------------------

    Hope that helps,
    Nate

     
    • Dieter Wimberger

      Hi Nate,

      Thanks for your post. I will see about finishing the hook in the jwma configuration for the secure socket implementations.

      Which libraries are you using (jsse? version?). I have not been able to adapt your code to work on OS X.

      Regards,
      Dieter

       
    • Nathaniel A. Johnson

      Hello,

      I am using jdk 1.4.1 where jsse is packed inside of the jdk.  Is that enough info, or are you looking for something more specific? 

      Nate

       
    • Michel Romy

      Michel Romy - 2003-05-15

      I have some trouble with the use of IMAPS with jwma. Is it now implemented in jwma 0.9.8 ?

      I tried and change address and port (993) in configuration.xml ?do I have also to switch protocol from "imap" to "imaps" ?

      Thanks for your help !

      Michel

       
      • Dieter Wimberger

        Michel,

        The following configurations can be/should be done:
        1. Socket Factory for secure sockets:

        <security>
            <secure-socket-factory>javax.net.ssl.SSLSocketFactory</secure-socket-factory>
        </security>

        This is the class that will be used to create secure sockets. Note that the above is the standard class, and requires you to have the correct certificate for the IMAP Server stored in the keystore of the user that is running the servlet container which hosts jwma.

        2. Set post office to secure
        <postoffice name="Default PostOffice" default="true" type="plain" secure="true">

        3. Set port to 993 (don't change the protocol!)
        <port>993</port>

        Now, this should basically work.

        However, a frequent problem is that the certificate of the IMAP server is not in the users keystore. You can do two things:
        1. place it there
        2. use the classes described in this Thread (allow to connect to any also non authenticated server) and  set the socket factory accordingly:
        <secure-socket-factory>my.package.security.SSLSocketFactory</secure-socket-factory>

        If it still does not work out, contact me, we get you going somehow (although we will need some debugging probably).

        Regards,
        Dieter

         
        • Dieter Wimberger

          Michel,

          Forgot some other piece of information that might be important:
          The security provider should be correctly configured for the JVM you are using. You can do so configuring your JDK approbiately (see documentation), or you can add the statements manipulating the security to your SocketFactory (as static initializer for example).
          Might be:
          Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());

          Plus other things that might be necessary to configure the specific provider you want to use.

          Regards,
          Dieter

           
  • hitesh shah

    hitesh shah - 2010-12-22

    I have put your war file into webapps directory of tomcat AS. I can see login screen but I can't get inside. What is the initial username and password to get into webmail?

    If we have to setup username and password then how do we do it.

     
  • Leonard Sitongia

    Hi,
    In the download, you'll find the docs directory, where there's a configuration document.  You configure references to the IMAP server, and you authenticate using your account on the IMAP server.  There's no login to webmail itself, unless you configure administrators, who also authenticate against the IMAP server.

     

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:





No, thanks