连接到IMAPS Microsoft Exchange服务器失败,因为”this.status”为空。

huangapple go评论87阅读模式
英文:

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&lt;Mail&gt; receive() throws MessagingException {
    IMAPStore emailStore = null;
    Folder emailFolder = null;

    // Initialize properties for the SMPT receiver
    Properties properties = new Properties();
    properties.setProperty(&quot;mail.imaps.ssl.enable&quot;, &quot;true&quot;);
    properties.setProperty(&quot;mail.imaps.ssl.checkserveridentity&quot;, &quot;false&quot;);
    properties.setProperty(&quot;mail.imaps.ssl.trust&quot;, &quot;*&quot;);
    properties.put(&quot;mail.store.protocol&quot;, &quot;imaps&quot;);
//        properties.put(&quot;mail.&quot; + protocol + &quot;.host&quot;, host);
    properties.put(&quot;mail.imaps.host&quot;, host);
//        properties.put(&quot;mail.&quot; + protocol + &quot;.port&quot;, port);
    properties.put(&quot;mail.imaps.port&quot;, port);



    //Get the SMTP session
    Session emailSession = Session.getInstance(properties);

    try {
        emailStore = (IMAPStore) emailSession.getStore();
        emailStore.connect(username, password);

        emailFolder = emailStore.getFolder(&quot;INBOX&quot;);
        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 = &quot;+&quot; 
  message = &quot;&quot;
  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(&quot;BAD&quot;);
}

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(
              &quot;&lt;username&gt;&quot;, &quot;&lt;password&gt;&quot;);
        }
     });

答案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:

   &lt;groupId&gt;org.apache.geronimo.specs&lt;/groupId&gt;
   &lt;artifactId&gt;geronimo-javamail_1.4_mail&lt;/artifactId&gt;

   &lt;groupId&gt;org.apache.geronimo.javamail&lt;/groupId&gt;
   &lt;artifactId&gt;geronimo-javamail_1.4_mail&lt;/artifactId&gt;

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

            &lt;exclusion&gt;
                &lt;groupId&gt;org.apache.geronimo.specs&lt;/groupId&gt;
                &lt;artifactId&gt;geronimo-javamail_1.4_mail&lt;/artifactId&gt;
            &lt;/exclusion&gt;
            &lt;exclusion&gt;
                &lt;groupId&gt;org.apache.geronimo.javamail&lt;/groupId&gt;
                &lt;artifactId&gt;geronimo-javamail_1.4_mail&lt;/artifactId&gt;
            &lt;/exclusion&gt;

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.

huangapple
  • 本文由 发表于 2023年7月17日 14:52:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/76702082.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定