英文:
Connecting to IMAPS MIcrosoft Exchange server fails with Cannot invoke "String.equals(Object)" because "this.status" is null
问题
我一直在尝试并且失败地连接到 IMAPs 服务器,使用 Javamail。这是我的示例实现:
public List<Mail> receive() throws MessagingException {
IMAPStore emailStore = null;
Folder emailFolder = null;
// 初始化 SMTP 接收器的属性
Properties properties = new Properties();
properties.setProperty("mail.imaps.ssl.enable", "true");
properties.setProperty("mail.imaps.ssl.checkserveridentity", "false");
properties.setProperty("mail.imaps.ssl.trust", "*");
properties.put("mail.store.protocol", "imaps");
properties.put("mail.imaps.host", host);
properties.put("mail.imaps.port", port);
// 获取 SMTP 会话
Session emailSession = Session.getInstance(properties);
try {
emailStore = (IMAPStore) emailSession.getStore();
emailStore.connect(username, password);
emailFolder = emailStore.getFolder("INBOX");
emailFolder.open(Folder.READ_WRITE);
return getNewMails(emailFolder);
} catch (Exception e) {
// 异常处理逻辑
}
}
代码在连接阶段失败。
这是服务器在初始连接时的响应:* CAPABILITY IMAP4 IMAP4rev1 AUTH=PLAIN SASL-IR UIDPLUS MOVE ID UNSELECT CHILDREN IDLE NAMESPACE LITERAL+
然后,Javacode 发现我没有经过身份验证,然后继续进行身份验证,之后我得到以下响应:
IMAPTaggedResponse
status = null
tag = "+"
message = ""
and 2 bytes in response
我完全无法继续。我尝试从配置属性中删除 "s",但这导致进展更少,因为服务器根本拒绝通信。
问题在于代码不是在我的实现中失败,而是在 javacode 本身中失败。这是失败的方法:
org.apache.geronimo.javamail.store.imap.connection.IMAPTaggedResponse#isBAD
失败的代码如下:
public boolean isBAD() {
return this.status.equals("BAD");
}
由于 IMAPTaggedResponse 返回的状态为 null,所以会导致 equals 方法失败。
我确实对凭据进行了 Base64 编码。
任何建议都将不胜感激。
英文:
I've been trying and failling to connect to IMAPs server using Javamail. This is my example implementation:
public List<Mail> receive() throws MessagingException {
IMAPStore emailStore = null;
Folder emailFolder = null;
// Initialize properties for the SMPT receiver
Properties properties = new Properties();
properties.setProperty("mail.imaps.ssl.enable", "true");
properties.setProperty("mail.imaps.ssl.checkserveridentity", "false");
properties.setProperty("mail.imaps.ssl.trust", "*");
properties.put("mail.store.protocol", "imaps");
// properties.put("mail." + protocol + ".host", host);
properties.put("mail.imaps.host", host);
// properties.put("mail." + protocol + ".port", port);
properties.put("mail.imaps.port", port);
//Get the SMTP session
Session emailSession = Session.getInstance(properties);
try {
emailStore = (IMAPStore) emailSession.getStore();
emailStore.connect(username, password);
emailFolder = emailStore.getFolder("INBOX");
emailFolder.open(Folder.READ_WRITE);
return getNewMails(emailFolder);
} catch (Exception e) {
exception handling logic
}
The code fails at connection.
This is the response of the server upon initial connection. * CAPABILITY IMAP4 IMAP4rev1 AUTH=PLAIN SASL-IR UIDPLUS MOVE ID UNSELECT CHILDREN IDLE NAMESPACE LITERAL+
Javacode then recognizes that I'm not authenticated and proceeds to authenticate, after which I get following response
IMAPTaggedResponse
status = null
tag = "+"
message = ""
and 2 bytes in response
I'm completely unable to proceed. I tried removing the "s" in imaps from the configuration properties, but that yields even less progress because the server outright refuses to communicate.
The problem is that the code fails not within my implementation, but within javacode itself. This is the failing method:
**org.apache.geronimo.javamail .store.imap.connection.IMAPTaggedResponse#isBAD**
and the failing code is the following:
public boolean isBAD() {
return this.status.equals("BAD");
}
and since the returning status from the IMAPTaggedResponse is null, it fails the equals method.
I do encode the credentials as Base64
any advice would be apretiated.
答案1
得分: 1
需要在获取会话时进行身份验证。
示例:
Session emailSession = Session.getInstance(properties,
new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(
"<username>", "<password>");
}
});
英文:
You need to authenticate when you're getting the session.
Example:
Session emailSession = Session.getInstance(properties,
new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(
"<username>", "<password>");
}
});
答案2
得分: 0
我已经弄清楚了。我使用了一个依赖项,它本身导入了这两个东西:
<groupId>org.apache.geronimo.specs</groupId>
<artifactId>geronimo-javamail_1.4_mail</artifactId>
<groupId>org.apache.geronimo.javamail</groupId>
<artifactId>geronimo-javamail_1.4_mail</artifactId>
这导致了一种情况,在运行时使用了不正确的库。当运行 mvn dependency:tree
时,很明显哪些依赖项负责这个问题。
在那之后,只需删除它们:
<exclusion>
<groupId>org.apache.geronimo.specs</groupId>
<artifactId>geronimo-javamail_1.4_mail</artifactId>
</exclusion>
<exclusion>
<groupId>org.apache.geronimo.javamail</groupId>
<artifactId>geronimo-javamail_1.4_mail</artifactId>
</exclusion>
请注意,我最终没有使用base64来存储凭据,而是仍然使用imaps而不是imap作为属性(尽管stackoverflow和javadoc上的大多数示例都使用imap),并且我使用完整的电子邮件地址作为用户名。
英文:
I figured it out. I used a dependency that itself imported these 2 things:
<groupId>org.apache.geronimo.specs</groupId>
<artifactId>geronimo-javamail_1.4_mail</artifactId>
<groupId>org.apache.geronimo.javamail</groupId>
<artifactId>geronimo-javamail_1.4_mail</artifactId>
That lead to a situation, where during runtime, incorrect library was used. When running mvn dependency:tree
it became obvious which dependencies are responsible for this.
aftr that, it was a matter of removing them with
<exclusion>
<groupId>org.apache.geronimo.specs</groupId>
<artifactId>geronimo-javamail_1.4_mail</artifactId>
</exclusion>
<exclusion>
<groupId>org.apache.geronimo.javamail</groupId>
<artifactId>geronimo-javamail_1.4_mail</artifactId>
</exclusion>
Note, that I ended up not using base64 for credentials, i still use imaps instead of imap for properties (even tough most examples on stackoverflow and javadoc state imap) and I use full email as username.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论