javaemailwhile-loopjakarta-mailpop3

How to fix Java Mail not displaying new emails in while loop


I'm making a script to turn on and off my lights using email and EVERYTHING works besides the fact that it won't update to the newest email and I don't know why. I couldn't find anything online, and I didn't try anything because I'm completely lost. BTW for more info the first time the script runs it runs the newest email but it doesn't do it again after that

This is my code please help

to my understanding the way the while loop should work is get a subject, then check to see if it matches what im asking it to look for then weather it is or isnt it goes about to the top and checks the most recent subject again then checing if its what im looking for then doing it again and again and again

import java.util.Properties;

import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.NoSuchProviderException;
import javax.mail.Session;
import javax.mail.Store;


public class GetEmails {

    public static void on(){
        SSH on = new SSH();
        on.command = "python3 on.py";
        on.run();
    }

    public static void off(){
        SSH off = new SSH();
        off.command = "python3 off.py";
        off.run();
    }

    public static void check(String host, String storeType, String user,
                             String password)
    {
        try {

            //create properties field
            Properties properties = new Properties();

            properties.put("mail.pop3.host", host);
            properties.put("mail.pop3.port", "995");
            properties.put("mail.pop3.starttls.enable", "true");
            Session emailSession = Session.getDefaultInstance(properties);

            //create the POP3 store object and connect with the pop server
            Store store = emailSession.getStore("pop3s");

            store.connect(host, user, password);

            //create the folder object and open it
            Folder emailFolder = store.getFolder("INBOX");
            emailFolder.open(Folder.READ_ONLY);

            // retrieve the messages from the folder in an array
            Message[] messages = emailFolder.getMessages();


            boolean power = true;
            while(true){
                int i = messages.length - 1;
                Message message = messages[i];
                String subject = message.getSubject();

//                System.out.println(subject);
                if(subject.equals("+myRoom") & power == false){
                    on();
                    power = true;
                    System.out.println("Light on");
                }
                else if (subject.equals("-myRoom") & power == true){
                    off();
                    power = false;
                    System.out.println("Light Off");

                }
                else{
                    continue;
                }
            }


        } catch (NoSuchProviderException e) {
            e.printStackTrace();
        } catch (MessagingException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {

        String host = "pop.gmail.com";// change accordingly
        String mailStoreType = "pop3";
        String username = "EMAIL@gmail.com";// change accordingly
        String password = "PASSWORD";// change accordingly

        check(host, mailStoreType, username, password);

    }

}

Solution

  • The problem is that you only call getMessages() before the while loop and then continuously check the same set of downloaded messages over and over again in the while-loop.

    You need to change the while-loop to download the latest (set of) messages.

    I would also point out that your code does not account for the possibility of receiving non-command messages that might push the latest command message into the "not the latest" slot (e.g. latest command message might have index messages.length - 2 while the non-command message has the messages.length - 1 index) nor does it account for the possibility of getting disconnected due to network outages or the server dropping your connection (which happens all the time for any number of reasons).