Skip to content
This repository was archived by the owner on May 25, 2022. It is now read-only.

Commit 8a740a2

Browse files
committed
Add MailerManager object
1 parent 2d664d8 commit 8a740a2

File tree

2 files changed

+23
-8
lines changed

2 files changed

+23
-8
lines changed

simplekotlinmail-client/src/jvmMain/kotlin/net/axay/simplekotlinmail/delivery/EmailDelivery.kt

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,14 @@ import kotlinx.coroutines.*
44
import org.simplejavamail.api.email.Email
55
import org.simplejavamail.api.mailer.Mailer
66

7-
/**
8-
* The default mailer instance, that is used if
9-
* the no other instance is provided.
10-
*/
11-
val DEFAULT_MAILER = mailerBuilder("localhost", 25)
12-
137
/**
148
* Send this email.
159
* @param mailer the mailer which should be used to deliver the message
1610
* @param onException optional callback which should be executed if an error occurs
1711
* @param onSuccess optional callback which should be executed if the message was delivered successfully
1812
*/
1913
suspend fun Email.send(
20-
mailer: Mailer = DEFAULT_MAILER,
14+
mailer: Mailer = MailerManager.defaultMailer,
2115
onException: suspend (Exception) -> Unit = {},
2216
onSuccess: suspend () -> Unit = {}
2317
) = withContext(Dispatchers.IO) {
@@ -33,4 +27,4 @@ suspend fun Email.send(
3327
* Send this email synchronously.
3428
* @param mailer the mailer which should be used to deliver the message
3529
*/
36-
fun Email.sendSync(mailer: Mailer = DEFAULT_MAILER) = mailer.sendMail(this)
30+
fun Email.sendSync(mailer: Mailer = MailerManager.defaultMailer) = mailer.sendMail(this)
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package net.axay.simplekotlinmail.delivery
2+
3+
import org.simplejavamail.api.mailer.Mailer
4+
5+
object MailerManager {
6+
7+
private val LOCAL_MAILER by lazy { mailerBuilder("localhost", 25) }
8+
9+
private var DEFAULT_MAILER: Mailer? = null
10+
11+
/**
12+
* The default mailer instance, that is used if
13+
* the no other instance is provided.
14+
*/
15+
var defaultMailer: Mailer
16+
get() = DEFAULT_MAILER ?: LOCAL_MAILER
17+
set(value) {
18+
DEFAULT_MAILER = value
19+
}
20+
21+
}

0 commit comments

Comments
 (0)