Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaMail: Java.awt.datatransfer.Transferable doesn't exist [duplicate]

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","[email protected]","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.

like image 588
Omar AlMA3lOl Avatar asked Jun 07 '17 13:06

Omar AlMA3lOl


2 Answers

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'
 }
like image 56
jmehrens Avatar answered Sep 20 '22 09:09

jmehrens


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

like image 37
EduardoMaia Avatar answered Sep 21 '22 09:09

EduardoMaia