rnwood / smtp4dev

smtp4dev - the fake smtp email server for development and testing

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[Bug] IMAP does not support SEARCH command (A5 BAD Error in arguments when searching for unseen flag term)

usersina opened this issue · comments

I have a small java 11 app that connects to an IMAP server and tries to find the unread messages using unseenFlagTerm as shown in the code below:

// This is simplified a bit but the point should be clear

import java.util.Properties;
import javax.mail.Session;
import javax.mail.Store;
import javax.mail.Flags;
import javax.mail.search.FlagTerm;


public Properties getProperties() {
    log.info("Setting up the mail config");
    properties = new Properties();
    properties.put("mail.smtp.host", "localhost");
    properties.put("mail.smtp.port", 25);
}

public static void main(String args[]) {
    Session session = Session.getInstance(getProperties());
    store = session.getStore("imap");
    store.connect("localhost", "", ""); // connet to IMAP server
    emailInbox = store.getFolder("INBOX");
    emailInbox.open(Folder.READ_WRITE);
    Flags seen = new Flags(Flags.Flag.SEEN);
    FlagTerm unseenFlagTerm = new FlagTerm(seen, false);
    Message[] messages = emailInbox.search(unseenFlagTerm); # This throws an error
}

It successfully connects, but the .search method crashes with

javax.mail.MessagingException: A5 BAD Error in arguments.
        at com.sun.mail.imap.IMAPFolder.search(IMAPFolder.java:2393) ~[javax.mail-1.6.2.jar:1.6.2]
       ...
Caused by: com.sun.mail.iap.BadCommandException: A5 BAD Error in arguments.
        at com.sun.mail.iap.Protocol.handleResult(Protocol.java:439) ~[javax.mail-1.6.2.jar:1.6.2]
        at com.sun.mail.imap.protocol.IMAPProtocol.issueSearch(IMAPProtocol.java:2596) ~[javax.mail-1.6.2.jar:1.6.2]
        at com.sun.mail.imap.protocol.IMAPProtocol.search(IMAPProtocol.java:2501) ~[javax.mail-1.6.2.jar:1.6.2]
        at com.sun.mail.imap.protocol.IMAPProtocol.search(IMAPProtocol.java:2486) ~[javax.mail-1.6.2.jar:1.6.2]
        at com.sun.mail.imap.IMAPFolder.search(IMAPFolder.java:2375) ~[javax.mail-1.6.2.jar:1.6.2]
        ... 14 common frames omitted

Which appears to be a ProtocolException thrown by a BadCommandException.

The reason I opened this issue is because a similar thread on stackoverflow suggested that the server is broken but I'm not sure.

I also tried digging a bit deeper with a debugger but I don't know what would be a useful information to have. I can share when asked.

FWIW, I setup smtp4dev as follows:

version: "3"
services:
  smtp4dev:
    image: rnwood/smtp4dev
    ports:
      - '5000:80' # Web interface
      - '25:25' # SMTP server
      - '143:143' # IMAP server

Same error using MailKit:

The IMAP server replied to the 'SEARCH' command with a 'BAD' response: Error in arguments.
when
_IMAPClient.Inbox.SearchAsync(MailKit.Search.SearchQuery.All)

GetMessageAsync and FetchAsync execute correctly

IMAP SEARCH support has been implemented in PR #1448.

This covers the following keys:
ALL
( ) groups for AND
OR
SUBJECT
TO
FROM
SEEN
UNSEEN

In addition, the IMAP server will now correctly reply with NO instead of BAD if the keys the client sends are not supported.