Play-Framework 2.3.x: 无法使用插件 "play-mailer" 发送电子邮件
Play-Framework 2.3.x: Unable to send emails using plugin "play-mailer"
我正在使用 play-framework 2.3.x
和 scala 2.11.4
。当集成 play-mailer
用于发送和来自我的应用程序的电子邮件时,没有任何反应。在日志中没有异常产生,也没有 return 值可用。以下是电子邮件属性:
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);
}
更新
主要问题是,我没有收到电子邮件
我认为您需要根据示例代码播放邮件程序进行此更改
首先添加文件 /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
}
}
和我的用户 class 只是为了示例
package models
/**
* Created by anquegi on 13/05/15.
*/
case class User(email:String) {
}
我希望您的用户 class 和用户列表适用于您的代码,如果不能,请编写它们。我希望这有效。我总是推荐 Alvin Alexander 博客中的这个条目来使用期货:http://alvinalexander.com/scala/scala-future-semantics
我正在使用 play-framework 2.3.x
和 scala 2.11.4
。当集成 play-mailer
用于发送和来自我的应用程序的电子邮件时,没有任何反应。在日志中没有异常产生,也没有 return 值可用。以下是电子邮件属性:
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);
}
更新
主要问题是,我没有收到电子邮件
我认为您需要根据示例代码播放邮件程序进行此更改
首先添加文件 /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
}
}
和我的用户 class 只是为了示例
package models
/**
* Created by anquegi on 13/05/15.
*/
case class User(email:String) {
}
我希望您的用户 class 和用户列表适用于您的代码,如果不能,请编写它们。我希望这有效。我总是推荐 Alvin Alexander 博客中的这个条目来使用期货:http://alvinalexander.com/scala/scala-future-semantics