首页 > 解决方案 > 无法使用 play-mailer 创建应用程序

问题描述

我正在尝试集成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)
  }

}

在我的 customAppLoader中,我创建了一个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以及如何创建它?

标签: playframework-2.6

解决方案


我可以定义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
  }

推荐阅读