java Javamail传输使用无效凭据成功进行身份验证
我有这个代码来验证电子邮件和密码。如果我使用了有效的凭据,只要我运行应用程序,它就会进行身份验证。但是,如果我注销并尝试使用一些无效凭据再次登录,它将继续成功地进行身份验证,并且不会出现异常。似乎传输正在缓存以前的数据(有效凭据),并在我再次登录时使用它。我检查过了,变量“email”和“password”没有问题。当我先尝试一些无效凭据,然后再尝试一些有效凭据时,情况正好相反。你们知道发生了什么事吗
这是发生这种情况的一段代码:
谢谢
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.