I want to get my Exchange emails on android and for that I am using javamail api for android... it works great on gmail and yahoo using imap.
The problem is that my exchange server has self signed certificate so android don't like this too much and I get 03-14 12:46:13.698: WARN/System.err(281): javax.mail.MessagingException: Not trusted server certificate;
I have seen this example: Sending Email in Android using JavaMail API without using the default/built-in app where somebody makes a send example over ssl.. I think I can use that JSSEProvider to accept my self signed certificate but I don't know how can I use it.
Please help me!
I had the same problem, and solved it in what I believe is a cleaner and more succinct way. I used the same reference linked to by Mark Allison, but instead of overriding the SSLSocketFactory and implementing your own TrustManager, I added the following:
For more info, look in the "Socket Factories" section of the link: http://java.sun.com/products/javamail/javamail-1.4.2/SSLNOTES142.txt
Of note, when I did this, I had to use an "imap" store, as opposed to "imaps":
Here's my complete code, which works well for me when accessing an Exchange 2010 server:
I have been having the same problem, and managed to get around it by configuring a trust manager as detailed at http://java.sun.com/products/javamail/javamail-1.4.2/SSLNOTES142.txt.
What I did was create my own TrustManager:
and use this in my own SSLSocketFactory:
To get this to work in javamail-android, you need to specify the new SSLSocketFactory before you get a Session instance:
The TrustManager which we defined now be used instead of the default one, and all certificates will be accepted.
Obviously there are some security issues with blindly accepting all certificates, and I would suggest doing some checking in your TrustManager, otherwise you could open yourself up to all kinds of security issues (such as man-in-the-middle attacks). Also, I would only use this where you really have to: for example you say that GMail and Ymail is working, so I would not use this mechanism when connecting to those.
I would put in an exception handler to catch the "Certificate not trusted" exception, and prompt the user to accept an untrusted certificate (with the necessary warning to only do this for servers that are absolutely trusted) before actually overriding the TrustManager.