Sending email from Scala / Play Framework version 2.3.6

I have just successfully sent an email from Play / Scala.  This is what I did:

1.  Add ”com.typesafe.play.plugins” %% “play-plugins-mailer” % “2.3.1” to libraryDependencies (build.sbt)

2.  Add the following to application.conf:


Email config

smtp.host=smtp.gmail.com smtp.port=465 smtp.ssl=yes smtp.tls=yes smtp.user=”my_username@gmail.com” smtp.password=my_password


I_f you use a ! in your smtp.password, put a “” round “!” (as ! is a reserved character).  Change your smtp.user (to your own email address)._

3. Create a new file conf/play.plugins:


1500:com.typesafe.plugin.CommonsMailerPlugin


 4. Add the following to routes:


GET /email controllers.Application.sendEmail


 5. Completely replace controllers/Application.scala (change the Recipient and From email addresses):


package controllers

import play.api.mvc._
import com.typesafe.plugin._
import play.api.Play.current

object Application extends Controller {

  def index = Action {
    Ok(views.html.index("Your new application is ready."))
  }

  def sendEmail() = Action {
    val mail = use[MailerPlugin].email
    mail.setSubject("mailer")
    mail.setRecipient("Miles Davenport <milesd@foo.com>","milesd@foo.com")
    //or use a list
    //mail.setBcc(List("Dummy <example@example.org>", "Dummy2 <example@example.org>"):_*)
    mail.setFrom("Miles Davenport <miles.davenport@anotheremail.com>")
    //adds attachment
    //mail.addAttachment("attachment.pdf", new File("/some/path/attachment.pdf"))
    //adds inline attachment from byte array
    //val data: Array[Byte] = "data".getBytes
    //mail.addAttachment("data.txt", data, "text/plain", "A simple file", EmailAttachment.INLINE)
    //sends html
    //mail.sendHtml("<html>html</html>" )
    //sends text/text
    mail.send( "text" )
    //sends both text and html
    //mail.send( "text", "<html>html</html>")
    Ok("Hello")
  }
}

6. Start Play. Point your favourite web browser at http://localhost:9000/email

Your browser should say Hello. Check your email. You should have a new email, with a subject of “mailer” :O)

More information is available from https://github.com/playframework/play-mailer

Author | Miles Davenport

Career programmer, who designs, assembles, fixes, and supports customers, software and systems.