javajakarta-mailpop3

How to manipulate a POP3 message?


The mail store used is POP3 :

mail.storeType=pop3s
mail.host=pop.moov.mg
mail.port=995
mail.username=somemail
mail.password=somepassword
mail.auth=true
mail.ssl.trust=*

I want to retrieve e-mails, then mark them, but I cannot mark them :

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;
    }
}

private POP3Store emailstore = null;
emailstore = getEmailStore();
Folder emailFolder = emailstore.getFolder("INBOX");
emailFolder.open(Folder.READ_WRITE);
Message[] messages = emailFolder.getMessages();
if (messages != null) {
  for (Message message : messages) {
    if (message.getHeader("eeeee") == null || !Arrays.asList(message.getHeader("eeeee")).contains("ticket created")) {
      try {
         mailService.createTicket(message);
         mailService.mark(message);
      } catch (Exception ex) {
         throw ex;
      }
    }
  }
}
System.out.println("FIN Receving");
emailFolder.close(true);

code of mailService.mark :

public void mark(Message message) throws MessagingException {
        try {
            message.setHeader("eeeee", "ticket created");
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
    }

At runtime I get IllegalWriteException POP3 messages are read-only

So how to manipulate the message ?


Solution

  • POP3 is an old protocol for retrieving messages. It is meant to be used to download messages with the idea that the retrieved messages are immediately deleted from the server, similarly to how a physical mailbox works.

    It simply doesn't offer mechanism for manipulating messages.