无法使用播放邮件程序创建应用程序

时间:2018-08-30 20:47:44

标签: playframework-2.6

我正在尝试将play-mailer集成到我的应用程序中-https://github.com/playframework/play-mailer#usage

我正在使用编译时注入。

到目前为止,在我的自定义应用加载程序中,我混合了MailerComponents特性(https://github.com/playframework/play-mailer/blob/master/play-mailer/src/main/scala/play/api/libs/mailer/MailerComponents.scala)。

application.conf中的配置为

play.mailer {
  host = localhost // (mandatory)
  port = 25 // (defaults to 25)
  ssl = no // (defaults to no)
  tls = no // (defaults to no)
  tlsRequired = no // (defaults to no)
  user = Administrator // (optional) //TODOM - am I suppose to enter the password here?
  password = codinngjedi // (optional)
  debug = no // (defaults to no, to take effect you also need to set the log level to "DEBUG" for the application logger)
  timeout = null // (defaults to 60s in milliseconds)
  connectiontimeout = null // (defaults to 60s in milliseconds)
  mock = true// (defaults to no, will only log all the email properties instead of sending an email)
}

我已经创建了一个MailerService,如下所示:

package services

import play.api.libs.mailer._

class MailerService(mailerClient: MailerClient) {
  def sendEmail = {
    val email = Email("Simple email", "Mister FROM <from@email.com>", Seq("Miss TO <to@email.com>"), bodyText = Some("A text message"))
    mailerClient.send(email)
  }

}

在我的自定义AppLoader中,我创建了MailerService的实例,如下所示:

val mailerService = new MailerService(mailerClient) //mailerClient is defined in MailerComponents of play-mailer library

我的代码未编译,因为我需要提供config所需的MailerComponents定义

trait MailerComponents {
  def config: Config
  lazy val mailerClient: SMTPMailer = new SMTPMailer(new SMTPConfigurationProvider(config).get())
}

但是我不知道该怎么做。 play-mailer文档说By default the Mailer Plugin will automatically configure the injected instance with the application.conf.,为什么我需要提供config以及如何创建它?

1 个答案:

答案 0 :(得分:0)

我可以将config定义为

import com.typesafe.config.{Config, ConfigFactory}
def config:Config = {
    val loadedConfig = ConfigFactory.load() //I suppose play looks for application.conf by default
    println("loaded config is "+loadedConfig)
    loadedConfig
  }
相关问题