我正在尝试使用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'
2条答案
按热度按时间jv2fixgn1#
显然,这个bug只在SDK级别30(Android 11),但在这里我找到了一个解决方案:
在
app/build.gradle
文件中,您需要1.升级到邮件API的较新版本(至少1.6.6):
1.在打包中排除这些文件选项:
(资料来源)
azpvetkf2#
我遇到了同样的问题,有几件事要注意,以解决它:
1.正如其他人(radda和this 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项目中均已启用-因此只需添加;/build.gradle
):*/app/build.gradle
):*...这样,您就一劳永逸地解决了 “最新版本” 问题 (包括在何处查找)。
要解决问题#2,请添加:
/app/build.gradle
):*这为我解决了这个问题,我很高兴不必再手动拖动
.jar
文件了: