Sending mail via GMail using Kotlin and commons-mail library

Date published30.07.2018Time to read2 minutes read

Continuing series of blog posts related to Kotlin, today we’ll together make a sample application showing us how we could automate email sending – using Kotlin programming language. Example is quite simple, and surely – you can enhance it with own features. Application mostly demonstrates compact Kotlin syntax, as well as full compatibility with Java or 3rd party libraries (in this case Apache commons-mail library).

Let’s first take a look at our script (scripts/mail/MailSender.kt):

package mail

import org.apache.commons.mail.DefaultAuthenticator
import org.apache.commons.mail.HtmlEmail

fun main(args: Array<String>) {
    val senderEmail = args[0]
    val password = args[1]
    val toMail = args[2]

    val email = HtmlEmail()
    email.hostName = ""
    email.setAuthenticator(DefaultAuthenticator(senderEmail, password))
    email.isSSLOnConnect = true
    email.subject = "Test email with inline image sent using Kotlin"
    val kotlinLogoURL = URL("")
    val cid = email.embed(kotlinLogoURL, "Kotlin logo")
    email.setHtmlMsg("<html><h1>Kotlin logo</h1><img src=\"cid:$cid\"></html>")

Application input

There are three input parameters required:

  • your GMail address
  • your GMail password
  • recipient's email address

Application dependencies

Application requires having 3 libs on your classpath. You can either use build tool to help you automate that, or, in case you don’t want to depend on that – just download the libs listed below, and put them in lib directory.

  • activation-1.1.1.jar
  • commons-email-1.4.jar
  • javax.mail-1.5.2.jar

Application layout

Application requires the following filesystem structure:

- project-root
  - scripts
    - mail
      - MailSender.kt
  - lib
    - activation-1.1.1.jar
    - commons-email-1.4.jar
    - javax.mail-1.5.2.jar

Compiling application (run from project root)

kotlinc \
 -cp lib/activation-1.1.1.jar:lib/commons-email-1.4.jar:lib/javax.mail-1.5.2.jar \
 scripts/mail/MailSender.kt -include-runtime -d mailSender.jar

Running application (run from project root)

java -cp "mailSender.jar:lib/\*" \
 mail.MailSenderKt \

In case you have enabled two factor authentication using your GMail account, you need to create application password here and use that as a password in command above.

Mail contain header and logo will be delivered to mail recipient you passed to java command above

That was all for today! Hope you liked it!