我正在尝试编写一个简单的 Java 程序,它只发送电子邮件并从我的收件箱中的电子邮件中检索文本,我对 Authenticator 对象的工作方式有点困惑。为什么在构造函数之后立即有括号中的代码,它是如何工作的?我找到了这个用于创建 Session 对象的示例代码,它工作正常,但我以前从未见过这种表示法,并且在其他地方找不到任何其他示例。
Session session = Session.getInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});