0

I want to read my emails from ms exchange, I am using java mail api from here http://code.google.com/p/javamail-android/ and I have successfully extract the emails from my google account... The problem with exchange is that I have it locally and I don't have public certificate and android gives me 03-07 13:12:25.708: WARN/System.err(3886): javax.mail.MessagingException: Not trusted server certificate;

How can I pass the certificate check?

I have seen the example of Vynayak from here: Sending Email in Android using JavaMail API without using the default/built-in app

that he uses some TrustManager class and I had tried to make that too but I don't know how to link the trustFactory class to the imap properties.

So far I have this in my activity:

Properties props = new Properties();
    props.setProperty("mail.store.protocol", "imaps");

    props.setProperty("mail.store.socketFactory.class",
                "com.imap.DummySSLSocketFactory");


    // Prevents to fall into NOT-secure connection
    props.setProperty("mail.pop3.socketFactory.fallback", "false");

    try {
        Session session = Session.getDefaultInstance(props, null);
        Store store = session.getStore("imaps");
        store.connect("mail.domain.com", "user", "pass");

        System.out.println(store);

        Folder inbox = store.getFolder("Inbox");
        inbox.open(Folder.READ_ONLY);
        Message messages[] = inbox.getMessages();
        for (Message message : messages) {
            System.out.println(message);
        }
    } catch (NoSuchProviderException e) {
        e.printStackTrace();
        System.exit(1);
    } catch (MessagingException e) {
        e.printStackTrace();
        System.exit(2);
    }

Here I know that the line props.setProperty("mail.store.socketFactory.class", "com.imap.DummySSLSocketFactory"); is not implemented properly, because I don't know how can I use it wit imaps..

And the DummyTrustManager:

package com.imap;

import javax.net.ssl.X509TrustManager;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;

public class DummyTrustManager implements X509TrustManager {

  public void checkClientTrusted(X509Certificate[] cert, String authType) throws CertificateException {

      // everything is trusted

  }

  public void checkServerTrusted(X509Certificate[] cert, String authType) throws CertificateException {

      // everything is trusted

  }

  public X509Certificate[] getAcceptedIssuers() {

      return new X509Certificate[0];

  }

} 

And the DummySoketSSLFactory:

  package com.imap;

  import java.io.IOException;
  import java.net.InetAddress;
  import java.net.Socket;
  import javax.net.SocketFactory;
  import javax.net.ssl.*;

  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) {

          }

      }

      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();

      }

  }

The dummy classes are from http://www.anddev.org/advanced-tutorials-f21/javamail-and-android-little-excursus-t3093.html

Please help me to get this work

Community
  • 1
  • 1
Cata
  • 11,133
  • 11
  • 65
  • 86

1 Answers1

1

The easiest solution is to create a self signing certificate. Example here.

Lucas B
  • 11,793
  • 5
  • 37
  • 50
  • Hi, thank's for posting, I have a self signed certificate from the microsoft CA, and android seems to have problems accepting such certificates – Cata Mar 07 '11 at 13:12
  • Kind of a crazy question, but does it throw the error if you point against a valid public certified site? – Lucas B Mar 07 '11 at 18:29
  • Hi lucas, unfortunately I don't have a public server to test on, only google – Cata Mar 08 '11 at 06:35