Android Studio VerifyError 从 JavaMail API 拒绝类 text_plain

Mai*_*a B 1 java email android jakarta-mail verifyerror

我正在开发一个应用程序,允许用户通过发送电子邮件与我联系(用户只输入消息,发件人和收件人的电子邮件都是我的)。我正在尝试使用 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] 无法解析返回的类型 'Unresolved Reference: java.awt.datatransfer.DataFlavor[]' 或 'Reference: javax. activation.ActivationDataFlavor[]'('com.sun.mail.handlers.handler_base' 的声明出现在 /data/app/~~T_TRkO9R_v9j4iEdr4K9Yg==/com.example.compusec-dPeAL8DtGJvU45dJpt8xxA==/base.apk)

这是我的代码:

    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("sender@gmail.com","senderpass");
            }
        });

        session.setDebug(true);

        MimeMessage mimeMessage = new MimeMessage(session);
        try {
            mimeMessage.setFrom(new InternetAddress("sender@gmail.com"));
            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;
    }
Run Code Online (Sandbox Code Playgroud)

和 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'
}
Run Code Online (Sandbox Code Playgroud)

rad*_*dda 14

在你的app/build.gradle文件中你需要

  1. 升级到较新版本的邮件 API(至少 1.6.6):
dependencies {
    implementation 'com.sun.mail:android-mail:1.6.6'
    implementation 'com.sun.mail:android-activation:1.6.6'
    ...
}
Run Code Online (Sandbox Code Playgroud)
  1. 在打包选项中排除这些文件:
android {
    ...
    packagingOptions {
        resources {
            exclude 'META-INF/NOTICE.md'
            exclude 'META-INF/LICENSE.md'
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

来源

将 Gradle Kotlin DSL 与版本目录结合使用:

app/build.gradle.kts

dependencies {
    implementation 'com.sun.mail:android-mail:1.6.6'
    implementation 'com.sun.mail:android-activation:1.6.6'
    ...
}
Run Code Online (Sandbox Code Playgroud)

gradle/libs.versions.toml

android {
    ...
    packagingOptions {
        resources {
            exclude 'META-INF/NOTICE.md'
            exclude 'META-INF/LICENSE.md'
        }
    }
}
Run Code Online (Sandbox Code Playgroud)


gbo*_*tha 7

我有相同的问题。我可以告诉你的是,如果你的目标是 Android 10 及更低版本(SDK 29 或更低),它工作正常。我已经使用它多年了。但是,一旦我以 Android 11 (SDK 30) 为目标,就会收到此错误。我的猜测是 com.sun.mail 依赖项需要更新。我正在使用最新的 1.6.5,但仍然无法正常工作。所以我建议现在针对 SDK 29,看看是否有帮助。

  • 我两天前提交了一个 Android bug,并与 Google 进行了交谈。他们表示这是 Android 11 的预期行为,我们的代码需要进行一些修改,但他们还不太清楚该怎么做。您可以查看此处提交的错误问题 https://issuetracker.google.com/issues/172544275 并将其加注星标以表明您受到影响,还可以添加您自己的评论。 (2认同)