jodd.mail.ReceivedEmails Maven / Gradle / Ivy
package jodd.mail;
import jakarta.mail.FetchProfile;
import jakarta.mail.Flags;
import jakarta.mail.Message;
import jakarta.mail.MessagingException;
import java.util.function.Consumer;
public class ReceivedEmails {
private final Message[] messages;
private final boolean envelope;
private final ReceiveMailSession session;
private final Flags flagsToSet;
private final Flags flagsToUnset;
private final Consumer processedMessageConsumer;
public ReceivedEmails(
final ReceiveMailSession session,
final Message[] messages,
final Flags flagsToSet,
final Flags flagsToUnset,
final boolean envelope,
final Consumer processedMessageConsumer
) {
this.session = session;
this.messages = messages;
this.flagsToSet = flagsToSet;
this.flagsToUnset = flagsToUnset;
this.envelope = envelope;
this.processedMessageConsumer = processedMessageConsumer;
}
public ReceivedEmail[] fetch() {
try {
return _fetch();
}
catch (final MessagingException ex) {
throw new MailException("Failed to process fetched messages", ex);
}
}
private ReceivedEmail[] _fetch() throws MessagingException {
if (messages.length == 0) {
return ReceivedEmail.EMPTY_ARRAY;
}
if (envelope) {
final FetchProfile fetchProfile = new FetchProfile();
fetchProfile.add(FetchProfile.Item.ENVELOPE);
fetchProfile.add(FetchProfile.Item.FLAGS);
session.folder.fetch(messages, fetchProfile);
}
final ReceivedEmail[] emails = new ReceivedEmail[messages.length];
for (int i = 0; i < messages.length; i++) {
final Message msg = messages[i];
// we need to parse message BEFORE flags are set!
emails[i] = new ReceivedEmail(msg, envelope, session.attachmentStorage);
if (!EmailUtil.isEmptyFlags(flagsToSet)) {
emails[i].flags(flagsToSet);
msg.setFlags(flagsToSet, true);
}
if (!EmailUtil.isEmptyFlags(flagsToUnset)) {
emails[i].flags().remove(flagsToUnset);
msg.setFlags(flagsToUnset, false);
}
if (EmailUtil.isEmptyFlags(flagsToSet) && !emails[i].isSeen()) {
msg.setFlag(Flags.Flag.SEEN, false);
}
}
if (processedMessageConsumer != null) {
processedMessageConsumer.accept(messages);
}
// if messages were marked to be deleted, we need to expunge the folder
if (!EmailUtil.isEmptyFlags(flagsToSet)) {
if (flagsToSet.contains(Flags.Flag.DELETED)) {
session.folder.expunge();
}
}
return emails;
}
public static ReceivedEmails empty() {
return new ReceivedEmails(null, new Message[0], null, null, false, null);
}
}