A Non-blocking “Email sending” functionality in Scala

In our last blog “Adding an Email Sending Functionality in Play using Scala” we explained how to include an Email sending functionality in a Play Scala Application. But the way in which we implemented it, made it a Blocking one i.e., application will wait until email has been sent.

In this blog we will explain, how to make the Email Sending functionality a Non-blocking one. By making it non-blocking, the application will not wait until email is sent & will move on to the next step.

To make this functionality a Non-Blocking one, just make following changes in the mailSendingApp we posted earlier:-

1) Import following packages in Application.scala file

import scala.concurrent.Future
import scala.concurrent.ExecutionContext.Implicits._


2) Add following code to Application.scala file

def sendMail: Action[play.api.mvc.AnyContent] = Action { implicit request =>
  mailForm.bindFromRequest.fold (
   formWithErrors => {
   mailData => {
    Future {sendEmail(mailData.email)}

 def sendEmail(email: String): Unit = {
  val mail = use[MailerPlugin].email
             mail.setSubject("Email sent using Scala")
             mail.send("Hello World")


Note – To download the App with changes just click here.

This entry was posted in Play Framework, Reactive, Scala, Web and tagged , , , . Bookmark the permalink.

2 Responses to A Non-blocking “Email sending” functionality in Scala

  1. rethab says:

    Note that this is not non-blocking, but simply asynchronous. Whichever thread ends up running the ‘sendEmail’ function still blocks.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s