diff --git a/e-invoice-domain/src/main/kotlin/net/codinux/invoicing/email/EmailsFetcher.kt b/e-invoice-domain/src/main/kotlin/net/codinux/invoicing/email/EmailsFetcher.kt index 7072fa1..600d3b2 100644 --- a/e-invoice-domain/src/main/kotlin/net/codinux/invoicing/email/EmailsFetcher.kt +++ b/e-invoice-domain/src/main/kotlin/net/codinux/invoicing/email/EmailsFetcher.kt @@ -48,7 +48,7 @@ open class EmailsFetcher( val folder = store.getFolder(options.emailFolderName) as IMAPFolder folder.open(Folder.READ_ONLY) - val status = FetchEmailsStatus(account, options) + val status = FetchEmailsStatus(account, folder, options) folder.addMessageCountListener(object : MessageCountAdapter() { override fun messagesAdded(event: MessageCountEvent) { @@ -91,13 +91,13 @@ open class EmailsFetcher( open fun fetchAllEmails(account: EmailAccount, options: FetchEmailsOptions = FetchEmailsOptions()): FetchEmailsResult { try { return connect(account, options) { store -> - val inbox = store.getFolder(options.emailFolderName) - inbox.open(Folder.READ_ONLY) + val folder = store.getFolder(options.emailFolderName) as IMAPFolder + folder.open(Folder.READ_ONLY) - val status = FetchEmailsStatus(account, options) + val status = FetchEmailsStatus(account, folder, options) - val emails = fetchAllEmailsInFolder(inbox, status).also { - inbox.close(false) + val emails = fetchAllEmailsInFolder(status).also { + folder.close(false) } FetchEmailsResult(emails, null, status.messageSpecificErrors) @@ -109,7 +109,8 @@ open class EmailsFetcher( } } - protected open fun fetchAllEmailsInFolder(folder: Folder, status: FetchEmailsStatus): List = runBlocking { + protected open fun fetchAllEmailsInFolder(status: FetchEmailsStatus): List = runBlocking { + val folder = status.folder val messageCount = folder.messageCount if (messageCount <= 0) { return@runBlocking emptyList() @@ -141,7 +142,7 @@ open class EmailsFetcher( val email = Email( 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" }, getPlainTextBody(messageBodyParts, status), getHtmlBody(messageBodyParts, status), attachments diff --git a/e-invoice-domain/src/main/kotlin/net/codinux/invoicing/email/FetchEmailsStatus.kt b/e-invoice-domain/src/main/kotlin/net/codinux/invoicing/email/FetchEmailsStatus.kt index 9b3c1f0..67f81e3 100644 --- a/e-invoice-domain/src/main/kotlin/net/codinux/invoicing/email/FetchEmailsStatus.kt +++ b/e-invoice-domain/src/main/kotlin/net/codinux/invoicing/email/FetchEmailsStatus.kt @@ -5,10 +5,12 @@ import jakarta.mail.Message import jakarta.mail.Part import net.codinux.invoicing.email.model.EmailAccount import net.codinux.invoicing.filesystem.FileUtil +import org.eclipse.angus.mail.imap.IMAPFolder import java.io.File data class FetchEmailsStatus( val account: EmailAccount, + val folder: IMAPFolder, val options: FetchEmailsOptions, val messageSpecificErrors: MutableList = mutableListOf() ) { diff --git a/e-invoice-domain/src/main/kotlin/net/codinux/invoicing/email/model/Email.kt b/e-invoice-domain/src/main/kotlin/net/codinux/invoicing/email/model/Email.kt index 9d531ce..420fba2 100644 --- a/e-invoice-domain/src/main/kotlin/net/codinux/invoicing/email/model/Email.kt +++ b/e-invoice-domain/src/main/kotlin/net/codinux/invoicing/email/model/Email.kt @@ -8,14 +8,7 @@ class Email( val subject: String, val sent: Instant?, val received: Instant, - /** - * 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 messageId: Long, val isEncrypted: Boolean = false, val plainTextBody: String?, val htmlBody: String?,