【发布时间】:2016-03-29 06:19:32
【问题描述】:
我正在尝试编写一个简单的 Java 程序,它只发送电子邮件并从我的收件箱中的电子邮件中检索文本,但我对 Authenticator 对象的工作方式有点困惑。为什么在构造函数之后立即有括号中的代码,它是如何工作的?我找到了这个用于创建 Session 对象的示例代码,它运行良好,但我以前从未见过这种表示法,也无法在其他地方找到它的任何其他示例。
Session session = Session.getInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});
【问题讨论】:
标签: java constructor jakarta-mail authenticator