Sending Emails in Playframework 2.0

In Playframework 2.0, it does not appear to be as simple to send emails (see comments on Using sendmail as SMTP server in Play Framework) as it did in Play 1.x. There is no out of the box mailer functionality... so, how do I send emails?

Playframework 2.x requires a plugin for Mail to work. It was not added to the core because developers felt it was trivial to get Email working, so decided it was better to create a plugin. However, the quick flurry of messages on google groups suggested they got it wrong...people expected the same functionality as Play 1.x.

As you expect from the community though, a plugin was quickly built. See https://github.com/playframework/play-mailer.

There will be more plugins to look out for as well, but this is the typesafe supported one by a core developer, so I expect it to be the best maintained.

emails - 1.3.x, Sending e-mail. E-mail functionality uses the Apache Commons Email library under the hood. You can use the play.libs.Mail utility class to send e-mail very  mail.smtp.host=smtp.taldius.net If your SMTP server requires authentication, use the following properties: mail.smtp.user=jfp mail.smtp.pass=topsecret Channel & ports. There are two ways to send the email over an encrypted channel.

The accepted answer is that Play needs a plugin to send e-mails. This is false. You can easily adapt any JVM mailing library for your Play app. Here's an example using Apache Commons Email, adapted for simplicity from here and our own production code.

import org.apache.commons.mail._
import scala.util.Try

private val emailHost = Play.configuration.getString("email.host").get

/**
 *  Sends an email
 *  @return Whether sending the email was a success
 */
def sendMail(from: (String, String), // (email -> name)
             to: Seq[String],
             cc: Seq[String] = Seq.empty,
             bcc: Seq[String] = Seq.empty,
             subject: String,
             message: String,
             richMessage: Option[String] = None,
             attachment: Option[java.io.File] = None) = {

  val commonsMail: Email = if(mail.attachment.isDefined) {
      val attachment = new EmailAttachment()
      attachment.setPath(mail.attachment.get.getAbsolutePath)
      attachment.setDisposition(EmailAttachment.ATTACHMENT)
      attachment.setName("screenshot.png")
      new MultiPartEmail().attach(attachment).setMsg(mail.message)
    } else if(mail.richMessage.isDefined) {
      new HtmlEmail().setHtmlMsg(mail.richMessage.get).setTextMsg(mail.message)
    } else {
      new SimpleEmail().setMsg(mail.message)
    }
  }

  commonsMail.setHostName(emailHost)

  to.foreach(commonsMail.addTo(_))
  cc.foreach(commonsMail.addCc(_))
  bcc.foreach(commonsMail.addBcc(_))

  val preparedMail = commonsMail.
    setFrom(mail.from._2, mail.from._1).
    setSubject(mail.subject)

  // Send the email and check for exceptions
  Try(preparedMail.send).isSuccess
}

def sendMailAsync(...) = Future(sendMail(...))

Given that e-mail sending is so trivially accomplished in Play, I'm surprised plugins are recommended at all. Depending on a plugin can hurt you if you want to upgrade Play versions, and I don't feel something that takes 30 LoC to accomplish yourself is worth it. Our code has worked unmodified upgrading from Play 2.0 to 2.1 to 2.2.

emails - 1.2.4, Sending e-mail. E-mail functionality uses the Apache Commons Email library under the hood. You can use the play.libs.Mail utility class to send e-mail very  E-mail functionality uses the Apache Commons Email library under the hood. You can use the play.libs.Mail utility class to send e-mail very easily. A simple e-mail: SimpleEmail email = new SimpleEmail(); email.setFrom("[email protected]"); email.addTo("[email protected]"); email.setSubject("subject"); email.setMsg("Message"); Mail.send(email);

I quickly hacked plugin with support for attachments, because so far the one mentioned @Codemwnci doesn't have it. You can check it out.

Sending e-mail, You can use the play.libs.Mail utility class to send e-mail very easily. A simple e-​mail: SimpleEmail email = new SimpleEmail(); email. Sending e-mail with Play. You can use the play.libs.Mail utility to send e-mail very easily: Mail.send("[email protected]","[email protected]","Subject","Message"); Mail and MVC integration. You can also send complex, dynamic e-mail using the standard templates mechanism and syntax. First, define a Mailer notifier in your application.

Sending Emails in Playframework 2.0, Playframework 2.x requires a plugin for Mail to work. It was not added to the core because developers felt it was trivial to get Email working,  MailMessage is the object you would use to send the email, you can fill this object with the From, To, Subject and Body field of your email message. SmtpClient would send this object, you can use Send or SendAsync methods to send email, depending on your framework and the methods that you would use to send the email from.

Play framework 2.5 sending email, You might just have a typo. You need tls instead of ttl. From the github page: play.​mailer { host (mandatory) port (defaults to 25) ssl (defaults to no) tls (defaults to  Outlook Add-in. The Sendinc Add-In for Microsoft Outlook makes it easy and convenient to send and receive encrypted email. Sending a message is as easy as composing a message just as you normally would, then checking the "Send Secure" checkbox before sending.

play-easymail, Framework 2.x This plugin uses concepts from Play20StartApp to make sending emails (text, html, Framework 2.6.x (play-easymail version 0.9.0 and up). For the most part, if you had code that could send email via SMTP in the full framework, it’s likely a matter of a copy and paste job to get it going in.NET Core now! If you are having issues with this, ensure that you are on.NET Core 2.0 framework. You can check this by editing your csproj file, it should look like the following :

Comments
  • This list of plugins for 2.0 has been handy--note that it is supposed to be temporary: github.com/playframework/Play20/wiki/Modules
  • @Codemwnci: I'm looking through this module for attachment support but it seems to be either hiding or non-existent. Tips?
  • Path is wrong in readme - try "com.typesafe" % "play-plugins-mailer_2.9.1" % "2.0.4". notice _2.9.1.
  • If you're using Play! 2.1, use this dependency: "com.typesafe" %% "play-plugins-mailer" % "2.1.0"
  • Thank you for the reply!
  • I appreciate your answer!