【发布时间】:2021-07-01 21:14:56
【问题描述】:
我正在使用 android javamail 库 1.6.2。我正在尝试阅读邮件并将其作为片段中的自定义对象列表返回,以在回收站视图中显示它们。我用来阅读邮件的代码是:
fun readMails(host: String, port: String,
username: String, password: String): List<Mail>? {
var folder: Folder? = null
var store: Store? = null
return try {
val properties = Properties()
properties[HOST] = host
properties[PORT] = port
properties[START_TLS] = "true"
val session = Session.getDefaultInstance(properties)
// Create IMAP store object and connect with the server
store = session.getStore(PROTOCOL)
store.connect(host, username, password)
// Create folder object and open it in read-only mode
folder = store.getFolder(FOLDER_TYPE)
folder.open(Folder.READ_ONLY)
// Fetch messages from the folder and print in a loop
val messages = folder.messages
val mails = messages.map {
Mail(
messageNumber = it.messageNumber,
subject = it.subject,
senders = it.from.toList().map { address ->
MailAddress(
type = address.type,
)
},
content = parseContent(it.content as Multipart)
)
}
Log.d(TAG, "readMails: $mails")
mails
} catch (e: NoSuchProviderException) {
Log.e(TAG, "NoSuchProviderException: ${e.localizedMessage}")
null
} catch (e: MessagingException) {
Log.e(TAG, "MessagingException: ${e.localizedMessage}")
null
} catch (e: Exception) {
Log.e(TAG, "Exception: ${e.localizedMessage}")
null
} finally {
folder?.close(false)
store?.close()
}
}
在片段中,我正在尝试使用以下方式阅读邮件:
viewLifecycleOwner.lifecycleScope.launch {
val emails = MailHelper.readMails(
host = "",
port = "",
username = "",
password = ""
)
mailAdapter.submitList(emails)
}
问题是我可以在控制台打印邮件,但我只能使用GlobalScope.launch {} 打印它们。如果我使用它,我无法在 recyclerview 中使用submitList() 显示适配器。如果我使用viewLifecycleOwner.lifecycleScope.launch {},我会不断收到android.os.NetworkOnMainThreadException。
【问题讨论】:
标签: android kotlin android-recyclerview jakarta-mail kotlin-coroutines