使用scala发送电子邮件并播放framwork在服务器

时间:2017-02-10 14:21:57

标签: scala email playframework

当我尝试使用此配置从localhost发送带有scala和Playmework的邮件时:

play.mailer {
  host=smtp.gmail.com
  port=465
  ssl=true
  user = "emaifortest@gmail.com"
  password = "password"
  debug=false
  mock=false
  from="emaifortest2013@gmail.com"
}

此函数在发送邮件之前将用户保存在tempuser集合中:

  def saveSendConfirmation = Action.async(parse.json) { implicit request =>
    request.body.validate[TempUserForm.Data].fold(
      form => Future.successful(BadRequest(Json.obj("message" -> "invalid data"))),
      data => {
        val firstName = data.firstName
        val lastName = data.lastName
        val email = data.email
        val customerId = data.customerId
        val communityId = data.communityId
        val entityId = data.entityId
        val roles = data.roles
        val currentDate: DateTime = new DateTime()
        val user = TempUser(Option(java.util.UUID.randomUUID), firstName, lastName, email, customerId, communityId, entityId, roles, Option(false), currentDate)
        tempUserService.create(user).map {
          case Right(_id) =>
            val email = Email(
              "Verification",
              "<emaifortest2013@gmail.com>",
              Seq(user.email),
              attachments = Seq(),
              bodyText = Some("A text message"),
              bodyHtml = Some(
                """ 
          <a href='http://5.189.165.188:9001/confirmation/""" + _id + """'>
"""))
            mailerClient.send(email)
            Ok(Json.toJson(Json.obj("_id" -> _id)))
          case Left(error) => BadRequest(Json.toJson(Json.obj("message" -> error)))
        }
      })
  }

当我在本地测试或使用testProd测试一切正常但在我尝试发送电子邮件时在服务器上我得到500错误:

! @732jpm95i - Internal server error, for (POST) [/tempuser] ->

play.api.UnexpectedException: Unexpected exception[EmailException: Sending the email to the following server failed : smtp.gmail.com:465]
    at play.api.http.HttpErrorHandlerExceptions$.throwableToUsefulException(HttpErrorHandler.scala:261) ~[com.typesafe.play.play_2.11-2.4.6.jar:2.4.6]

1 个答案:

答案 0 :(得分:0)

whole error log显示答案本身

Caused by: javax.mail.AuthenticationFailedException: 
...
Please log in via your web browser and then try again.
Learn more at https://support.google.com/mail/answer/78754

您可以从本地登录,因此看起来Google会检查IP或尝试询问身份验证过程中的第二步。您需要阅读https://support.google.com/mail/answer/7126229

我有类似的情况并使用&#34; less secure apps&#34;谷歌帐户中的选项,所以我可以从本地和服务器发送电子邮件。