【问题标题】:Javamail Transport.send(message) dosn't work in android using kotlinJavamail Transport.send(message) 在使用 kotlin 的 android 中不起作用
【发布时间】:2021-06-15 00:45:24
【问题描述】:

我想向用户发送一封电子邮件,我已经学习了很多教程,并且我做了他们所做的,但是,当我想执行 Transport.send(message) 时,程序停在这一行,

    suspend fun sendEmail()   {
  
    try {
        val props = Properties()
        props.put("mail.smtp.host", "smtp.gmail.com");
        props.put("mail.smtp.socketFactory.port", "465");
        props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.port", "465");

        Log.d(TAG, "sendEmail: one")
        val session = Session.getInstance(props, object : Authenticator() {
            override fun getPasswordAuthentication(): PasswordAuthentication {
                return PasswordAuthentication("my email ", "my password")
            }
        })
        val mm = MimeMessage(session)
        mm.setFrom(InternetAddress("my email"))
        mm.addRecipient(Message.RecipientType.TO, InternetAddress("recipient email"))
        mm.subject = "subject"
        mm.setText("hi how are you")
        Log.d(TAG, "sendEmail: two")
        Transport.send(mm)
        Log.d(TAG, "sendEmail: three")
        
        


    }catch (e: MessagingException){
        Log.d(TAG, "sendEmail: ${e.message}")
       throw Exception(e.message)
        
    }

}

我可以记录一和二,但三个不会显示,我也无法记录异常捕获中的内容

有什么办法吗?

【问题讨论】:

    标签: android email kotlin jakarta-mail transport


    【解决方案1】:

    我找到了解决方案,我应该启动 sendEmail() 并将协程限制在 Dispatchers.IO 中

    【讨论】:

      猜你喜欢
      • 2012-09-12
      • 1970-01-01
      • 1970-01-01
      • 2021-11-17
      • 2021-10-06
      • 2013-05-06
      • 2023-03-15
      • 2016-04-28
      • 2018-10-01
      相关资源
      最近更新 更多