【问题标题】:Sending Emails From Ktor Application从 Ktor 应用程序发送电子邮件
【发布时间】:2021-06-08 19:06:20
【问题描述】:

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

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

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

【问题讨论】:

    标签: ktor


    【解决方案1】:

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

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

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

    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()
    

    【讨论】:

    • 如果您使用 smtp.googlemail.com,您必须关闭“不太安全的应用程序访问”,否则您将收到“有人刚刚使用您的密码尝试从非-谷歌应用程序”。此外,此设置不适用于启用两步验证的帐户。这种方法有效,但不如通过 Web 服务的 OAuth2 安全。
    【解决方案2】:

    Ktor 中没有用于发送电子邮件的内置功能。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-05-24
      • 2011-05-08
      • 2011-08-06
      • 2011-02-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多