Using now stable message UID instead of unreliable messageNumber

This commit is contained in:
dankito 2024-11-26 21:22:18 +01:00
parent 1e986e800d
commit 3e401b1c00
3 changed files with 12 additions and 16 deletions

View File

@ -48,7 +48,7 @@ open class EmailsFetcher(
val folder = store.getFolder(options.emailFolderName) as IMAPFolder val folder = store.getFolder(options.emailFolderName) as IMAPFolder
folder.open(Folder.READ_ONLY) folder.open(Folder.READ_ONLY)
val status = FetchEmailsStatus(account, options) val status = FetchEmailsStatus(account, folder, options)
folder.addMessageCountListener(object : MessageCountAdapter() { folder.addMessageCountListener(object : MessageCountAdapter() {
override fun messagesAdded(event: MessageCountEvent) { override fun messagesAdded(event: MessageCountEvent) {
@ -91,13 +91,13 @@ open class EmailsFetcher(
open fun fetchAllEmails(account: EmailAccount, options: FetchEmailsOptions = FetchEmailsOptions()): FetchEmailsResult { open fun fetchAllEmails(account: EmailAccount, options: FetchEmailsOptions = FetchEmailsOptions()): FetchEmailsResult {
try { try {
return connect(account, options) { store -> return connect(account, options) { store ->
val inbox = store.getFolder(options.emailFolderName) val folder = store.getFolder(options.emailFolderName) as IMAPFolder
inbox.open(Folder.READ_ONLY) folder.open(Folder.READ_ONLY)
val status = FetchEmailsStatus(account, options) val status = FetchEmailsStatus(account, folder, options)
val emails = fetchAllEmailsInFolder(inbox, status).also { val emails = fetchAllEmailsInFolder(status).also {
inbox.close(false) folder.close(false)
} }
FetchEmailsResult(emails, null, status.messageSpecificErrors) FetchEmailsResult(emails, null, status.messageSpecificErrors)
@ -109,7 +109,8 @@ open class EmailsFetcher(
} }
} }
protected open fun fetchAllEmailsInFolder(folder: Folder, status: FetchEmailsStatus): List<Email> = runBlocking { protected open fun fetchAllEmailsInFolder(status: FetchEmailsStatus): List<Email> = runBlocking {
val folder = status.folder
val messageCount = folder.messageCount val messageCount = folder.messageCount
if (messageCount <= 0) { if (messageCount <= 0) {
return@runBlocking emptyList() return@runBlocking emptyList()
@ -141,7 +142,7 @@ open class EmailsFetcher(
val email = Email( val email = Email(
message.from?.joinToString(), message.subject ?: "", message.from?.joinToString(), message.subject ?: "",
message.sentDate?.let { map(it) }, map(message.receivedDate), message.messageNumber, message.sentDate?.let { map(it) }, map(message.receivedDate), status.folder.getUID(message),
parts.any { it.mediaType == "application/pgp-encrypted" }, parts.any { it.mediaType == "application/pgp-encrypted" },
getPlainTextBody(messageBodyParts, status), getHtmlBody(messageBodyParts, status), getPlainTextBody(messageBodyParts, status), getHtmlBody(messageBodyParts, status),
attachments attachments

View File

@ -5,10 +5,12 @@ import jakarta.mail.Message
import jakarta.mail.Part import jakarta.mail.Part
import net.codinux.invoicing.email.model.EmailAccount import net.codinux.invoicing.email.model.EmailAccount
import net.codinux.invoicing.filesystem.FileUtil import net.codinux.invoicing.filesystem.FileUtil
import org.eclipse.angus.mail.imap.IMAPFolder
import java.io.File import java.io.File
data class FetchEmailsStatus( data class FetchEmailsStatus(
val account: EmailAccount, val account: EmailAccount,
val folder: IMAPFolder,
val options: FetchEmailsOptions, val options: FetchEmailsOptions,
val messageSpecificErrors: MutableList<FetchEmailsError> = mutableListOf() val messageSpecificErrors: MutableList<FetchEmailsError> = mutableListOf()
) { ) {

View File

@ -8,14 +8,7 @@ class Email(
val subject: String, val subject: String,
val sent: Instant?, val sent: Instant?,
val received: Instant, val received: Instant,
/** val messageId: Long,
* From documentation of underlying mail library:
* "Since message numbers can change within a session if the folder is expunged, clients are advised not to use
* message numbers as references to messages."
*
* -> use with care. Message numbers are not valid / the same anymore after expunging.
*/
val messageNumber: Int,
val isEncrypted: Boolean = false, val isEncrypted: Boolean = false,
val plainTextBody: String?, val plainTextBody: String?,
val htmlBody: String?, val htmlBody: String?,