如何从我的应用程序通过电子邮件发送otp?

时间:2017-12-22 13:26:12

标签: android email smtp one-time-password

我曾尝试使用电子邮件发送方法将随机数作为otp发送到电子邮件地址 - 但这仅适用于从Android工作室直接安装应用程序的设备但是安装生成的apk的设备无法发送电子邮件 我使用普通的电子邮件发送代码发送otp ..(但电子邮件发送仅适用于直接从Android工作室安装应用程序的设备..

Random rand = new Random();
        n = rand.nextInt(55320) + 1;

        rec = reciep.getText().toString();
        ml = rec;
        subject = "Your OTP FOR app";
        textMessage = "YOUR OTP IS ->" + n;

        Properties props = new 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");

        session = Session.getDefaultInstance(props, new Authenticator() {
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication("myemail", "mypassword");
            }
        });

        pdialog = ProgressDialog.show(context, "", "Sending OTP...", true);

        RetreiveFeedTask task = new RetreiveFeedTask();
        task.execute();

    }
}

1 个答案:

答案 0 :(得分:1)

转到Google并打开安全性较低的应用 https://myaccount.google.com/lesssecureapps