I flag a message to be deleted after a treatment :
...
import javax.mail.*;
...
public void delete(Message message) throws MessagingException {
try {
message.setFlag(Flags.Flag.DELETED, true);
} catch (Exception e) {
e.printStackTrace();
throw e;
}
}
...
Then I want to get messages :
...
import com.sun.mail.pop3.POP3Store;
import java.util.Properties;
...
@Value("${mail.host}")
private String host;
@Value("${mail.storeType}")
private String storeType;
@Value("${mail.port}")
private int port;
@Value("${mail.username}")
private String username;
@Value("${mail.password}")
private String password;
@Value("${mail.auth}")
private boolean auth;
@Value("${mail.ssl.trust}")
private String sss_trust;
private POP3Store emailstore = null;
private boolean start = true;
...
public POP3Store getEmailStore() throws Exception {
Properties properties = new Properties();
properties.setProperty("mail.store.protocol", storeType);
properties.setProperty("mail." + storeType + ".host", host);
properties.setProperty("mail." + storeType + ".port", String.valueOf(port));
properties.setProperty("mail." + storeType + ".auth", String.valueOf(auth));
properties.setProperty("mail." + storeType + ".socketFactory.class", "javax.net.ssl.SSLSocketFactory");
properties.setProperty("mail." + storeType + ".ssl.trust", sss_trust);
try {
Session emailSession = Session.getDefaultInstance(properties);
POP3Store emailStore = (POP3Store) emailSession.getStore(storeType);
return emailStore;
} catch (NoSuchProviderException e) {
e.printStackTrace();
throw e;
}
}
Folder emailFolder = emailstore.getFolder("INBOX");
emailFolder.open(Folder.READ_ONLY);
Message[] messages = emailFolder.getMessages(); // how to get messages that are not flagged as deleted ?
if (messages != null) {
for (Message message : messages) {
try {
mailService.createTicket(message);
mailService.delete(message);
} catch (Exception ex) {
throw ex;
}
}
}
emailFolder.close(true);
As I commented I want to get messages that are not flagged deleted. How to do that ?
Based on Message
class, you can do something like this:
for (Message message : messages) {
if (!message.getFlags().contains(Flags.Flag.DELETED)) { // This will hopefully help
try {
mailService.createTicket(message);
mailService.delete(message);
} catch (Exception ex) {
throw ex;
}
}
}