Android上的JavaMail API

kd3sttzy  于 2023-02-27  发布在  Android
关注(0)|答案(2)|浏览(161)

我正在尝试使用JavaMail API在Android上发送电子邮件,这只是一个简单的测试
但每当我尝试启动应用程序时,就会弹出此异常

2020-09-23 13:23:42.442 21860-21971/ae.ucg.mailtest E/AndroidRuntime: FATAL EXCEPTION: DefaultDispatcher-worker-2
    Process: ae.ucg.mailtest, PID: 21860
    java.lang.VerifyError: Rejecting class com.sun.mail.handlers.text_plain that attempts to sub-type erroneous class com.sun.mail.handlers.handler_base (declaration of 'com.sun.mail.handlers.text_plain' appears in /data/app/~~nyiEJPyVs6-ADHqg-ebklA==/ae.ucg.mailtest-_Q4dGJfXP_9F5CZNfhDVtQ==/base.apk)
        at java.lang.Class.newInstance(Native Method)
        at javax.activation.MailcapCommandMap.getDataContentHandler(MailcapCommandMap.java:601)
        at javax.activation.MailcapCommandMap.createDataContentHandler(MailcapCommandMap.java:555)
        at javax.activation.DataHandler.getDataContentHandler(DataHandler.java:597)
        at javax.activation.DataHandler.writeTo(DataHandler.java:299)
        at javax.mail.internet.MimeUtility.getEncoding(MimeUtility.java:316)
        at javax.mail.internet.MimeBodyPart.updateHeaders(MimeBodyPart.java:1551)
        at javax.mail.internet.MimeMessage.updateHeaders(MimeMessage.java:2238)
        at javax.mail.internet.MimeMessage.saveChanges(MimeMessage.java:2198)
        at javax.mail.Transport.send(Transport.java:99)
        at ae.ucg.mailtest.MainActivity$onCreate$1.invokeSuspend(MainActivity.kt:38)
        at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
        at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:56)
        at kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely(CoroutineScheduler.kt:571)
        at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.executeTask(CoroutineScheduler.kt:738)
        at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.runWorker(CoroutineScheduler.kt:678)
        at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run(CoroutineScheduler.kt:665)
     Caused by: java.lang.VerifyError: Verifier rejected class com.sun.mail.handlers.handler_base: java.awt.datatransfer.DataFlavor[] com.sun.mail.handlers.handler_base.getTransferDataFlavors() failed to verify: java.awt.datatransfer.DataFlavor[] com.sun.mail.handlers.handler_base.getTransferDataFlavors(): [0x4]  can't resolve returned type 'Unresolved Reference: java.awt.datatransfer.DataFlavor[]' or 'Reference: javax.activation.ActivationDataFlavor[]' (declaration of 'com.sun.mail.handlers.handler_base' appears in /data/app/~~nyiEJPyVs6-ADHqg-ebklA==/ae.ucg.mailtest-_Q4dGJfXP_9F5CZNfhDVtQ==/base.apk)
        at java.lang.Class.newInstance(Native Method) 
        at javax.activation.MailcapCommandMap.getDataContentHandler(MailcapCommandMap.java:601) 
        at javax.activation.MailcapCommandMap.createDataContentHandler(MailcapCommandMap.java:555) 
        at javax.activation.DataHandler.getDataContentHandler(DataHandler.java:597) 
        at javax.activation.DataHandler.writeTo(DataHandler.java:299) 
        at javax.mail.internet.MimeUtility.getEncoding(MimeUtility.java:316) 
        at javax.mail.internet.MimeBodyPart.updateHeaders(MimeBodyPart.java:1551) 
        at javax.mail.internet.MimeMessage.updateHeaders(MimeMessage.java:2238) 
        at javax.mail.internet.MimeMessage.saveChanges(MimeMessage.java:2198) 
        at javax.mail.Transport.send(Transport.java:99) 
        at ae.ucg.mailtest.MainActivity$onCreate$1.invokeSuspend(MainActivity.kt:38) 
        at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33) 
        at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:56) 
        at kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely(CoroutineScheduler.kt:571) 
        at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.executeTask(CoroutineScheduler.kt:738) 
        at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.runWorker(CoroutineScheduler.kt:678) 
        at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run(CoroutineScheduler.kt:665) 

我知道图书馆本身出了问题,但我到底该怎么办?
这是我的密码

override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        lifecycleScope.launch(Dispatchers.IO) {
            try {
                val props = Properties().apply {
                    this["mail.smtp.host"] = "smtp.gmail.com"
                    this["mail.smtp.socketFactory.port"] = "465"
                    this["mail.smtp.socketFactory.class"] = "javax.net.ssl.SSLSocketFactory"
                    this["mail.smtp.auth"] = "true"
                    this["mail.smtp.port"] = "465"
                }

                val session = Session.getDefaultInstance(props, object : Authenticator() {
                    override fun getPasswordAuthentication() = PasswordAuthentication(Config.EMAIL, Config.PASSWORD)
                })

                val message = MimeMessage(session).apply {
                    setFrom(InternetAddress(Config.EMAIL))
                    addRecipient(Message.RecipientType.TO, InternetAddress("..."))
                    subject = "Hello world"
                    setText("This is a hello world message", "UTF-8")
                }
                Transport.send(message)
            } catch (e: Exception) {
                Toast.makeText(this@MainActivity, e.message, Toast.LENGTH_LONG).show()
            }
        }
    }

这是我正在使用的图书馆

implementation 'com.sun.mail:android-mail:1.6.5'
implementation 'com.sun.mail:android-activation:1.6.5'
jv2fixgn

jv2fixgn1#

显然,这个bug只在SDK级别30(Android 11),但在这里我找到了一个解决方案:
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'
    ...
}

1.在打包中排除这些文件选项:

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

(资料来源)

azpvetkf

azpvetkf2#

我遇到了同样的问题,有几件事要注意,以解决它:
1.正如其他人(raddathis answer)所提到的,有一个Google Issue Tracker Issue涵盖并提供了发生此问题的原因:
API级别30添加了一个新的(验证器)特性,用于验证所有引用的代码是否确实存在--或者构建是否失败。
1.即使在修复问题时--通过引用并确保提供引用的代码 (例如,通过更新依赖项到最新版本--目前是1.6.7;您会发现构建仍然会抛出一个相对直接的抱怨:

要解决(并获得最新):
1.如果您是通过/app/libs/mail.jar/app/libs/activation.jar/app/libs/additionnal.jar手动提供这些库-您可以继续并删除它们;google()maven()存储库在Android项目中均已启用-因此只需添加;

  • 添加到项目级Gradle文件(/build.gradle):*
buildscript {
    ext {
        // ...
        java_mail_version = "1.6.7"
    }
    // ...
}

// ...
  • 添加到您的应用/模块级Gradle文件(/app/build.gradle):*
//...
dependencies {
    // ...

    // Dumb Email Sending
    implementation "com.sun.mail:android-mail:$java_mail_version"

    // REMOVE THESE - DEPS WILL DOWNLOAD AUTOMATICALLY
    //implementation 'com.sun.mail:android-activation:1.6.7' // from 1.6.4
    //implementation 'com.sun.mail:android-additionnal:1.6.7' // from 1.6.4
}

// ...

...这样,您就一劳永逸地解决了 “最新版本” 问题 (包括在何处查找)
要解决问题#2,请添加:

  • 添加到同一个应用程序/模块级别的gradle文件(/app/build.gradle):*
// ...
android {
    // ...
    packagingOptions {
        resources {
            excludes += '/META-INF/{AL2.0,LGPL2.1}'
            excludes += '/META-INF/{NOTICE.md,LICENSE.md}' // <-- This line
        }
    }
}

// ...

这为我解决了这个问题,我很高兴不必再手动拖动.jar文件了:

相关问题