scala Play Framework 2.4:发送电子邮件
scala Play Framework 2.4: sending email
我在使用 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
}
最后,邮件 Class:
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)
}
}
到目前为止没有编译错误,但是我不明白如何初始化这个 class.. 我应该如何获得 "MailerClient" 实例?
文档中写着"Then in your router definition, use the 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
有什么想法吗?
我相信你可以简单地让 play framework 注入邮件客户端。设置配置并使用 @Inject()
注释您的构造函数应该有效。
您的 Mail
控制器声明如下所示:
class Mail @Inject() (mailer: MailerClient) {
您可以使用 运行-time 依赖注入,正如其他答案所建议的那样。但是,如果您想继续使用当前的方法,请继续阅读...
问题是,MailerComponents
特征在 3.x branch. it does seem to exist in the master branch, but not in their 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 上提出一个问题。也许你应该。
我在使用 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
}
最后,邮件 Class:
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)
}
}
到目前为止没有编译错误,但是我不明白如何初始化这个 class.. 我应该如何获得 "MailerClient" 实例?
文档中写着"Then in your router definition, use the 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
有什么想法吗?
我相信你可以简单地让 play framework 注入邮件客户端。设置配置并使用 @Inject()
注释您的构造函数应该有效。
您的 Mail
控制器声明如下所示:
class Mail @Inject() (mailer: MailerClient) {
您可以使用 运行-time 依赖注入,正如其他答案所建议的那样。但是,如果您想继续使用当前的方法,请继续阅读...
问题是,MailerComponents
特征在 3.x branch. it does seem to exist in the master branch, but not in their 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 上提出一个问题。也许你应该。