Я пытаюсь отправить почту Gmail с помощью SMTP в Kotlin.
Это также работает на эмуляторе Android, но не работает на реальных устройствах, таких как планшет или телефон Sumsung.
Я попробовал несколько способов отправки почты на Gmail, но на эмуляторе все работает, но на реальных устройствах не работает.
Код: Выделить всё
val stringSenderEmail = "*******@gmail.com"
val receiverEmail = InternetAddress(recipients)
val stringPasswordSenderEmail = "***********"
val stringHost = "smtp.gmail.com"
val properties: Properties = System.getProperties()
properties.setProperty("mail.transport.protocol", "smtp")
properties.setProperty("mail.host", stringHost)
properties["mail.smtp.host"] = stringHost
properties["mail.smtp.port"] = "465"
properties["mail.smtp.socketFactory.fallback"] = "false"
properties.setProperty("mail.smtp.quitwait", "false")
properties["mail.smtp.socketFactory.port"] = "465"
properties["mail.smtp.starttls.enable"] = "true"
properties["mail.smtp.socketFactory.class"] = "javax.net.ssl.SSLSocketFactory"
properties["mail.smtp.ssl.enable"] = "true"
properties["mail.smtp.auth"] = "true"
val session: Session = Session.getInstance(properties, object : Authenticator() {
override fun getPasswordAuthentication(): javax.mail.PasswordAuthentication {
return javax.mail.PasswordAuthentication(
stringSenderEmail,
stringPasswordSenderEmail
)
}
}).apply {
debug = true // Enable session debugging
}
val mimeMessage = MimeMessage(session)
mimeMessage.addRecipient(Message.RecipientType.TO, receiverEmail)
mimeMessage.subject = subject
mimeMessage.setText(body)
val thread = Thread {
try {
Transport.send(mimeMessage)
} catch (e: MessagingException) {
Log.d("sending email error ====>", e.toString())
e.printStackTrace()
}
}
thread.start()
Код: Выделить всё
Пожалуйста, помогите мне.
Спасибо
Подробнее здесь: https://stackoverflow.com/questions/790 ... -in-kotlin