Sending emails in Databricks by using Scala

This is going to be quite a short post, but is more intended as reference :)

Something I found to be quite interesting is sending emails through Databricks to warn me when something happened in parts of my code. After searching a bit, I came up with this code that would help me send an email:

Databricks secrets are being used, check out my other post on how to configure those: Databricks Secrets

import java.util.Properties
import java.util.Properties
import javax.mail.{Message, Session}
import javax.mail.internet.{InternetAddress, MimeMessage}

import scala.io.Source

val host = "<YOUR_SMTP>"
val port = "587"

val address = "<YOUR_EMAIL>"
val username = "<YOUR_USERNAME>"
val password = dbutils.secrets.get("<SECRET_SCOPE>", "EMAIL_SMTP_PASSWORD") 

def sendEmail(mailSubject: String, mailContent: String) = {
    val properties = new Properties()
    properties.put("mail.smtp.port", port)
    properties.put("mail.smtp.auth", "true")
    properties.put("mail.smtp.starttls.enable", "true")

    val session = Session.getDefaultInstance(properties, null)
    val message = new MimeMessage(session)
    message.addRecipient(Message.RecipientType.TO, new InternetAddress(address));
    message.setContent(mailContent, "text/html")

    val transport = session.getTransport("smtp")
    transport.connect(host, username, password)
    transport.sendMessage(message, message.getAllRecipients)

Xavier Geerinck

Xavier works as a Cloud Solution Architect at Microsoft, helping its customer unlock the full potential of the cloud. Even though he is still considered a young graduate, he achieved his first success at the age 16, by creating and selling his first startup. He then took this knowledge to create and help more startups in different markets such as technology, social media, philanthropy and home care. While in the meantime gaining more enterprise insights at renowned enterprises such as Nokia, Cisco and now Microsoft.

Read More