Search code examples
androidemail-clientself-signed

How can I connect to a mail server using IMAP over SSL in android?


I need to establish the Connection with MailServer (Custom Mail server). Whenever I tried to connect it throws the javax.net.SSLException, Not trusted server certificate exception.

I don,t know how to create the certificate for this. And also don't know to pass that certificate to make the secure connection with mail server.

My Code is:

Properties props;// = new Properties();
            Session session;

            props=new Properties();

            props.put("mail.imap.socketFactory.port", "993");   
            props.put("mail.imap.socketFactory.class",   
                    "javx.net.ssl.SSLSocketFactory");   
            session=Session.getDefaultInstance(props, null);
            Store store = session.getStore("imaps");
            store.connect(hostName,portNumber, emailId,password);
            //the above statement throws the Exception    
            Folder folder = store.getFolder("INBOX");

I'd like to know how to create a self-signed certificate for an Android application.


Solution

  • Creating a self-signed certificate won't solve your problem, it is the fact that the server you're connecting to is using a self-signed certificate that's causing the error that you're seeing.

    You either need to purchase a trusted certificate and install it on the mail server (which may be outside of your control) or you need to change the behaviour of javamail to accept certificates which are not signed by a recognised authority.

    Have a look at my answer to android javamail api imap over ssl which may help you to implement the second option.