Implemented configuring connect timeout
This commit is contained in:
parent
2b08db3374
commit
a078b8bf66
|
@ -43,7 +43,7 @@ open class EmailsFetcher(
|
|||
|
||||
open fun listenForNewEmails(account: EmailAccount, options: ListenForNewMailsOptions) = runBlocking {
|
||||
try {
|
||||
connect(account) { store ->
|
||||
connect(account, options) { store ->
|
||||
val folder = store.getFolder(options.emailFolderName) as IMAPFolder
|
||||
folder.open(Folder.READ_ONLY)
|
||||
|
||||
|
@ -89,7 +89,7 @@ open class EmailsFetcher(
|
|||
|
||||
open fun fetchAllEmails(account: EmailAccount, options: FetchEmailsOptions = FetchEmailsOptions()): FetchEmailsResult {
|
||||
try {
|
||||
return connect(account) { store ->
|
||||
return connect(account, options) { store ->
|
||||
val inbox = store.getFolder(options.emailFolderName)
|
||||
inbox.open(Folder.READ_ONLY)
|
||||
|
||||
|
@ -274,8 +274,8 @@ open class EmailsFetcher(
|
|||
date.toInstant()
|
||||
|
||||
|
||||
protected open fun <T> connect(account: EmailAccount, connected: (Store) -> T): T {
|
||||
val properties = mapAccountToJavaMailProperties(account)
|
||||
protected open fun <T> connect(account: EmailAccount, options: FetchEmailsOptions, connected: (Store) -> T): T {
|
||||
val properties = mapAccountToJavaMailProperties(account, options)
|
||||
|
||||
val session = Session.getInstance(properties)
|
||||
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.imap.host", account.serverAddress)
|
||||
put("mail.imap.port", account.port?.toString() ?: "993") // Default IMAP over SSL
|
||||
put("mail.imap.ssl.enable", "true")
|
||||
|
||||
put("mail.imap.connectiontimeout", "10000")
|
||||
put("mail.imap.timeout", "10000")
|
||||
val timeout = (options.connectTimeoutSeconds * 1000).toString()
|
||||
put("mail.imap.connectiontimeout", timeout)
|
||||
put("mail.imap.timeout", timeout)
|
||||
}
|
||||
|
||||
}
|
|
@ -2,7 +2,6 @@ package net.codinux.invoicing.email
|
|||
|
||||
import net.codinux.invoicing.email.model.Email
|
||||
import java.io.File
|
||||
import java.nio.file.Files
|
||||
|
||||
open class FetchEmailsOptions(
|
||||
val downloadMessageBody: Boolean = false,
|
||||
|
@ -11,7 +10,9 @@ open class FetchEmailsOptions(
|
|||
*/
|
||||
val downloadAttachmentsWithExtensions: List<String> = DefaultDownloadedAttachmentsWithExtensions,
|
||||
val attachmentsDownloadDirectory: File = DefaultAttachmentsDownloadDirectory,
|
||||
|
||||
val emailFolderName: String = "INBOX",
|
||||
val connectTimeoutSeconds: Int = 5,
|
||||
|
||||
val onError: ((FetchEmailsError) -> 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 IllegalFileCharacters = listOf('\\', '/', ':', '*', '?', '"', '<', '>', '|', '\u0000')
|
||||
val IllegalFileCharacters = listOf('\\', '/', ':', '*', '?', '"', '<', '>', '|', '\u0000') // TODO: make generic for attachment filename
|
||||
}
|
||||
|
||||
fun emailReceived(email: Email) {
|
||||
|
|
|
@ -10,8 +10,10 @@ open class ListenForNewMailsOptions(
|
|||
downloadMessageBody: Boolean = false,
|
||||
downloadAttachmentsWithExtensions: List<String> = DefaultDownloadedAttachmentsWithExtensions,
|
||||
attachmentsDownloadDirectory: File = DefaultAttachmentsDownloadDirectory,
|
||||
|
||||
emailFolderName: String = "INBOX",
|
||||
connectTimeoutSeconds: Int = 5,
|
||||
|
||||
onError: ((FetchEmailsError) -> Unit)? = null,
|
||||
onEmailReceived: (Email) -> Unit
|
||||
) : FetchEmailsOptions(downloadMessageBody, downloadAttachmentsWithExtensions, attachmentsDownloadDirectory, emailFolderName, onError, onEmailReceived)
|
||||
) : FetchEmailsOptions(downloadMessageBody, downloadAttachmentsWithExtensions, attachmentsDownloadDirectory, emailFolderName, connectTimeoutSeconds, onError, onEmailReceived)
|
Loading…
Reference in New Issue