Skip to content

Instantly share code, notes, and snippets.

@BlackthornYugen
Last active June 3, 2024 00:15
Show Gist options
  • Save BlackthornYugen/1b3e1ff4426294e7054c9a7190e8f2cd to your computer and use it in GitHub Desktop.
Save BlackthornYugen/1b3e1ff4426294e7054c9a7190e8f2cd to your computer and use it in GitHub Desktop.
Sending an email using kotlin and javax.mail
/*
* Sending an email using kotlin and Apache Commons Email
*
* Usage: java -jar app.jar <user> <password> <from> <to> <cc>
*/
package main.kotlin.sendmail
import org.apache.commons.mail.Email
import org.apache.commons.mail.EmailException
import org.apache.commons.mail.HtmlEmail
import java.util.*
// dependencies {
// implementation 'org.apache.commons:commons-email:1.5'
// }
// <dependency>
// <groupId>org.apache.commons</groupId>
// <artifactId>commons-email</artifactId>
// <version>1.5</version>
// </dependency>
fun main(args: Array<String>) {
val userName = args[0]
val password = args[1]
// FYI: passwords as a command arguments isn't safe
// They go into your bash/zsh history and are visible when running ps
val emailFrom = args[2]
val emailTo = args[3]
val emailCC = args[4]
val subject = "SMTP Test"
val text = "Hello Kotlin Mail"
try {
val email: HtmlEmail = HtmlEmail()
email.hostName = "smtp.office365.com"
email.setSmtpPort(587)
email.setAuthentication(userName, password)
email.isSSLOnConnect = true
email.setFrom(emailFrom)
email.addTo(emailTo)
email.addCc(emailCC)
email.subject = subject
email.setMsg(text)
email.send()
println("Sent message successfully....")
} catch (emailException: EmailException) {
emailException.printStackTrace()
}
}
/*
* Sending an email using kotlin and javax.mail (Oracle JDK)
*
* Usage: java -jar app.jar <user> <password> <from> <to> <cc>
*/
package main.kotlin.sendmail
import java.util.*
import javax.mail.*
import javax.mail.internet.*
fun main(args: Array<String>) {
val userName = args[0]
val password = args[1]
// FYI: passwords as a command arguments isn't safe
// They go into your bash/zsh history and are visible when running ps
val emailFrom = args[2]
val emailTo = args[3]
val emailCC = args[4]
val subject = "SMTP Test"
val text = "Hello Kotlin Mail"
val props = Properties()
putIfMissing(props, "mail.smtp.host", "smtp.office365.com")
putIfMissing(props, "mail.smtp.port", "587")
putIfMissing(props, "mail.smtp.auth", "true")
putIfMissing(props, "mail.smtp.starttls.enable", "true")
val session = Session.getDefaultInstance(props, object : javax.mail.Authenticator() {
override fun getPasswordAuthentication(): PasswordAuthentication {
return PasswordAuthentication(userName, password)
}
})
session.debug = true
try {
val mimeMessage = MimeMessage(session)
mimeMessage.setFrom(InternetAddress(emailFrom))
mimeMessage.setRecipients(Message.RecipientType.TO, InternetAddress.parse(emailTo, false))
mimeMessage.setRecipients(Message.RecipientType.CC, InternetAddress.parse(emailCC, false))
mimeMessage.setText(text)
mimeMessage.subject = subject
mimeMessage.sentDate = Date()
val smtpTransport = session.getTransport("smtp")
smtpTransport.connect()
smtpTransport.sendMessage(mimeMessage, mimeMessage.allRecipients)
smtpTransport.close()
} catch (messagingException: MessagingException) {
messagingException.printStackTrace()
}
}
private fun putIfMissing(props: Properties, key: String, value: String) {
if (!props.containsKey(key)) {
props[key] = value
}
}
@Jyotiranjan8596
Copy link

i am getting this error

java.lang.ClassNotFoundException: Didn't find class "java.awt.datatransfer.Transferable"

@BlackthornYugen
Copy link
Author

i am getting this error

java.lang.ClassNotFoundException: Didn't find class "java.awt.datatransfer.Transferable"

Oh weird... What jdk are you using?

@M3d-Edge
Copy link

Where to put to email, Thanks

@loxal
Copy link

loxal commented Jun 2, 2024

javax.mail.* does not seem to be available in liberica-full-22. Which JRE or JDK were you using?

@BlackthornYugen
Copy link
Author

BlackthornYugen commented Jun 2, 2024

javax.mail.* does not seem to be available in liberica-full-22. Which JRE or JDK were you using?

I'm not 100% sure, probably Oracle's JDK, I made this in 2019. I wouldn't touch an Oracle JDK after 2020 when they started suing people. org.apache.commons:commons-email looks good though.

@loxal
Copy link

loxal commented Jun 3, 2024

@BlackthornYugen I think with

implementation("jakarta.mail:jakarta.mail-api:2.1.3")
implementation("org.eclipse.angus:jakarta.mail:2.0.3")

...and its jakarta.mail.* classes is what you need today. javax.mail.* not part of the JDK anymore which is probably a good thing anyway.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment