问题
This is a continuation of Java mail listener using Spring Integration : mail isn't received by multiple app instances . I'm using below ImapMailReceiver code :
@Bean
public ImapMailReceiver receiver() {
ImapMailReceiver receiver = new ImapMailReceiver(
"imaps://username:pwd@mail.company.com/INBOX");
receiver.setShouldMarkMessagesAsRead(false);
receiver.setSimpleContent(true);
receiver.setUserFlag("test-flag");
//receiver.setJavaMailProperties(javaMailProperties());
return receiver;
}
My application has been deployed in dev and stage servers.As per the debug logs : This email server does not support RECENT or USER flags
. Hence whatever userflag i'm setting via above code isn't useful and mails will be received by only once instance of my application (either dev or stage ) and not all instances.So mails are getting dropped by one instance. How to make it work so that all of my application instances receives new emails? Should i set any javamail properties ? How to make it work
UPDATE used below custom searchTermStrategy . For every poll list of new messages + set of old messages will be received . Haven't tested on multiple application instances yet.
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;
}
}
回答1:
The simplest solution would be to use different accounts for each environment (forward mails from one to the other so both get them).
If that's not possible, the issue is with the FLAGGED
flag, which is unconditionally set and excluded in the default search term.
Unfortunately, the method that sets that flag is private so you can't change that behavior.
I think the only solution is a custom search strategy that does not include NOT (FLAGGED)
and keep state locally to ignore messages that you have already read.
来源:https://stackoverflow.com/questions/62214324/java-mail-listner-this-email-server-does-not-support-recent-or-user-flags