Play-Framework 2.3.x:无法使用插件“播放邮件”发送电子邮件 [英] Play-Framework 2.3.x: Unable to send emails using plugin "play-mailer"

查看:145
本文介绍了Play-Framework 2.3.x:无法使用插件“播放邮件”发送电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am using play-framework 2.3.x and scala 2.11.4. When integrate play-mailer for sending and emails from my application, there is nothing happen. In the logs there are no exception produces and no return values are available. Following is email properties:

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 方法:

I need to send my all emails to async task. My CustomUtility method :

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

更新

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

The major problem is that, i am not receiving an emails

推荐答案

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

I think that you need to make this changes from the sample code play mailer

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

firts add the file /conf/play.plugins with the content:

1500:play.api.libs.mailer.CommonsMailerPlugin

第二您在application.conf中的配置必须是:

Second you configuration in application.conf must be:

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

,控制器发送你的代码,哟确实需要使用期货,因为我按照 github存储库

and the controller sending your code this, yo do need to use futures as I follow the example in the 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!")
  }


}

您可以在asyn任务中使用此代码,如异步

You can use this code inside an asyn task like async

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

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

对于控制器中的asyncronus methos

for an asyncronus methos in a controller

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,我推荐使用此代码

If you want to use scala Futures in your action I recommend that use this code

导入:

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

方法是: p>

the method is:

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

and your 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
  }

}

和我的用户类仅用于样本

and my user class just for the sample

package models

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

}

我希望您的用户类和用户列表与您的代码一起工作,如果不是请写下它们。我希望这有效。我一直推荐这个条目在Alvin Alexander博客中使用期货: http://alvinalexander.com/scala/ scala-future-semantics

I hope that your user class and user list works with your code if not please write them. I hope this works. I always recommend this entry in Alvin Alexander Blog for using futures: http://alvinalexander.com/scala/scala-future-semantics

这篇关于Play-Framework 2.3.x:无法使用插件“播放邮件”发送电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆