'How do I read emails from a Junk Folder using a java program

The program below uses POP3 to read emails and save attachments to a specific folder. However, when the email is marked as Spam, its is unable to read it. How can I adjust the program so that it can read both the inbox and SPAM/JUNK. //----------------------SOME CODE REMOVED--------------------------

    public class EmailService{

    @Value("${mail.pop.port}")   
    private String POP3_PORT;

   @Value("${mail.pop.server}")
   private String POP3_SERVER; 
 
  @Override
  public List<String> downloadEmailAttachments(final String mailBoxEmail, final String mailboxPassword,final String saveLocation, final String[] fileExtensions)
            throws MessagingException { 

Properties properties = setMailServerProperties(POP3_SERVER, POP3_PORT); 
Store store = setSessionStoreProperties(mailBoxEmail, mailboxPassword, properties); 
List<String> attachmentsAll = new ArrayList<>();
Folder inbox = store.getFolder("INBOX"); 
Folder spam = store.getFolder("SPAM"); 
getEmailAttachments(attachmentsAll, inbox, saveLocation, fileExtensions); 
getEmailAttachments(attachmentsAll, spam, saveLocation, fileExtensions); 
store.close();
return attachmentsAll; 
} 

private void getEmailAttachments(List<String> attachmentsAll, Folder folder, final String saveLocation, final String[] fileExtensions) {
        try {

            Flags seen = new Flags(Flags.Flag.RECENT);
            FlagTerm unseenFlagTerm = new FlagTerm(seen, false);
            folder.open(Folder.READ_WRITE);
            Message[] messages = folder.search(new FlagTerm(new Flags(Flags.Flag.SEEN), false));
            int messagesCount = messages.length;
            List<String> attachments = new ArrayList<>();
            if (messagesCount > 0) {
                log.debug("Emails found in {} : {}", folder.getName(), messagesCount);
                for (Message message : messages) {
                    try {
                        Address[] fromAddress = message.getFrom();
                        String from = fromAddress[0].toString();
                        String subject = message.getSubject();
                        String sentDate = message.getSentDate().toString();
                        log.debug("Received a message from {}: subject: {}, date {}", from, subject, sentDate);
                        boolean hasAttachment = hasAttachments(message);
                        if (hasAttachment) {
                            attachments = downloadAttachments(message, saveLocation, fileExtensions);
                            if (!attachments.isEmpty()) {
                                attachmentsAll.addAll(attachments);
                            }
                        }
                    } catch (Exception e) {
                        log.error("Failed to read/delete message {}", e.getMessage());
                        log.debug("Error :", e);
                    } finally {
                        //Finally, delete the message you just read
                        message.setFlag(Flags.Flag.DELETED, true);
                    }
                }
            }
            folder.close(true);
        } catch (MessagingException e) {
            log.error("Reading folder exception  {}", e.getMessage());
        }

    }

}

//---------------------------------------------------------------------------------



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source