Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ repositories {
}

// Append dependency
implementation("com.icerockdev.service:email-service:0.3.0")
implementation("com.icerockdev.service:email-service:0.4.0")
````

## Library usage
Expand Down Expand Up @@ -38,6 +38,12 @@ implementation("com.icerockdev.service:email-service:0.3.0")
subject = "TEST EMAIL"
to = mutableMapOf("[email protected]" to "Test Person")
html = "<h1>Test test test</h1>"
attachments = listOf(
Mail.Attachment(
file = File("test.pdf"),
name = "test.pdf"
)
)
}.sendAsync()
````

Expand Down
2 changes: 1 addition & 1 deletion email-service/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ apply(plugin = "java")
apply(plugin = "kotlin")

group = "com.icerockdev.service"
version = "0.3.0"
version = "0.4.0"

val sourcesJar by tasks.registering(Jar::class) {
archiveClassifier.set("sources")
Expand Down
35 changes: 30 additions & 5 deletions email-service/src/main/kotlin/com/icerockdev/service/email/Mail.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,16 @@ import kotlinx.coroutines.Job
import kotlinx.coroutines.launch
import org.apache.commons.mail.DefaultAuthenticator
import org.apache.commons.mail.Email
import org.apache.commons.mail.EmailConstants.UTF_8
import org.apache.commons.mail.HtmlEmail
import org.apache.commons.mail.EmailConstants.*
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import java.io.File
import java.net.URL
import javax.activation.DataSource
import javax.activation.FileDataSource
import javax.activation.URLDataSource
import javax.mail.util.ByteArrayDataSource

class Mail(private val coroutineScope: CoroutineScope?, private val config: SMTPConfig) {

Expand All @@ -31,6 +37,7 @@ class Mail(private val coroutineScope: CoroutineScope?, private val config: SMTP
var fromName: String = ""
var fromEmail: String = ""
var charset = UTF_8
var attachments: List<Attachment> = emptyList()
private var isHtml: Boolean = false

private fun prepareEmail(): Email {
Expand Down Expand Up @@ -64,14 +71,16 @@ class Mail(private val coroutineScope: CoroutineScope?, private val config: SMTP
email.addTo(entry.key, entry.value)
}

email.setCharset(charset);
email.setCharset(charset)
attachments.forEach { attachment ->
email.attach(attachment.dataSource, attachment.name, attachment.description)
}

return email
}

fun send() {
val email = prepareEmail()
email.send()
prepareEmail().send()
}

fun sendAsync(): Job {
Expand All @@ -84,11 +93,27 @@ class Mail(private val coroutineScope: CoroutineScope?, private val config: SMTP
try {
email.send()
} catch (t: Throwable) {
LOGGER.error(t.localizedMessage)
LOGGER.error(t.localizedMessage, t)
}
}
}

class Attachment(
val dataSource: DataSource,
val name: String,
val description: String? = null
) {
constructor(file: File, name: String, description: String? = null) : this(
FileDataSource(file), name, description
)

constructor(url: URL, name: String, description: String? = null) : this(URLDataSource(url), name, description)

constructor(data: ByteArray, name: String, description: String? = null, charset: String = UTF_8) : this(
ByteArrayDataSource(data, "application/octet-stream;charset=$charset"), name, description
)
}

private companion object {
val LOGGER: Logger = LoggerFactory.getLogger(Mail::class.java)
}
Expand Down