【问题标题】:Play-Framework 2.3.x: Unable to send emails using plugin "play-mailer"Play-Framework 2.3.x:无法使用插件“play-mailer”发送电子邮件
【发布时间】:2015-05-13 07:28:01
【问题描述】:

我正在使用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);
}

更新

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

【问题讨论】:

  • 你能告诉我你在哪里找到了用于发送带有附件的电子邮件的示例代码的链接。
  • @Vicky 2 抱歉,我没有看到任何这样的例子。请在play-mailergithub 找到测试用例。希望这会对你有所帮助。

标签: scala email reactive-programming playframework-2.3


【解决方案1】:

我认为您需要从示例代码播放邮件中进行此更改

首先添加文件 /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 等异步任务中使用此代码

import play.api.libs.concurrent.Execution.Implicits.defaultContext

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

对于控制器中的异步方法

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

【讨论】:

  • 你好@anquegi,这对我仍然不起作用。此代码与示例代码相同。我不想创建不同的Action 来发送电子邮件。在我的操作中,我阅读了文件并创建了用户电子邮件列表以将电子邮件发送给用户。您的解决方案不起作用。
  • 好的,我将进行编辑并添加未来的 onsunccess 和 Failure 方法
  • 我希望这最终是你想要的。
  • 感谢@anquegi 的帮助,但另一个问题是,我没有收到电子邮件。
  • 我进行了编辑,最后的代码有效,您是否按照所有步骤操作?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-12-24
  • 1970-01-01
  • 2016-11-25
  • 1970-01-01
  • 1970-01-01
  • 2011-10-31
  • 1970-01-01
相关资源
最近更新 更多