Javamail传输使用无效凭据进行身份验证成功

时间:2016-08-06 19:50:44

标签: java android javamail

我有这段代码来验证电子邮件和密码。如果我使用有效凭据,则只要我运行该应用程序,它就会进行身份验证。但是,如果我注销并尝试使用一些无效凭证再次登录,它将继续成功进行身份验证,并且不会出现异常。看起来Transport正在保留以前的数据(有效凭据)缓存并在我再次登录时使用它。我检查了变量没有问题"电子邮件"和#34;密码"。当我首先尝试一些无效的凭证而后来尝试一些有效的凭证时,则相反。你们对发生的事情有什么想法吗?

这是它发生的代码:

谢谢!

public void check_user(final String email, final String password){
    final ProgressDialog pb = new ProgressDialog(this);
    pb.setIndeterminate(true);
    pb.setTitle("Verificando usuário");
    pb.setMessage("Por favor, aguarde...");
    pb.setCancelable(false);
    pb.show();
    Thread t = new Thread(new Runnable() {
        @Override
        public void run() {
            Properties props = new Properties();

            props.put("mail.smtp.host", "smtp.office365.com");
            props.put("mail.smtp.starttls.enable", "true");
            props.put("mail.smtp.auth", "true");
            props.put("mail.smtp.port", "587");

            Session session = Session.getDefaultInstance(props,
                    new javax.mail.Authenticator() {
                        //Authenticating the password
                        protected PasswordAuthentication getPasswordAuthentication() {
                            return new PasswordAuthentication(email, password);
                        }
                    });

            try {
                transport = session.getTransport("smtp");
                transport.connect(email, password);
                transport.close();
             } catch (Exception e) {
                e.printStackTrace();
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(LoginActivity.this, "Usuário e/ou senha inválidos.", Toast.LENGTH_LONG).show();
                        pb.dismiss();
                    }
                });
                return;
            }
            SharedPreferences.Editor data = getSharedPreferences("user_data", 0).edit();


            data.putString("username", email).commit();
            data.putString("password", password).commit();
            data.putBoolean("isLogged", true).commit();
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    get_in();
                }
            });

        }
    });
    t.start();

}

1 个答案:

答案 0 :(得分:0)