这是Java mail listener using Spring Integration : mail isn't received by multiple app instances的延续。我正在使用下面的ImapMailReceiver代码:
@Bean
public ImapMailReceiver receiver() {
ImapMailReceiver receiver = new ImapMailReceiver(
"imaps://username:[email protected]/INBOX");
receiver.setShouldMarkMessagesAsRead(false);
receiver.setSimpleContent(true);
receiver.setUserFlag("test-flag");
//receiver.setJavaMailProperties(javaMailProperties());
return receiver;
}
我的应用程序已部署在开发和阶段服务器中。根据调试日志:This email server does not support RECENT or USER flags
。因此,无论我通过上述代码设置的任何用户标志都没有用,并且只有我的应用程序实例(dev或stage)而不是所有实例都将接收邮件,因此邮件将被一个实例丢弃。如何使其工作,以便我所有的应用程序实例都收到新的电子邮件?我应该设置任何javamail属性吗?如何运作
UPDATE在自定义searchTermStrategy下使用。对于新消息的每个轮询列表,将接收一组旧消息。尚未在多个应用程序实例上进行测试。
private class CustomSearchTermStrategy implements SearchTermStrategy {
CustomSearchTermStrategy() {
}
@Override
public SearchTerm generateSearchTerm(Flags supportedFlags, Folder folder) {
SearchTerm searchTerm = null;
boolean recentFlagSupported = false;
if (supportedFlags != null) {
recentFlagSupported = supportedFlags.contains(Flags.Flag.RECENT);
if (recentFlagSupported) {
searchTerm = new FlagTerm(new Flags(Flags.Flag.RECENT), true);
}
if (supportedFlags.contains(Flags.Flag.ANSWERED)) {
NotTerm notAnswered = new NotTerm(new FlagTerm(new Flags(Flags.Flag.ANSWERED), true));
if (searchTerm == null) {
searchTerm = notAnswered;
} else {
searchTerm = new AndTerm(searchTerm, notAnswered);
}
}
if (supportedFlags.contains(Flags.Flag.DELETED)) {
NotTerm notDeleted = new NotTerm(new FlagTerm(new Flags(Flags.Flag.DELETED), true));
if (searchTerm == null) {
searchTerm = notDeleted;
} else {
searchTerm = new AndTerm(searchTerm, notDeleted);
}
}
if (supportedFlags.contains(Flags.Flag.SEEN)) {
NotTerm notSeen = new NotTerm(new FlagTerm(new Flags(Flags.Flag.SEEN), true));
if (searchTerm == null) {
searchTerm = notSeen;
} else {
searchTerm = new AndTerm(searchTerm, notSeen);
}
}
}
// if (!recentFlagSupported) {
// searchTerm = applyTermsWhenNoRecentFlag(folder, searchTerm);
// }
return searchTerm;
}
}
最简单的解决方案是为每个环境使用不同的帐户(将邮件从一个帐户转发到另一个帐户,以便两者都可以接收它们。]
如果不可能,则问题在于FLAGGED
标志,该标志是无条件设置的,并且不包含在默认搜索词中。
不幸的是,设置该标志的方法是私有的,因此您无法更改该行为。
我认为唯一的解决方案是不包含NOT (FLAGGED)
并在本地保持状态以忽略您已阅读的消息的自定义搜索策略。