问题
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
My Scala Code:
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)
}
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);
}
Update
The major problem is that, i am not receiving an emails
回答1:
I think that you need to make this changes from the sample code play mailer
firts add the file /conf/play.plugins with the content:
1500:play.api.libs.mailer.CommonsMailerPlugin
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"
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!")
}
}
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()
}
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))
}
do not forget to add this import
import scala.concurrent.ExecutionContext.Implicits.global
If you want to use scala Futures in your action I recommend that use this code
imports:
import scala.concurrent.Future
import scala.util.{Failure, Success}
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")
}
and your CustomUtility
package controllers
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) {
}
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
来源:https://stackoverflow.com/questions/30208339/play-framework-2-3-x-unable-to-send-emails-using-plugin-play-mailer