Last active
September 3, 2024 10:20
-
-
Save BlackthornYugen/1b3e1ff4426294e7054c9a7190e8f2cd to your computer and use it in GitHub Desktop.
Sending an email using kotlin and javax.mail
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* 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() | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* 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 | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@BlackthornYugen I think with
...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.