Play-Framework 2.3.x:无法使用插件发送电子邮件"播放邮件"

时间:2015-05-13 07:28:01

标签: scala email reactive-programming playframework-2.3

我正在使用play-framework 2.3.xscala 2.11.4。当集成play-mailer用于从我的应用程序发送和发送电子邮件时,没有任何事情发生。在日志中没有异常产生,也没有返回值。以下是电子邮件属性:

smtp.host = "smtp.gmail.com" 
smtp.port = 25 
smtp.user = "n****@gmail.com" 
smtp.password = "*******"
smtp.debug = true 
smtp.mock = true 

我的Scala代码:

Future{
  var subject = "Invitation email";
  var from = "h****@gmail.com";
  var to = userList.map { user => user.email }.seq;
  var email: Email = new Email(subject, from, to);
  CustomUtility.sendEmail(email)
}

我需要将所有电子邮件发送到async任务。我的CustomUtility方法:

def sendEmail(email: Email){
 var message = MailerPlugin.send(email);
 println("MESSAGE >>>>>>>>>>>>>>>>>>>>>>>>>> : "+message);
}

更新

主要问题是,我没有收到电子邮件

1 个答案:

答案 0 :(得分:1)

我认为您需要从示例代码播放邮件

进行此更改

firts将文件/conf/play.plugins添加到内容:

1500:play.api.libs.mailer.CommonsMailerPlugin

其次,您在application.conf中的配置必须是:

smtp.host="smtp.gmail.com"
smtp.port=465
smtp.ssl=true
smtp.tls=true
smtp.user="yourgmailuser@gmail.com"
smtp.password="yourpasswor"

和控制器发送你的代码,你需要使用期货,因为我按照github repository

中的例子
package controllers


import models.SignUpValidation
import play.api.libs.json.{JsError, Json}
import play.api.mvc._


import java.io.File

import play.api.libs.mailer._
import org.apache.commons.mail.EmailAttachment
import play.api.mvc.{Action, Controller}
import play.api.Play.current


object Application extends Controller {

  def send = Action {

val email:Email = Email(
  "Simple email",
  "Mister FROM <from@email.com>",
  Seq("Miss TO <to@email.com>"),
  attachments = Seq(
    AttachmentFile("favicon.png", new File(current.classloader.getResource("public/images/favicon.png").getPath)),
    AttachmentData("data.txt", "data".getBytes, "text/plain", Some("Simple data"), Some(EmailAttachment.INLINE))
  ),
  bodyText = Some("A text message"),
  bodyHtml = Some("<html><body><p>An <b>html</b> message</p></body></html>")
)

val id = MailerPlugin.send(email)

Ok(s"Email $id sent!")
  }


}

您可以在async

之类的asyn任务中使用此代码
import play.api.libs.concurrent.Execution.Implicits.defaultContext

val futureInt: Future[Int] = scala.concurrent.Future {
  sendMail()
}

对于控制器中的asyncronus方法

def sendWithFuture = Action.async {


      val futureString = scala.concurrent.Future {

        val email:Email = Email(
          "Simple email",
          "Mister FROM <anquegi@email.com>",
          Seq("Miss TO <antonio.querol@cuaqea.com>"),
          attachments = Seq(
            AttachmentFile("favicon.png", new File(current.classloader.getResource("public/images/favicon.png").getPath)),
            AttachmentData("data.txt", "data".getBytes, "text/plain", Some("Simple data"), Some(EmailAttachment.INLINE))
          ),
          bodyText = Some("A text message"),
          bodyHtml = Some("<html><body><p>An <b>html</b> message</p></body></html>")
        )

        MailerPlugin.send(email)


      }
      futureString.map(i => Ok("Got result: " + i))



  }

不要忘记添加此导入

import scala.concurrent.ExecutionContext.Implicits.global

如果您想在行动中使用scala Futures,我建议您使用此代码

进口:

import scala.concurrent.Future
import scala.util.{Failure, Success}

方法是:

def SendUsingScalaFutures = Action {

//Your code

val userList:List[User] = List(
  new User("email1"), new User("email2"))

val task = Future {
  var subject = "Invitation email";
  var from = "anquegi@gmail.com";
  var to = userList.map { user => user.email }.seq;

  var email: Email = new Email(subject, from, to);
  CustomUtility.sendEmail(email)// this will be better to return a String

}

// whenever the task completes, execute this code
task.onComplete {
  case Success(value) => println(s"MESSAGE >>>>>>>>>>>>>>>>>>>>>>>>>> :  ${value}" )
  case Failure(e) => println(s"D'oh! The task failed: ${e.getMessage}")
}



//Other code

Ok("Finish")



 }

和您的CustomUtility

包裹控制器

import play.api.libs.mailer._
import play.api.Play.current


/**
 * Created by anquegi on 13/05/15.
 */
object CustomUtility {
  def sendEmail(email: Email): Unit =  {
    val message = MailerPlugin.send(email);
    message
  }

}

和我的用户类仅用于示例

package models

/**
 * Created by anquegi on 13/05/15.
 */
case class User(email:String) {

}

我希望您的用户类和用户列表能够与您的代码一起使用,如果没有请编写它们。我希望这有效。我总是建议在Alvin Alexander博客中使用期货:http://alvinalexander.com/scala/scala-future-semantics