scala - 在 scala playframework(2.6) 中向 Gmail 发送邮件
问题描述
我正在尝试向我的 gmail 帐户发送邮件。我收到“电子邮件发送”的回复。但我没有收到任何邮件。我正在使用 Scala Playframework(2.6)
libraryDependencies += "com.typesafe.play" %% "play-mailer" % "6.0.0"
libraryDependencies += "com.typesafe.play" %% "play-mailer-guice" % "6.0.0"
这远远是我所做的
控制器类
package controllers
import play.api.libs.mailer._
import java.io.File
import java.io.File
import java.io.InputStream
import play.api.Environment
import org.apache.commons.mail.EmailAttachment
import play.api.libs.mailer._
import play.api.mvc.{AbstractController, Action, Controller, ControllerComponents}
import akka.http.scaladsl.model.HttpHeader.ParsingResult.Ok
import org.apache.commons.mail.EmailAttachment
import javax.inject.Inject
import play.api.libs.json.Json
class MailController @Inject()(mailer: MailerClient, environment: Environment) extends Controller {
def sendWithCustomMailer = Action {
// val mailer = new SMTPMailer(SMTPConfiguration("typesafe.org", 1234))
// val id = mailer.send(Email("Simple email", "Mister FROM <abhinaykumar499@gmail.com>"))
val emailfrom="xxxxxx@gmail.com"
val emailto="yyyyyyyyy@gmail.com"
val subject ="Simple Email"
val bodytext="A text message";
val email = Email("Simple email", ""+emailfrom+"", Seq(""+emailto+""), bodyText = Some("A text message"))
mailer.send(email)
Ok(s"Email sent!")
}
我已经在我的 Application.conf 中添加了这个
play.mailer {
smtp.host = "smtp.gmail.com" // (mandatory)
port = 465// (defaults to 25)
ssl = true // (defaults to no)
tls = false // (defaults to no)
tlsRequired =false // (defaults to no)
user = "xxxxxx@gmail.com" // (optional)
password = "12121212" // (optional)
// debug = false // (defaults to no, to take effect you also need to set the log level to "DEBUG" for the application logger)
timeout = 600 // (defaults to 60s in milliseconds)
connectiontimeout = 600 // (defaults to 60s in milliseconds)
mock = true // (defaults to no, will only log all the email properties instead of sending an email)
}
解决方案
我认为您错误地配置了主机部分。您写了smtp.host = "smtp.gmail.com"
但正确的值是host = "smtp.gmail.com"
.
在我的项目中,google smtp 的工作配置如下:
play {
mailer {
host = "smtp.gmail.com"
port = 587
tls = yes
user = "xxxxxx@gmail.com"
password = "xxxxxxx"
}
}
推荐阅读
- scala - Scala 上类型边界隐含?
- sql - (?)SQL - 互连连接(如何在两个表之间建立链?)
- caching - 如何管理缓存龙卷风登录
- javascript - 两个 for 循环是否比一个更大的循环慢?
- scala - Scala:使用 Play 框架进行模板测试
- numpy - 带有字符串输入的 TensorFlow 数据集不保留数据类型
- wpf - DotNetCore、Prism 7、WPF - 未创建服务
- javascript - 三.js设置Mesh的全局位置导致抖动伪影
- makefile - makefile 中的 '*' 、 '~' 和 '%' 是什么?
- docker - 使用“Docker 外部的 Docker”设置时无法连接到 Jenkins 管道中的 Docker 容器