Javamail getContent() 致命错误 java.lang.NoClassDefFoundError

Javamail getContent() fatal error java.lang.NoClassDefFoundError

我正在尝试制作一个应用程序来 "simply" 阅读纯文本电子邮件(而且只有纯文本。我发送了电子邮件,所以我知道它只是纯文本)。
我在这里和那里拿起代码来做。
我终于成功地阅读了 "from" "subject" "sentDate" 和其他,但没有 "content"。这是我想阅读的内容...
一切正常,但是当我调用 getContent() 时,我遇到了一个致命错误。

我在 app\libs.
中使用 javax.mail.jar = javamail 1.6.2 我试过 mail.jar 和 activation.jar,但没有用。
我试图将所有阅读代码放入 "void" 中,但没有用。

即使打字

Object content = message.getContent();

没用...

在清单中,我有

<uses-permission android:name="android.permission.INTERNET" />

在 build.graddle(模块应用程序)中我有

compile files('libs/javax.mail.jar')

mainActivity.java:

package myName.myAPP;


import android.os.StrictMode;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.Properties;

import javax.mail.Address;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Store;


public class MainActivity extends AppCompatActivity {



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        String msg = "";
        String from;
        String subject;
        String sentDate;
        String contentType


        // -----------------------------------------------StricMode ERROR--------------------------------------
        // Ces deux lignes sont à ajouter si l'on veut éviter une erreur:
        // at android.os.strictmode$androidblockguardpolicy.onnetwork
        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);
        // -----------------------------------------------StricMode ERROR--------------------------------------

        Properties connectionProperties = new Properties();
        Session session = Session.getDefaultInstance(connectionProperties,null);
        try {
            Store store = session.getStore("pop3");
            store.connect("pop.free.fr", "user", "password");
            Folder folderInbox = store.getFolder("Inbox");
            folderInbox.open(Folder.READ_ONLY);
            Message messages[] = folderInbox.getMessages();

            for (int i = 0; i < messages.length; i++) {
                Message message = messages[i];
                Address[] fromAddress = message.getFrom();
                from = fromAddress[0].toString();
                subject = message.getSubject();
                sentDate = message.getSentDate().toString();
                contentType = message.getContentType();

                String content = message.getContent().toString();  

                msg = msg + from + subject + sentDate + contentType + " %%%%% ";
            }
            folderInbox.close(false);
            store.close();
            ((TextView)findViewById(R.id.MsgContent)).setText(msg);
        }catch (Exception e) {
            StringWriter sw = new StringWriter();
            e.printStackTrace(new PrintWriter(sw));
            String trace = sw.toString();
            ((TextView)findViewById(R.id.MsgContent)).setText(trace);
        }
    }
}

运行 时,应用程序崩溃。 logcat 如下(当 GenyMotion 上的 运行 时):

07-02 00:08:29.031 2419-2419/myName.myAPP E/AndroidRuntime: FATAL EXCEPTION: main
Process: myName.myAPP, PID: 2419
java.lang.NoClassDefFoundError: javax.mail.internet.MimeBodyPart$MimePartDataHandler
at javax.mail.internet.MimeMessage.getDataHandler(MimeMessage.java:1475)
at javax.mail.internet.MimeMessage.getContent(MimeMessage.java:1508)
at myName.myAPP.MainActivity.onCreate(MainActivity.java:69)
at android.app.Activity.performCreate(Activity.java:5990)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
at android.app.ActivityThread.access0(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
07-02 00:08:35.380 2419-2431/myName.myAPP E/System: Uncaught exception thrown by finalizer
07-02 00:08:35.381 2419-2431/myName.myAPP E/System: java.lang.NullPointerException: Attempt to invoke virtual method 'void java.net.Socket.close()' on a null object reference
at com.sun.mail.pop3.Protocol.close(Protocol.java:382)
at com.sun.mail.pop3.POP3Folder.close(POP3Folder.java:296)
at com.sun.mail.pop3.POP3Folder.finalize(POP3Folder.java:576)
at java.lang.Daemons$FinalizerDaemon.doFinalize(Daemons.java:191)
at java.lang.Daemons$FinalizerDaemon.run(Daemons.java:174)
at java.lang.Thread.run(Thread.java:818)
[ 07-02 00:08:36.272    81:   81 D/         ]
Socket deconnection
[ 07-02 00:08:38.284    81:   81 D/         ]
Socket deconnection
[ 07-02 00:08:40.297    81:   81 D/         ]
Socket deconnection

我查过了。 javax.mail.internet.MimeBodyPart$MimePartDataHandler 在 javax.mail.jar

我迷路了。 需要帮助才能继续。

希望我的英语比 java 好。

天堂存在。

为了解决问题,"javax.mail.jar"不能编译,但是PROVIDED.
Build.gradle(module:app) 如图:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 24
    buildToolsVersion "24.0.2"
    packagingOptions {
        pickFirst 'META-INF/LICENSE.txt' // picks the JavaMail license file
    }

    defaultConfig {
        applicationId "myNam.myAPP"
        minSdkVersion 18
        targetSdkVersion 24
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

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

dependencies {
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:24.2.1'
    compile 'com.sun.mail:android-mail:1.6.2'
    compile 'com.sun.mail:android-activation:1.6.2'
    provided files('libs/javax.mail.jar')
}

重要的几行是:

    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.6.2'
    compile 'com.sun.mail:android-activation:1.6.2'
    provided files('libs/javax.mail.jar')
}

这样就完美了。
绝望哭泣的一周

非常感谢。