Spring 与 Outlook 集成邮件入站重复

Spring Integration Mail inbound duplication with Outlook

我目前正在开展一个项目,该项目将轮询电子邮件收件箱以获取每日报告。

当访问 Amazon Workmail 上托管的电子邮件用户时,我有一个使用 spring-integration-mail 和 imapInboundAdapter 的有效实施。

当收件箱中有一封未读且未标记的电子邮件时,它会收到该电子邮件并正确地仅发出一条消息。

但是,当我将电子邮件服务器更改为由 outlook 托管的服务器时,我反而在同一个投票中收到了两次电子邮件。

据我所知,电子邮件的可见/标志状态似乎在 outlook 上第二次尝试轮询时才更新,但在 workmail 上第一次尝试时更新。

第二次尝试 outlook 检索到一封本应已处理的电子邮件。

IntegrationFlows
    .from(
        Mail.imapInboundAdapter(format("imaps://%s:%s/INBOX", source.getHost(), source.getPort()))
            .javaMailAuthenticator(authenticator)
            .maxFetchSize(10),
        e -> e.poller(... maxMessagesPerPoll = -1, cronTrigger = "*/15 * * ? * *" ...)
    ).log(INFO, m -> "Received email: " + m)

这是访问工作邮件时发生的日志记录:

com.sun.mail.imap.connectionpool         : getStoreProtocol() - connection available -- size: 1
com.sun.mail.imap.connectionpool         : getStoreProtocol() -- storeConnectionInUse
com.sun.mail.imap                        : IMAPProtocol noop
com.sun.mail.imap.connectionpool         : releaseStoreProtocol()
com.sun.mail.imap.connectionpool         : getStoreProtocol() borrowing a connection
com.sun.mail.imap.connectionpool         : getStoreProtocol() - connection available -- size: 1
com.sun.mail.imap.connectionpool         : getStoreProtocol() -- storeConnectionInUse
com.sun.mail.imap.connectionpool         : releaseFolderStoreProtocol()
o.s.integration.mail.ImapMailReceiver    : opening folder [imaps://[SNIPPED]@imap.mail.us-west-2.awsapps.com:993/INBOX]
com.sun.mail.imap                        : connection available -- size: 1
com.sun.mail.imap.messagecache           : create cache of size 8
o.s.integration.mail.ImapMailReceiver    : attempting to receive mail from folder [INBOX]
o.s.integration.mail.ImapMailReceiver    : This email server does not support RECENT or USER flags. System flag 'Flag.FLAGGED' will be used to prevent duplicates during email fetch.
com.sun.mail.imap.messagecache           : create message number 8
o.s.integration.mail.ImapMailReceiver    : found 1 new messages
o.s.integration.mail.ImapMailReceiver    : Received 1 messages
o.s.integration.mail.ImapMailReceiver    : USER flags are not supported by this mail server. Flagging message with system flag
com.sun.mail.imap.connectionpool         : connection pool current size: 0   pool size: 1
com.sun.mail.imap.connectionpool         : connection pool current size: 0   pool size: 1
com.sun.mail.imap                        : added an Authenticated connection -- size: 1
o.s.i.mail.MailReceivingMessageSource    : received mail message [org.springframework.integration.mail.AbstractMailReceiver$IntegrationMimeMessage@a101a27]
o.s.integration.handler.LoggingHandler   : Received email: GenericMessage [payload=org.springframework.integration.mail.AbstractMailReceiver$IntegrationMimeMessage@a101a27, headers={id=20fb1886-eeda-c1e2-7ce0-8a9aae4f7ebc, timestamp=1624590243455}]
com.sun.mail.imap.connectionpool         : getStoreProtocol() - connection available -- size: 1
com.sun.mail.imap.connectionpool         : getStoreProtocol() -- storeConnectionInUse
com.sun.mail.imap                        : IMAPProtocol noop
com.sun.mail.imap.connectionpool         : releaseStoreProtocol()
com.sun.mail.imap.connectionpool         : getStoreProtocol() borrowing a connection
com.sun.mail.imap.connectionpool         : getStoreProtocol() - connection available -- size: 1
com.sun.mail.imap.connectionpool         : getStoreProtocol() -- storeConnectionInUse
com.sun.mail.imap.connectionpool         : releaseFolderStoreProtocol()
o.s.integration.mail.ImapMailReceiver    : opening folder [imaps://[SNIPPED]@imap.mail.us-west-2.awsapps.com:993/INBOX]
com.sun.mail.imap                        : connection available -- size: 1
com.sun.mail.imap.messagecache           : create cache of size 8
o.s.integration.mail.ImapMailReceiver    : attempting to receive mail from folder [INBOX]
o.s.integration.mail.ImapMailReceiver    : This email server does not support RECENT or USER flags. System flag 'Flag.FLAGGED' will be used to prevent duplicates during email fetch.
o.s.integration.mail.ImapMailReceiver    : found 0 new messages
o.s.integration.mail.ImapMailReceiver    : Received 0 messages
com.sun.mail.imap.connectionpool         : connection pool current size: 0   pool size: 1
com.sun.mail.imap.connectionpool         : connection pool current size: 0   pool size: 1
com.sun.mail.imap                        : added an Authenticated connection -- size: 1

这是点击 outlook 时发生的日志记录:

com.sun.mail.imap.connectionpool         : getStoreProtocol() - connection available -- size: 1
com.sun.mail.imap.connectionpool         : getStoreProtocol() -- storeConnectionInUse
com.sun.mail.imap                        : IMAPProtocol noop
com.sun.mail.imap.connectionpool         : releaseStoreProtocol()
com.sun.mail.imap.connectionpool         : getStoreProtocol() borrowing a connection
com.sun.mail.imap.connectionpool         : getStoreProtocol() - connection available -- size: 1
com.sun.mail.imap.connectionpool         : getStoreProtocol() -- storeConnectionInUse
com.sun.mail.imap.connectionpool         : releaseFolderStoreProtocol()
o.s.integration.mail.ImapMailReceiver    : opening folder [imaps://[SNIPPED]@outlook.office365.com:993/INBOX]
com.sun.mail.imap                        : connection available -- size: 1
com.sun.mail.imap.messagecache           : create cache of size 2
o.s.integration.mail.ImapMailReceiver    : attempting to receive mail from folder [INBOX]
o.s.integration.mail.ImapMailReceiver    : This email server does not support RECENT or USER flags. System flag 'Flag.FLAGGED' will be used to prevent duplicates during email fetch.
com.sun.mail.imap.messagecache           : create message number 2
o.s.integration.mail.ImapMailReceiver    : found 1 new messages
o.s.integration.mail.ImapMailReceiver    : Received 1 messages
o.s.integration.mail.ImapMailReceiver    : USER flags are not supported by this mail server. Flagging message with system flag
com.sun.mail.imap.connectionpool         : connection pool current size: 0   pool size: 1
com.sun.mail.imap.connectionpool         : connection pool current size: 0   pool size: 1
com.sun.mail.imap                        : added an Authenticated connection -- size: 1
o.s.i.mail.MailReceivingMessageSource    : received mail message [org.springframework.integration.mail.AbstractMailReceiver$IntegrationMimeMessage@5dffc30b]
o.s.integration.handler.LoggingHandler   : Received email: GenericMessage [payload=org.springframework.integration.mail.AbstractMailReceiver$IntegrationMimeMessage@5dffc30b, headers={id=9d158e52-d46e-fd82-38b7-b438a4899a1e, timestamp=1624589763164}]
com.sun.mail.imap.connectionpool         : getStoreProtocol() - connection available -- size: 1
com.sun.mail.imap.connectionpool         : getStoreProtocol() -- storeConnectionInUse
com.sun.mail.imap                        : IMAPProtocol noop
com.sun.mail.imap.connectionpool         : releaseStoreProtocol()
com.sun.mail.imap.connectionpool         : getStoreProtocol() borrowing a connection
com.sun.mail.imap.connectionpool         : getStoreProtocol() - connection available -- size: 1
com.sun.mail.imap.connectionpool         : getStoreProtocol() -- storeConnectionInUse
com.sun.mail.imap.connectionpool         : releaseFolderStoreProtocol()
o.s.integration.mail.ImapMailReceiver    : opening folder [imaps://[SNIPPED]@outlook.office365.com:993/INBOX]
com.sun.mail.imap                        : connection available -- size: 1
com.sun.mail.imap.messagecache           : create cache of size 2
o.s.integration.mail.ImapMailReceiver    : attempting to receive mail from folder [INBOX]
o.s.integration.mail.ImapMailReceiver    : This email server does not support RECENT or USER flags. System flag 'Flag.FLAGGED' will be used to prevent duplicates during email fetch.
com.sun.mail.imap.messagecache           : create message number 2
o.s.integration.mail.ImapMailReceiver    : found 1 new messages
o.s.integration.mail.ImapMailReceiver    : Received 1 messages
o.s.integration.mail.ImapMailReceiver    : USER flags are not supported by this mail server. Flagging message with system flag
com.sun.mail.imap.connectionpool         : connection pool current size: 0   pool size: 1
com.sun.mail.imap.connectionpool         : connection pool current size: 0   pool size: 1
com.sun.mail.imap                        : added an Authenticated connection -- size: 1
o.s.i.mail.MailReceivingMessageSource    : received mail message [org.springframework.integration.mail.AbstractMailReceiver$IntegrationMimeMessage@59c04c6c]
o.s.integration.handler.LoggingHandler   : Received email: GenericMessage [payload=org.springframework.integration.mail.AbstractMailReceiver$IntegrationMimeMessage@59c04c6c, headers={id=002e6b83-caf5-86cd-b090-44aa346df119, timestamp=1624589766210}]
com.sun.mail.imap.connectionpool         : getStoreProtocol() - connection available -- size: 1
com.sun.mail.imap.connectionpool         : getStoreProtocol() -- storeConnectionInUse
com.sun.mail.imap                        : IMAPProtocol noop
com.sun.mail.imap.connectionpool         : releaseStoreProtocol()
com.sun.mail.imap.connectionpool         : getStoreProtocol() borrowing a connection
com.sun.mail.imap.connectionpool         : getStoreProtocol() - connection available -- size: 1
com.sun.mail.imap.connectionpool         : getStoreProtocol() -- storeConnectionInUse
com.sun.mail.imap.connectionpool         : releaseFolderStoreProtocol()
o.s.integration.mail.ImapMailReceiver    : opening folder [imaps://[SNIPPED]@outlook.office365.com:993/INBOX]
com.sun.mail.imap                        : connection available -- size: 1
com.sun.mail.imap.messagecache           : create cache of size 2
o.s.integration.mail.ImapMailReceiver    : attempting to receive mail from folder [INBOX]
o.s.integration.mail.ImapMailReceiver    : This email server does not support RECENT or USER flags. System flag 'Flag.FLAGGED' will be used to prevent duplicates during email fetch.
o.s.integration.mail.ImapMailReceiver    : found 0 new messages
o.s.integration.mail.ImapMailReceiver    : Received 0 messages
com.sun.mail.imap.connectionpool         : connection pool current size: 0   pool size: 1
com.sun.mail.imap.connectionpool         : connection pool current size: 0   pool size: 1
com.sun.mail.imap                        : added an Authenticated connection -- size: 1

听起来像 setFlag() 是 Outlook 的异步。您可能会考虑不要一次获取所有消息 - .maxFetchSize(1)。另一种方法是使用自定义 SearchTermStrategyselector 来跳过您已阅读的消息。

问题原来与 windows outlook 邮件客户端直接相关。

如果您在 Outlook 客户端中使用 imap 添加帐户,如果您通过客户端将电子邮件标记为未读(可能与文件夹处理相关),则会导致重复发生。

我不确定确切原因,但将帐户添加为完整的 Outlook 帐户(而不是通过 imap)解决了问题。

否则,您可以在网络浏览器中将电子邮件标记为未读,而不是在 Outlook 客户端中。