0

I'm making an application on android that retrieves emails from gmail accounts using JavaMail API. Here's my code:

    Properties props = new Properties();
    props.setProperty("mail.store.protocol", "imaps");
    props.setProperty("mail.imaps.host", mailServer);
    props.setProperty("mail.imaps.port", "993");
    props.setProperty("mail.imaps.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
    props.setProperty("mail.imaps.socketFactory.fallback", "false");
    if (debug) Log.i("check","before session");
    Session imapSession = Session.getDefaultInstance(props,null);
    if (debug) Log.i("check","after session");

Whenever the code reaches the imapSession line, the application just crashes. In other words, it never reaches the last line. I've tried several mailServer strings:

imaps.gmail.com
imap.gmail.com
imaps.googlemail.com
imap.googlemail.com

and nothing seems to work. Is it the port? I'm sure I've added the right permissions, the error in the log cat doesn't mention the permissions anyways.

Another question where can I get such information for Gmail and other email servers?

himura
  • 1,555
  • 3
  • 19
  • 30

1 Answers1

1

Let's start by simplifying things. You've made several of the common JavaMail mistakes. Throw your code away and use the simple code in the JavaMail FAQ for connecting to Gmail. Then use the tips in the JavaMail FAQ for debugging any remaining problems. If you still have problems, post additional details here.

Bill Shannon
  • 29,579
  • 6
  • 38
  • 40
  • Thank you... I'll go through the links and come back to you. Sorry i got busy these days – himura Aug 15 '12 at 20:24
  • So my mistakes were: 1- Session imapSession = Session.getDefaultInstance(props,null); 2- props.setProperty("mail.imaps.socketFactory.class", "javax.net.ssl.SSLSocketFactory"); what i did: 1- may make a problem for me latter, but for now i don't think it's the problem since the app crashes from the very first use. But anyways, its better to use .getInstance()... am I righ? 2- i removed it and set "mail.smtp.ssl.enable" to "true" as the link suggests. I also removed: props.setProperty("mail.imaps.socketFactory.fallback", "false"); – himura Aug 23 '12 at 13:44
  • 1
    there's still an error - here's the log - E/AndroidRuntime(19029): java.lang.NoClassDefFoundError: javax.mail.Session . . . i found that i have to add some jar files, but i did add them! where did i go wrong?! – himura Aug 23 '12 at 13:53
  • Well, clearly you didn't add them correctly. Is this what you meant by "the app is crashing" in your original post? Anyway, it looks like you're into an Android-specific problem that someone else will have to help you with. There's lots of other questions here about using JavaMail with Android so maybe a little searching will turn up the answer. – Bill Shannon Aug 23 '12 at 17:29
  • I have just fixed it. it seems that i had to arrange them in a certain order! i followed [this answer](http://stackoverflow.com/questions/11823975/getting-noclassdeffounderror-when-using-common-lang-stringutils-in-android-java/11824038#11824038) it worked, but I don't understand why!!? do you have any idea? – himura Aug 23 '12 at 18:21