gpt4 book ai didi

发送前的 JavaMail 密码验证

转载 作者:行者123 更新时间:2023-11-30 09:03:51 26 4
gpt4 key购买 nike

如何捕获 AuthenticationFailedExceptions?

我基本上有一个登录屏幕,我从用户名和密码中获取文本。我正在使用 Gmail 身份验证。

Properties properties = new Properties();
properties.put("mail.smtp.auth", "true");
properties.put("mail.smtp.starttls.enable", "true");
properties.put("mail.smtp.host","smtp.gmail.com");
properties.put("mail.smtp.port","587");

Session session = Session.getInstance(properties,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
}
);

如何查看验证是否成功?我知道调用诸如 Transport.send(message) 之类的语句时会出现错误。但我想检查身份验证是否成功 - 而不是实际发送消息。谢谢!

最佳答案

Transport transport;
try {
transport = session.getTransport("smtp");
transport.connect("smtp.gmail.com", username, password);
transport.close();

//Authentication success
} catch (AuthenticationException e) {
System.out.println("Authentication Exception");
//Authentication failed. Handle this here.
}

假设用户名和密码已定义,这部分代码位于 OP 中原始代码部分之后,将能够进行验证。

如果执行了 catch 语句,则身份验证失败。如果不是,则认证成功。

关于发送前的 JavaMail 密码验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25498542/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com