从 Ktor 应用程序发送电子邮件

Arr*_*row 9 ktor

我目前正在使用 Ktor Netty 引擎创建我的应用程序

我在文档中搜索了当用户向我的服务器发送请求时处理发送电子邮件的任何功能,但什么也没找到。

post("/api/v1/auth") {
    // TODO send email when request is sent!
}
Run Code Online (Sandbox Code Playgroud)

Arr*_*row 9

Ktor apache-email-commons 中有一个可用的 java 依赖项

implementation("org.apache.commons:commons-email:1.5")
Run Code Online (Sandbox Code Playgroud)

然后使用 SMTP 服务器,例如 Gmail:

val email = SimpleEmail()
email.hostName = "smtp.googlemail.com"
email.setSmtpPort(465)
email.setAuthenticator(DefaultAuthenticator("email-account", "account-password"))
email.isSSLOnConnect = true
email.setFrom("sender-email-account")
email.subject = "email-subject"
email.setMsg("message-content")
email.addTo("target-email-address")
email.send()
Run Code Online (Sandbox Code Playgroud)

  • 自 2022 年 5 月起,Google 不再支持非付费工作区帐户的“安全性较低的应用程序访问”。 (2认同)