I tried to make a validation code when user sign up via below fun
class GMailSender : Authenticator() {
val fromEmail = "myemail@a.com"
val password = "mypwd"
val code = (1000..9999).random().toString()
override fun getPasswordAuthentication(): PasswordAuthentication {
return PasswordAuthentication(fromEmail, password)
}
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.put("mail.smtp.ssl.enable", "true")
props.setProperty("mail.smtp.quitwait", "false")
val session = Session.getDefaultInstance(props, this@GMailSender)
val message = MimeMessage(session)
message.sender = InternetAddress(fromEmail)
message.addRecipient(Message.RecipientType.TO, InternetAddress(toEmail))
message.subject =
"verfication code"
message.setText("validation" + code)
Transport.send(message)
}
}
and it really works. but the problem is when i tried to compare by below code
private fun check_VerificationCode(): Boolean {
var check = false
binding.verificationBtn.setOnClickListener {
if (binding.verificationArea.text.toString() == GMailSender().code ) {
check = true
} else {
Toast.makeText(this, "unmatch", Toast.LENGTH_SHORT).show() Toast.makeText(this,binding.verificationArea.text.toString(),Toast.LENGTH_SHORT).show()
Toast.makeText(this,GMailSender().code,Toast.LENGTH_SHORT).show()
}
}
return check
}
}
and i have two problems.
- The number in the email(the number that is created when email send) is different from the actual numbers(GMailSender().code)
- The random number is keep changing.
How can i match them correctly?
Aucun commentaire:
Enregistrer un commentaire