Javamail transport getting success to authenticate with invalid credentials

半城伤御伤魂 提交于 2019-12-25 08:34:35

问题


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.



来源:https://stackoverflow.com/questions/38808172/javamail-transport-getting-success-to-authenticate-with-invalid-credentials

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!