Java Android验证电子邮件凭据
我在这里尝试了两种答案:没有正确的结果 答案适用于gmail身份验证,但我希望能够覆盖任何SMTP主机。我正在使用smtp.1和1.com进行测试。无论何时我给出了不正确的凭证(上面有两个答案)我仍然会收到一条“成功”消息 我想将其格式化为第二个答案,下面是我使用的代码:Java Android验证电子邮件凭据,java,android,email,Java,Android,Email,我在这里尝试了两种答案:没有正确的结果 答案适用于gmail身份验证,但我希望能够覆盖任何SMTP主机。我正在使用smtp.1和1.com进行测试。无论何时我给出了不正确的凭证(上面有两个答案)我仍然会收到一条“成功”消息 我想将其格式化为第二个答案,下面是我使用的代码: settings_email_test.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { Str
settings_email_test.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String server = String.valueOf(settings_email_server_inp.getText());
int port = Integer.parseInt(settings_email_port_inp.getText().toString());
String username = String.valueOf(settings_email_username_inp.getText());
String password = String.valueOf(settings_email_password_inp.getText());
boolean auth = true;
String security = "SSL";
if(confirmSMTP(server, port, username, password, auth, security)){
Toast.makeText(getApplicationContext(), "success", Toast.LENGTH_LONG).show();
}
}
});
使用此方法:
public boolean confirmSMTP(String host, int port, String username, String password, boolean auth, String enctype) {
boolean result = false;
try {
Properties props = new Properties();
if (auth) {
props.setProperty("mail.smtp.auth", "true");
} else {
props.setProperty("mail.smtp.auth", "false");
}
if (enctype.endsWith("TLS")) {
props.setProperty("mail.smtp.starttls.enable", "true");
} else if (enctype.endsWith("SSL")) {
props.setProperty("mail.smtp.startssl.enable", "true");
}
Session session = Session.getInstance(props, null);
Transport transport = session.getTransport("smtp");
transport.connect(host, port, username, password);
transport.close();
result = true;
} catch(AuthenticationFailedException e) {
Toast.makeText(getApplicationContext(), "SMTP: Authentication Failed", Toast.LENGTH_LONG).show();
} catch(MessagingException e) {
Toast.makeText(getApplicationContext(), "SMTP: Messaging Exception Occurred", Toast.LENGTH_LONG).show();
} catch (Exception e) {
Toast.makeText(getApplicationContext(), "SMTP: Unknown Exception", Toast.LENGTH_LONG).show();
}
return result;
}
注意:我对答案中的代码做了一点修改,只是让端口为int,auth为boolean。无论哪种方式,当我使用不正确的凭据时,我都会收到一条成功消息和一条无错误消息。虽然如果我使用gmail,一切都很好
对于任何SMTP主机,我需要做什么才能使其正常工作?我发布的链接中给出的答案都有效-我的问题是我的应用程序正在缓存信息,我首先使用正确的凭据运行了“测试”。由于这些凭据是肯定的,并且应用程序缓存了这些凭据,因此它一直在成功写入
我在应用程序销毁时清除了缓存,并用它清除了那些凭据。我现在能够获得正确的交互。我发布的链接中给出的答案都是有效的-我的问题是我的应用程序正在缓存信息,我首先使用正确的凭据运行了“测试”。由于这些凭据是肯定的,并且应用程序缓存了这些凭据,因此它一直在成功写入 我在应用程序销毁时清除了缓存,并用它清除了那些凭据。我现在能够获得正确的交互