working on dropping old mails
This commit is contained in:
@@ -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;
|
||||
|
||||
@@ -73,9 +75,7 @@ public class ImapClient {
|
||||
|
||||
private void handleMessages() throws MessagingException {
|
||||
LOG.debug("Reading email of {}:",username);
|
||||
if (!inbox.isOpen()){
|
||||
inbox.open(IMAPFolder.READ_WRITE);
|
||||
}
|
||||
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,26 @@ public class ImapClient {
|
||||
return this;
|
||||
}
|
||||
|
||||
public void dropMailsOlderThan(Integer holdTime) throws MessagingException {
|
||||
var now = new Date();
|
||||
if (holdTime == null) return;
|
||||
LOG.debug("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){
|
||||
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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user