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

时间:2019-07-02 18:26:38

标签: java android email javamail

我正在尝试使应用程序“简单地”阅读纯文本电子邮件(仅纯文本。我发送电子邮件,所以我知道它只是纯文本)。
我在这里和那里拾取代码。
我最终成功地阅读了“从”,“主题”,“ sentDate”和其他内容,但没有阅读“内容”。这是我要阅读的内容...
一切正常,但是当我调用 getContent()时,出现了致命错误。

我在app \ libs中使用javax.mail.jar = javamail 1.6.2。
我已经尝试过使用mail.jar和Activation.jar,但是它不起作用。
我试图将所有读取的代码放入“空”中,并且不起作用。

即使在输入时

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);
        }
    }
}

运行时,应用程序崩溃。 日志记录如下(在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.access$800(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

我已经检查过了。 j avax.mail.internet.MimeBodyPart$MimePartDataHandler在javax.mail.jar

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

希望我的英语比我的Java强。

1 个答案:

答案 0 :(得分:0)

天堂存在。

要解决此问题,不得编译“ javax.mail.jar”,但 已提供
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')
}

有了它,它就可以很好地工作了。
一周的绝望和哭泣。

非常感谢。