diff --git a/README.md b/README.md
index b6a2889..85c6663 100644
--- a/README.md
+++ b/README.md
@@ -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
@@ -38,6 +38,12 @@ implementation("com.icerockdev.service:email-service:0.3.0")
subject = "TEST EMAIL"
to = mutableMapOf("to@icerockdev.com" to "Test Person")
html = "
Test test test
"
+ attachments = listOf(
+ Mail.Attachment(
+ file = File("test.pdf"),
+ name = "test.pdf"
+ )
+ )
}.sendAsync()
````
diff --git a/email-service/build.gradle.kts b/email-service/build.gradle.kts
index e4505a9..c615b78 100644
--- a/email-service/build.gradle.kts
+++ b/email-service/build.gradle.kts
@@ -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")
diff --git a/email-service/src/main/kotlin/com/icerockdev/service/email/Mail.kt b/email-service/src/main/kotlin/com/icerockdev/service/email/Mail.kt
index ee3dd08..a5c35c0 100644
--- a/email-service/src/main/kotlin/com/icerockdev/service/email/Mail.kt
+++ b/email-service/src/main/kotlin/com/icerockdev/service/email/Mail.kt
@@ -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) {
@@ -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 = emptyList()
private var isHtml: Boolean = false
private fun prepareEmail(): Email {
@@ -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 {
@@ -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)
}