首页 > 解决方案 > 从 Ktor 应用程序发送电子邮件

问题描述

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

当用户向我的服务器发送请求但一无所获时,我在文档中搜索了任何处理发送电子邮件的功能。

post("/api/v1/auth") {
    // TODO send email when request is sent!
}

标签: ktor

解决方案


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

implementation("org.apache.commons:commons-email:1.5")

然后使用 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()

推荐阅读