Implemented configuring connect timeout

This commit is contained in:
dankito 2024-11-26 06:10:01 +01:00
parent 2b08db3374
commit a078b8bf66
3 changed files with 14 additions and 10 deletions

View File

@ -43,7 +43,7 @@ open class EmailsFetcher(
open fun listenForNewEmails(account: EmailAccount, options: ListenForNewMailsOptions) = runBlocking { open fun listenForNewEmails(account: EmailAccount, options: ListenForNewMailsOptions) = runBlocking {
try { try {
connect(account) { store -> connect(account, options) { store ->
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)
@ -89,7 +89,7 @@ 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) { store -> return connect(account, options) { store ->
val inbox = store.getFolder(options.emailFolderName) val inbox = store.getFolder(options.emailFolderName)
inbox.open(Folder.READ_ONLY) inbox.open(Folder.READ_ONLY)
@ -274,8 +274,8 @@ open class EmailsFetcher(
date.toInstant() date.toInstant()
protected open fun <T> connect(account: EmailAccount, connected: (Store) -> T): T { protected open fun <T> connect(account: EmailAccount, options: FetchEmailsOptions, connected: (Store) -> T): T {
val properties = mapAccountToJavaMailProperties(account) val properties = mapAccountToJavaMailProperties(account, options)
val session = Session.getInstance(properties) val session = Session.getInstance(properties)
session.getStore("imap").use { store -> session.getStore("imap").use { store ->
@ -285,15 +285,16 @@ open class EmailsFetcher(
} }
} }
protected open fun mapAccountToJavaMailProperties(account: EmailAccount) = Properties().apply { protected open fun mapAccountToJavaMailProperties(account: EmailAccount, options: FetchEmailsOptions) = Properties().apply {
put("mail.store.protocol", "imap") put("mail.store.protocol", "imap")
put("mail.imap.host", account.serverAddress) put("mail.imap.host", account.serverAddress)
put("mail.imap.port", account.port?.toString() ?: "993") // Default IMAP over SSL put("mail.imap.port", account.port?.toString() ?: "993") // Default IMAP over SSL
put("mail.imap.ssl.enable", "true") put("mail.imap.ssl.enable", "true")
put("mail.imap.connectiontimeout", "10000") val timeout = (options.connectTimeoutSeconds * 1000).toString()
put("mail.imap.timeout", "10000") put("mail.imap.connectiontimeout", timeout)
put("mail.imap.timeout", timeout)
} }
} }

View File

@ -2,7 +2,6 @@ package net.codinux.invoicing.email
import net.codinux.invoicing.email.model.Email import net.codinux.invoicing.email.model.Email
import java.io.File import java.io.File
import java.nio.file.Files
open class FetchEmailsOptions( open class FetchEmailsOptions(
val downloadMessageBody: Boolean = false, val downloadMessageBody: Boolean = false,
@ -11,7 +10,9 @@ open class FetchEmailsOptions(
*/ */
val downloadAttachmentsWithExtensions: List<String> = DefaultDownloadedAttachmentsWithExtensions, val downloadAttachmentsWithExtensions: List<String> = DefaultDownloadedAttachmentsWithExtensions,
val attachmentsDownloadDirectory: File = DefaultAttachmentsDownloadDirectory, val attachmentsDownloadDirectory: File = DefaultAttachmentsDownloadDirectory,
val emailFolderName: String = "INBOX", val emailFolderName: String = "INBOX",
val connectTimeoutSeconds: Int = 5,
val onError: ((FetchEmailsError) -> Unit)? = null, val onError: ((FetchEmailsError) -> Unit)? = null,
val onEmailReceived: ((Email) -> Unit)? = null val onEmailReceived: ((Email) -> Unit)? = null
@ -21,7 +22,7 @@ open class FetchEmailsOptions(
val DefaultAttachmentsDownloadDirectory: File = File(System.getProperty("java.io.tmpdir"), "eInvoices").also { it.mkdirs() } val DefaultAttachmentsDownloadDirectory: File = File(System.getProperty("java.io.tmpdir"), "eInvoices").also { it.mkdirs() }
val IllegalFileCharacters = listOf('\\', '/', ':', '*', '?', '"', '<', '>', '|', '\u0000') val IllegalFileCharacters = listOf('\\', '/', ':', '*', '?', '"', '<', '>', '|', '\u0000') // TODO: make generic for attachment filename
} }
fun emailReceived(email: Email) { fun emailReceived(email: Email) {

View File

@ -10,8 +10,10 @@ open class ListenForNewMailsOptions(
downloadMessageBody: Boolean = false, downloadMessageBody: Boolean = false,
downloadAttachmentsWithExtensions: List<String> = DefaultDownloadedAttachmentsWithExtensions, downloadAttachmentsWithExtensions: List<String> = DefaultDownloadedAttachmentsWithExtensions,
attachmentsDownloadDirectory: File = DefaultAttachmentsDownloadDirectory, attachmentsDownloadDirectory: File = DefaultAttachmentsDownloadDirectory,
emailFolderName: String = "INBOX", emailFolderName: String = "INBOX",
connectTimeoutSeconds: Int = 5,
onError: ((FetchEmailsError) -> Unit)? = null, onError: ((FetchEmailsError) -> Unit)? = null,
onEmailReceived: (Email) -> Unit onEmailReceived: (Email) -> Unit
) : FetchEmailsOptions(downloadMessageBody, downloadAttachmentsWithExtensions, attachmentsDownloadDirectory, emailFolderName, onError, onEmailReceived) ) : FetchEmailsOptions(downloadMessageBody, downloadAttachmentsWithExtensions, attachmentsDownloadDirectory, emailFolderName, connectTimeoutSeconds, onError, onEmailReceived)