Android Studio VerifyError拒绝JavaMail API中的class text_plain

问题描述

我正在开发一个允许用户通过发送电子邮件与我联系的应用程序(用户仅输入消息,发送者和接收者的电子邮件都是我的)。我正在尝试使用JavaMail API通过gmail实现此功能。但是,我在Transport.send(mimeMessage)行中不断收到此错误

以下是错误消息:

原因:java.lang.VerifyError:拒绝尝试对错误的com.sun.mail.handlers.handler_base类进行子类型化的类com.sun.mail.handlers.text_plain(声明为“ com.sun.mail。 handlers.text_plain'出现在/ data / app / ~~ T_TRkO9R_v9j4iEdr4K9Yg == / com.example.compusec-dPeAL8DtGJvU45dJpt8xxA == / base.apk)

原因:java.lang.VerifyError:验证者拒绝类com.sun.mail.handlers.handler_base:java.awt.datatransfer.DataFlavor [] com.sun.mail.handlers.handler_base.getTransferDataFlavors()无法验证:java.awt.datatransfer.DataFlavor [] com.sun.mail.handlers.handler_base.getTransferDataFlavors():[0x4]无法解析返回的类型“未解析的参考:java.awt.datatransfer.DataFlavor []”或“参考” :javax.activation.ActivationDataFlavor []”(“ com.sun.mail.handlers.handler_base”的声明出现在/data/app/~~T_TRkO9R_v9j4iEdr4K9Yg==/com.example.compusec-dPeAL8DtGJvU45dJpt8apA=/

这是我的代码

    protected Void doInBackground(Void... voids) {
        Properties properties = new Properties();
        properties.put("mail.smtp.host","smtp.gmail.com");
        properties.put("mail.smtp.socketFactory.port","465");
        properties.put("mail.smtp.socketFactory.class","javax.net.ssl.SSLSocketFactory");
        properties.put("mail.smtp.auth","true");
        properties.put("mail.smtp.port","465");

        session = javax.mail.Session.getInstance(properties,new javax.mail.Authenticator(){
            protected PasswordAuthentication getpasswordAuthentication(){
                return new PasswordAuthentication("[email protected]","senderpass");
            }
        });

        session.setDebug(true);

        MimeMessage mimeMessage = new MimeMessage(session);
        try {
            mimeMessage.setFrom(new InternetAddress("[email protected]"));
            mimeMessage.addRecipients(Message.RecipientType.TO,String.valueOf(new InternetAddress(email)));
            mimeMessage.setSubject(subject);
            mimeMessage.setText(message);
            Transport.send(mimeMessage);
        } catch (MessagingException e) {
            e.printstacktrace();
        }

        return null;
    }

和gradle脚本:

plugins {
    id 'com.android.application'
}

android {
    compileSdkVersion 30
    buildToolsversion "30.0.2"

    packagingOptions {
        pickFirst 'meta-inf/LICENSE.txt' // picks the JavaMail license file
    }

    defaultConfig {
        applicationId "com.example.myapplication"
        minSdkVersion 23
        targetSdkVersion 30
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'),'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
}

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

dependencies {

    implementation 'androidx.appcompat:appcompat:1.1.0'
    implementation 'com.google.android.material:material:1.1.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    compile 'com.sun.mail:android-mail:1.6.2'
    compile 'com.sun.mail:android-activation:1.6.2'
    testImplementation 'junit:junit:4.+'
    androidTestImplementation 'androidx.test.ext:junit:1.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
}

解决方法

我遇到了同样的问题。我可以告诉您的是,如果您以Android 10及更低版本(SDK 29或更低版本)为目标,则可以正常运行。我已经使用多年了。但是,一旦我定位到Android 11(SDK 30),我就会收到此错误。我的猜测是com.sun.mail依赖项需要更新。我正在使用最新的1.6.5,但仍然无法使用。因此,我建议暂时定位SDK 29,看看是否有帮助。

,

build.gradle 中使用:

android {
    ...
    packagingOptions {
        exclude 'META-INF/NOTICE.md'
        exclude 'META-INF/LICENSE.md'
    }
}

dependencies {
    implementation 'com.sun.mail:android-mail:1.6.6'
    implementation 'com.sun.mail:android-activation:1.6.6'
    ...
}

感谢关于 this 问题的最后几条评论