【问题标题】:scala Play Framework 2.4: sending emailscala Play Framework 2.4:发送电子邮件
【发布时间】:2015-12-24 09:51:11
【问题描述】:

我在使用 play-mailer 时尝试从 scala Play 框架 2.4 发送电子邮件,我已按照他们示例页面中的说明进行操作,但没有成功。

我已将依赖项添加到 build.sbt:

libraryDependencies ++= Seq(
  "com.typesafe.play" %% "play-mailer" % "3.0.1"
)

在 application.conf 中我添加了以下内容:

play.mailer {
    host=smtp.gmail.com
    port=465 
    ssl=true
    tls=true
    user="testme@gmail.com"
    password=abracadabra
}

最后是邮件类:

package controllers

import java.io.File
import javax.inject.Inject
import org.apache.commons.mail.EmailAttachment
import play.api.Configuration
import play.api.Play.current
import play.api.libs.mailer._

class Mail(mailer: MailerClient) {
  def send = {
    val cid = "1234"
    val email = Email(
      "Simple email",
      "Mister FROM <from@email.com>",
      Seq("Miss TO <to@email.com>"),
      bodyText = Some("A text message"),
      bodyHtml = Some("some data....")
    )
    mailer.send(email)
  }
}

到目前为止没有编译错误,但是我不明白如何初始化这个类..我应该如何获得“MailerClient”实例?

在文档中写着“然后在你的路由器定义中,使用 trait MailerComponents”,代码示例如下:

import play.api._
import play.api.ApplicationLoader.Context
import router.Routes
import play.api.libs.mailer._

class MyApplicationLoader extends ApplicationLoader {
  def load(context: Context) = {
    new ApplicationComponents(context).application
  }
}

class ApplicationComponents(context: Context) extends BuiltInComponentsFromContext(context) with MailerComponents {
  lazy val myComponent = new MyComponent(mailerClient)
  // create your controllers here ...
  lazy val router = new Routes(...) // inject your controllers here
}

(我在application.conf中添加了“play.application.loader=SimpleApplicationLoader”)

但我得到以下编译错误:

D:\myApp\app\SimpleApplicationLoader.scala:12: not found: type MailerComponents

[error] class ApplicationComponents(context: Context) extends BuiltInComponentsFromContext(context) with MailerComponents {
[error]                                                                                                  ^
[error] D:\myApp\app\SimpleApplicationLoader.scala:13: not found: value mailerClient
[error]   lazy val applicationController = new controllers.Mail(mailerClient)
[error]                                                         ^
[error] two errors found
[error] (compile:compileIncremental) Compilation failed

有什么想法吗?

【问题讨论】:

    标签: scala email playframework playframework-2.4


    【解决方案1】:

    您可以使用运行时依赖注入,正如其他答案所建议的那样。但是,如果您想采用当前的方法,请继续阅读...

    问题是,MailerComponents 特征在 3.x branch 中不存在。它似乎存在于master branch 中,但不存在于他们的next branch 中。我不确定他们在那里做什么。

    如果你想继续这个例子,你需要做一些摆弄并弄清楚如何让它编译。例如。环顾四周,我想出了以下内容。

    import play.api._
    import play.api.ApplicationLoader.Context
    import router.Routes
    import play.api.libs.mailer._
    
    class SimpleApplicationLoader extends ApplicationLoader {
      def load(context: Context) = {
        new ApplicationComponents(context).application
      }
    }
    
    class ApplicationComponents(context: Context) extends BuiltInComponentsFromContext(context) {
      val mailerClient = new CommonsMailer(configuration)
      lazy val applicationController = new controllers.ApplicationScala(mailerClient)
      lazy val assets = new controllers.Assets(httpErrorHandler)
      lazy val router = new Routes(httpErrorHandler, applicationController)
    }
    

    基本上,我并没有依赖不存在的MailerComponent 为我创建一个mailerClient,而是自己做的。

    如果controllers.ApplicationScala中有以下行

    val id = mailer.configure(Configuration.from(Map("host" -> "typesafe.org", "port" -> 1234))).send(email)
    

    替换为:

    val id = mailer.send(email)
    

    它会编译。同时,我认为我应该在 github 上提出一个关于此的问题。也许你应该这样做。

    【讨论】:

    • 谢谢巴希特!由于当前的方法不起作用,我试图通过运行时依赖注入发送电子邮件。现在,我不明白如何调用发送..我不希望用一个动作来做,而是每次发生其他几个动作之一时发送一封通知电子邮件......如果我尝试执行controllers.Mailer.send()从我的一个 Action 函数中,我得到以下编译错误:object Mailer is not a member of package controllers [error] Note: class Mailer exists, but it has no companion object.
    • @Jenny 看起来您没有实例化 Mailer 类。您不能直接使用controllers.Mailer.send()(就像您在路线中所做的那样)。如果您使用的是运行时 DI,请尝试将 Mailer 实例注入您的控制器。而且我认为 Mailer 类不应该是控制器包的一部分。
    【解决方案2】:

    我相信你可以简单地让 play 框架注入邮件客户端。设置配置并使用 @Inject() 注释您的构造函数应该可以工作。 Mail 控制器的声明如下所示:

    class Mail @Inject() (mailer: MailerClient) {
    

    【讨论】:

    • 谢谢卢卡斯。使用运行时依赖注入,我无法理解如何调用发送..我不希望使用操作来执行此操作,而是每次发生其他几个操作之一时发送通知电子邮件...如果我尝试从我的 Action 函数之一执行 controllers.Mailer.send() 我得到以下编译错误:object Mailer is not a member of package controllers [error] Note: class Mailer exists, but it has no companion object.
    • 如果你在不同的类中并且想要执行Mail类的方法,你将需要它的实例,你也可以要求注入class MyOtherClass @Inject() (mailer: Mailer)
    猜你喜欢
    • 1970-01-01
    • 2011-10-31
    • 1970-01-01
    • 1970-01-01
    • 2013-12-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-03
    相关资源
    最近更新 更多