'" javax.mail.AuthenticationFailedException ~" Using SMTP on Android
I want to send Authenticate code(randomly generated) by using SMTP on Android. But, I have a problem
This is my error
https://i.stack.imgur.com/W5XLZ.png
Do you have any solution about my problem?
Here's my source code
class GmailSender : Authenticator() {
private val fromEmail = R.string.manager_email.toString()
private val password = R.string.manager_email_pw.toString()
fun sendEmail(toEmail: String) {
CoroutineScope(Dispatchers.IO).launch {
val props = Properties()
props.setProperty("mail.transport.protocol", "smtp")
props.setProperty("mail.host", "smtp.gmail.com")
props.put("mail.smtp.auth", "true")
props.put("mail.smtp.port", "465")
props.put("mail.smtp.socketFactory.port", "465")
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory")
props.put("mail.smtp.socketFactory.fallback", "false")
props.setProperty("mail.smtp.quitwait", "false")
val session = Session.getDefaultInstance(props, this@GmailSender)
val message = MimeMessage(session)
message.setFrom(InternetAddress(fromEmail))
message.setRecipient(Message.RecipientType.TO, InternetAddress(toEmail))
message.subject = "Test subject"
message.setText("This is test email")
Transport.send(message)
}
}
}
// in MainActivity
binding.btnAuthenticate.setOnClickListener {
val toEmail = binding.etvInputUnivEmail.text.toString()
GmailSender().sendEmail(toEmail)
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
