微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

java – Android Studio:NoClassDefFoundError

我正在尝试构建一个使用javamail和gmail的smtp服务发送电子邮件的应用程序,但是当我运行它时,它会在调用Session.getInstance时崩溃.调试后,看起来它是关于com.sun.mail.util.MailLogger的NoClassDefFoundError.我读了elsewhere,我必须添加一个较旧的邮件包才能得到它,但我仍然会收到错误.

这是我在Android Studio中的内容

 // Get system properties
    Properties properties = System.getProperties();

    // Setup mail server
    properties.put("mail.smtp.host", host);
    properties.put("mail.smtp.auth", "true");
    properties.put("mail.smtp.starttls.enable", "true");
    properties.put("mail.transport.protocol","smtp");
    properties.put("mail.smtp.port", "587");

    authenticator = getAuthenticator();
    /* What getAuthenticator looks like:
    return new javax.mail.Authenticator() {
        protected PasswordAuthentication getpasswordAuthentication() {
            return new PasswordAuthentication("[email protected] ", "password");
        }
    };
    */

    // Get the default Session object, with authentication
    try {
        session = Session.getInstance(properties, authenticator);
    } catch (Error e) {
        // Unnecessary code for debugging
        int hold = 0;
    }

    // Create a default MimeMessage object.
    MimeMessage message = new MimeMessage(session);

    try {
        // Set From: header field of the header.
        message.setFrom(new InternetAddress(from));

        // Set To: header field of the header.
        message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));

        // Set Subject: header field
        message.setSubject(subject);

        // Now set the actual message
        message.setText(content);

        // Send message
        Transport.send(message);
    } catch (AddressException e) {
        throw new Error("bad address");
    } catch (MessagingException e){
        throw new Error("bad message data");
    }

我的构建看起来像这样:

apply plugin: 'com.android.application'
android {
     compileSdkVersion 22
     buildToolsversion "22.0.1"

     defaultConfig {
         applicationId "com.bsitpa.myfirstapp"
         minSdkVersion 15
         targetSdkVersion 22
         versionCode 1
         versionName "1.0"
     }
     buildTypes {
         release {
             minifyEnabled false
             proguardFiles getDefaultProguardFile('proguard-android.txt'),    'proguard-rules.pro'
         }
     }
     packagingOptions {
        exclude 'meta-inf/LICENSE.txt'
     }
}

dependencies {
    compile filetree(include: ['*.jar'], dir: 'libs')
    compile 'com.android.support:appcompat-v7:22.2.1'
    compile 'javax.mail:javax.mail-api:1.5.3'
    //compile 'javax.mail:mail:1.5.0-b01'
    compile files('mail-1.5.0-b01.jar')
}

调试错误
java.lang.NoClassDefFoundError:Lcom / sun / mail / util / MailLogger的解析失败”

而且你知道,我对AndroidStudio和Gradle完全不熟悉.

万分感谢,
瑞安

解决方法:

不确定你是否已经解决了这个问题,但我发现你需要使用特定于Android的Java Mail库.

compile 'com.sun.mail:android-mail:1.5.5'
compile 'com.sun.mail:android-activation:1.5.5'

我使用最新的Java Mail库遇到了您的错误和其他类似的错误,但切换到这些修复了所有这些.

https://java.net/projects/javamail/pages/Android

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐