java.lang.NoClassDefFoundError: javax.activation.DataHandler in android

I am making a email sending application in android phone. For that I included two jar file activation. jar and mail.jar. But when I run this application and try to send the mail, I got following error on LogCat.

java.lang.NoClassDefFoundError: javax.activation.DataHandler 

I am new to android. so I am not able to figure out this issue. please Help me. Thanks


Solution 1:

There is an Android-friendly port of javamail which you should be using. There are three libraries that you need to include in your app: mail.jar, activation.jar, and additionnal.jar(sic). It looks like you are missing something that the activation library depends on, and this could be because you are not using the Android port of this library.

I have used the Android-friendly version of javamail successfully in a project, and it works really well.

Solution 2:

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.5'
     compile 'com.sun.mail:android-activation:1.5.5'
 }

Starting with JavaMail 1.6.0:

JavaMail for Android requires at least Android API level 19, which corresponds to Android KitKat, currently the oldest supported version of Android.