IMAP email channel adapter throw exception “A5 BAD

2019-07-07 05:43发布

问题:

I am using imap-idle-channel-adapter to receive emails in spring integration:

A5 SEARCH NOT (ANSWERED) NOT (DELETED) NOT (SEEN) NOT (FLAGGED) ALL
A5 BAD invalid command or parameters

seems the imap server regard above commands as invalid, anyone can tell me how to fix ?

My debug messages:

DEBUG: successfully loaded resource: /META-INF/javamail.default.address.map
DEBUG: getProvider() returning javax.mail.Provider[STORE,imaps,com.sun.mail.imap.IMAPSSLStore,Oracle]
DEBUG IMAPS: mail.imap.fetchsize: 16384
DEBUG IMAPS: mail.imap.ignorebodystructuresize: false
DEBUG IMAPS: mail.imap.statuscachetimeout: 1000
DEBUG IMAPS: mail.imap.appendbuffersize: -1
DEBUG IMAPS: mail.imap.minidletime: 10
DEBUG IMAPS: closeFoldersOnStoreFailure
DEBUG IMAPS: trying to connect to host "imap.mxhichina.com", port 993, isSSL true
* OK AliYun IMAP Server Ready(10.177.11.50)
A0 CAPABILITY
* CAPABILITY IMAP4rev1 IDLE XLIST UIDPLUS ID SASL-IR AUTH=XOAUTH AUTH=EXTERNAL
A0 OK CAPABILITY completed
DEBUG IMAPS: AUTH: XOAUTH
DEBUG IMAPS: AUTH: EXTERNAL
DEBUG IMAPS: protocolConnect login, host=imap.mxhichina.com, user=*****, password=<non-null>
DEBUG IMAPS: mechanism PLAIN not supported by server
DEBUG IMAPS: mechanism LOGIN not supported by server
DEBUG IMAPS: mechanism NTLM not supported by server
DEBUG IMAPS: mechanism XOAUTH2 disabled by property: mail.imaps.auth.xoauth2.disable
DEBUG IMAPS: LOGIN command trace suppressed
DEBUG IMAPS: LOGIN command result: A1 OK LOGIN completed
A2 CAPABILITY
* CAPABILITY IMAP4rev1 IDLE XLIST UIDPLUS ID SASL-IR AUTH=XOAUTH AUTH=EXTERNAL
A2 OK CAPABILITY completed
DEBUG IMAPS: AUTH: XOAUTH
DEBUG IMAPS: AUTH: EXTERNAL
A3 LIST "" INBOX
* LIST () "/" "INBOX"
A3 OK LIST completed
DEBUG IMAPS: connection available -- size: 1
A4 SELECT INBOX
* 2 EXISTS
* 0 RECENT
* OK [UNSEEN 0]
* OK [UIDNEXT 25] Predicted next UID.
* OK [UIDVALIDITY 2] UIDs valid.
* FLAGS (\Answered \Seen \Deleted \Draft \Flagged)
* OK [PERMANENTFLAGS (\Answered \Seen \Deleted \Draft \Flagged)] Limited.
A4 OK [READ-WRITE] SELECT completed
A5 SEARCH NOT (ANSWERED) NOT (DELETED) NOT (SEEN) NOT (FLAGGED) ALL
A5 BAD invalid command or parameters
13:41:00.557 WARN  [task-scheduler-1][org.springframework.integration.mail.ImapIdleChannelAdapter] error occurred in idle task
javax.mail.MessagingException: A5 BAD invalid command or parameters;

my configurations

<int-mail:imap-idle-channel-adapter id="mailAdapter"
                                    store-uri="imaps://${username}:${password}@imap.mxhichina.com/INBOX"
                                    channel="inboundChannel"
                                    auto-startup="true"
                                    should-delete-messages="true"
                                    should-mark-messages-as-read="true"
                                    java-mail-properties="javaMailProperties"/>

    <util:properties id="javaMailProperties">
        <prop key="mail.imap.socketFactory.class">javax.net.ssl.SSLSocketFactory</prop>
        <prop key="mail.imap.socketFactory.fallback">false</prop>
        <prop key="mail.store.protocol">imaps</prop>
        <prop key="mail.transport.protocol">smtps</prop>
        <prop key="mail.smtps.auth">true</prop>
        <prop key="mail.debug">true</prop>
        <prop key="mail.smtp.starttls.enable">true</prop>
    </util:properties>

回答1:

NOT(*****) command is not supported by my mail server, so following command doesn't work

A5 SEARCH NOT (ANSWERED) NOT (DELETED) NOT (SEEN) NOT (FLAGGED) ALL

So I changed DefaultSearchTermStrategy to my own one, which named UnseenSearchTermStrategy

@Component
public class UnseenSearchTermStrategy  implements SearchTermStrategy {
    UnseenSearchTermStrategy(){
        super();
    }
    @Override
    public SearchTerm generateSearchTerm(Flags flags, Folder folder) {
        return new FlagTerm(new Flags(Flags.Flag.SEEN), false);
    }
}

and following commands will be executed on mail server and works well

A5 SEARCH UNSEEN ALL

Related topics in stackoverflow Using spring integration IMAP adapter, how to fetch an email which was marked "unread" manually?