【问题标题】:Sending Emails in Playframework 2.0在 Playframework 2.0 中发送电子邮件
【发布时间】:2012-05-08 05:08:19
【问题描述】:

在 Playframework 2.0 中,发送电子邮件似乎不像在 Play 1.x 中那样简单(请参阅Using sendmail as SMTP server in Play Framework 上的 cmets)。没有开箱即用的邮件功能......那么,我如何发送电子邮件?

【问题讨论】:

    标签: playframework playframework-2.0


    【解决方案1】:

    Playframework 2.x 需要一个插件才能使 Mail 工作。它没有被添加到核心,因为开发人员觉得让电子邮件工作很简单,所以决定最好创建一个插件。然而,谷歌群组上的一连串消息表明他们弄错了......人们期望与 Play 1.x 具有相同的功能。

    正如您对社区所期望的那样,一个插件很快就建立起来了。见https://github.com/playframework/play-mailer

    还会有更多插件值得关注,但这是核心开发人员支持的类型安全插件,所以我希望它是维护得最好的。

    【讨论】:

    • 这个 2.0 的插件列表很方便——注意它应该是临时的:github.com/playframework/Play20/wiki/Modules
    • @Codemwnci:我正在查看此模块以获取附件支持,但它似乎隐藏或不存在。提示?
    • 自述文件中的路径错误 - 尝试"com.typesafe" % "play-plugins-mailer_2.9.1" % "2.0.4"。通知_2.9.1
    • 如果您使用的是 Play! 2.1、使用这个依赖:"com.typesafe" %% "play-plugins-mailer" % "2.1.0"
    • @LeventeHolló 2.2.6 的依赖项是什么?谢谢
    【解决方案2】:

    公认的答案是 Play 需要一个插件来发送电子邮件。这是错误的。您可以轻松地为您的 Play 应用程序调整任何 JVM 邮件库。这是一个使用 Apache Commons 电子邮件的示例,为了简单起见,来自 here 和我们自己的生产代码。

    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(...))
    

    鉴于在 Play 中发送电子邮件如此简单,我很惊讶插件竟然被推荐了。如果您想升级 Play 版本,依赖插件可能会对您造成伤害,而且我不觉得需要 30 LoC 来完成自己的事情是值得的。我们的代码在从 Play 2.0 到 2.1 到 2.2 的未经修改的情况下工作。

    【讨论】:

    • 感谢您的回答!
    【解决方案3】:

    我很快就破解了支持附件的插件,因为到目前为止提到的@Codemwnci 没有它。 You can check it out.

    【讨论】:

      猜你喜欢
      • 2010-11-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-19
      • 1970-01-01
      • 1970-01-01
      • 2011-09-16
      相关资源
      最近更新 更多