Merge branch 'main' into lang_de

This commit is contained in:
2022-04-30 13:12:57 +02:00
9 changed files with 187 additions and 22 deletions

View File

@@ -6,6 +6,8 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.mail.*;
import java.time.Duration;
import java.util.Date;
import java.util.HashSet;
import java.util.Properties;
@@ -72,10 +74,8 @@ public class ImapClient {
}
private void handleMessages() throws MessagingException {
LOG.debug("Lese E-Mail von {}:",username);
if (!inbox.isOpen()){
inbox.open(IMAPFolder.READ_WRITE);
}
LOG.debug("Reading email of {}:",username);
if (!inbox.isOpen()) inbox.open(IMAPFolder.READ_WRITE);
for (Message message : inbox.getMessages()){
if (message.isSet(Flags.Flag.SEEN)) continue;
handle(message);
@@ -145,6 +145,27 @@ public class ImapClient {
return this;
}
public void dropMailsOlderThan(Integer holdTime) throws MessagingException {
var now = new Date();
if (holdTime == null) return;
LOG.info("Removing mails older than {} days:",holdTime);
if (!inbox.isOpen()) inbox.open(IMAPFolder.READ_WRITE);
for (Message message : inbox.getMessages()){
Date receivedDate = message.getReceivedDate();
Duration duration = Duration.between(receivedDate.toInstant(),now.toInstant());
var days = duration.toDays();
LOG.info("Message {} is {} days old!");
if (days > holdTime){
LOG.info("…removing");
Folder folder = message.getFolder();
if (!folder.isOpen()) folder.open(Folder.READ_WRITE);
message.setFlag(Flags.Flag.DELETED, true);
}
}
inbox.expunge();
}
public String host(){
return host;
}