11

I wasn't going to open a question, but I had no solution to this problem!
I have a problem receiving messages from IMAP server.
The error says "Caused by: java.lang.ClassNotFoundException: Didn't find class "java.awt.datatransfer.Transferable" on path...." Here's my code:

enter String test(){
    String all="";
    try{
        class Runner extends AsyncTask<Object, String, String> {
            @Override
            protected String doInBackground(Object... params) {
                Looper.prepare();
                String all ="";
                try{
                    Message[] msgs = ReceiveMail("imap.gmail.com","993","USER@gmail.com","PASS"); // After passing this line, error logging says error is in this line!
                    for(Message m: msgs){
                        all+=m.getSubject()+"\n"+m.getContent().toString()+"\n\n"; // Error shows here, but popups above
                    }
                    return all;
                }catch (Exception e){
                    e.printStackTrace();
                }
                Looper.loop();
                return all;
            }
        }
        Runner r = new Runner();
        all = r.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, this).get();
    }catch (Exception e){
        e.printStackTrace();
    }
    return all;
}
private Message[] ReceiveMail(String host,String port,String user,String pass){
    try{
        Properties props = new Properties();
        props.setProperty("mail.store.protocol", "imaps");
        props.setProperty("mail.imaps.host", host);
        props.setProperty("mail.imaps.port", port);
        props.setProperty("mail.imaps.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
        props.setProperty("mail.imaps.socketFactory.fallback", "false");
        Session imapSession = Session.getInstance(props);
        Store store = imapSession.getStore("imaps");
        store.connect(host, user, pass);
        Folder inbox = store.getFolder("Inbox");
        inbox.open(Folder.READ_ONLY);
        return inbox.getMessages();
    }catch (Exception e){
        e.printStackTrace();
        //log_all("ReceiveMail function: "+e.getMessage());
    }
    return null;
}

What is the problem?
Note When I don't use AsyncTask error "Network in main thread" appears.

SOLUTION:
Download these and add them as libraries.
Remove javax.* from dependencies tab in the module settings.
That'll solve it.

Omar AlMA3lOl
  • 268
  • 2
  • 9
  • Apparently, you are using some library that does not work on Android, as it was written for standard Java SE or Java EE. – CommonsWare Jun 07 '17 at 13:54
  • 1. I don't think AWT is included in Android. 2. Java.awt.datatransfer is for Drag and drop in java awt/swing applications. What did you do to get such a dependency??? – Axel Jun 07 '17 at 13:55
  • @CommonsWare I'm not using any external libraries. Just the once available in the dependencies tab -> Library Dependency! – Omar AlMA3lOl Jun 07 '17 at 14:01
  • @Axel I'm not so sure what I did to get it. I only copied the code from some other question answered and approved, but here we are! – Omar AlMA3lOl Jun 07 '17 at 14:02
  • "I'm not using any external libraries" -- yes, you are. "Just the once available in the dependencies tab -> Library Dependency!" -- those are external libraries. – CommonsWare Jun 07 '17 at 14:13
  • @CommonsWare Oh! Then what's my error case? – Omar AlMA3lOl Jun 07 '17 at 14:17
  • As I wrote, apparently, you are using some library that does not work on Android, as it was written for standard Java SE or Java EE. Stop using that library. Find some other solution for whatever you are trying to accomplish here. For example, [this library](https://github.com/konradrenner/kore-javamail) claims to be "Android ready". – CommonsWare Jun 07 '17 at 14:23
  • Can you post the code of `ReceiveMail`? – Axel Jun 07 '17 at 14:37
  • @Axel it's there! – Omar AlMA3lOl Jun 07 '17 at 15:36

2 Answers2

11

The JavaMail API Reference Implementation version 1.5.5 and later have built in support for Android and include support for OAuth2. Per the documentation:

Android does not provide a Java Compatible runtime and so can't run the standard JavaMail distribution. Instead, a special version of JavaMail is available for Android. This special version of JavaMail depends on a special version of the JavaBeans Activation Framework.

You can try out this version by adding the following to your build.gradle file for your Android application:

 android {
     packagingOptions {
         pickFirst 'META-INF/LICENSE.txt' // picks the JavaMail license file
     }
 }

 repositories { 
     jcenter()
     maven {
         url "https://maven.java.net/content/groups/public/"
     }
 }

 dependencies {
     compile 'com.sun.mail:android-mail:1.5.6'
     compile 'com.sun.mail:android-activation:1.5.6'
 }
jmehrens
  • 10,580
  • 1
  • 38
  • 47
2

Since it doesn't find some class maybe you are missing an external jar?

Edit

You can use JavaMail API to handle your email tasks. JavaMail API is available in JavaEE package and its jar is available for download. Sadly it cannot be used directly in an Android application since it uses AWT components which are completely incompatible in Android. (Thats why you get this error)

You can find the Android port for JavaMail at the following location: http://code.google.com/p/javamail-android/

Add the jars to your application and use the SMTP method

EduardoMaia
  • 591
  • 1
  • 4
  • 17