Javamail transport getting success to authenticate

2019-09-09 13:44发布

问题:

I have this code to authenticate the e-mail and password. If I use valid credentials, as soon as I run the app, it authenticates. But if I logout and try to login again with some invalid credential, it continues to getting success to authenticate and no Exception is risen. It seems like the Transport is keeping the previously data (the valid credentials) cached and using it when I login again. I checked and there's no problem with the variables "email" and "password". The opposite happens when I try some invalid credentials first and some valid one later. Do you guys have any idea about what is happening?

This is the piece of code where it happens:

Thanks!

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:

Change Session.getDefaultInstance to Session.getInstance.