11

A Non-blocking "Email sending" functionality in Scala - Knoldus Blogs

 3 years ago
source link: https://blog.knoldus.com/non-blocking-email-sending-functionality-scala/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

A Non-blocking "Email sending" functionality in Scala

Reading Time: < 1 minute

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

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

2) Add following code to Application.scala file

xxxxxxxxxx
def sendMail: Action[play.api.mvc.AnyContent] = Action { implicit request =>
mailForm.bindFromRequest.fold (
formWithErrors => {
Redirect("/")
},
mailData => {
Future {sendEmail(mailData.email)}
Redirect("/")
})
}
def sendEmail(email: String): Unit = {
val mail = use[MailerPlugin].email
mail.setSubject("Email sent using Scala")
mail.addRecipient(email)
mail.addFrom(email)
mail.send("Hello World")
}

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


Recommend

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK