如何修复java mail在while循环中不显示新电子邮件的问题

rkue9o1l  于 2021-08-20  发布在  Java
关注(0)|答案(1)|浏览(329)

我正在制作一个脚本,用电子邮件打开和关闭我的灯,除了它不会更新到最新的电子邮件,我不知道为什么之外,一切都正常。我在网上找不到任何东西,也没有尝试任何东西,因为我完全迷路了。顺便说一句,脚本第一次运行时会运行最新的电子邮件,但之后不会再运行
这是我的代码,请帮忙
根据我的理解,while循环应该工作的方式是获取一个主题,然后检查它是否符合我要求它查找的内容,然后检查它是否符合我的要求。它转到顶部,再次检查最近的主题,然后检查它是否符合我要找的内容,然后一次又一次地执行

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

    }

}
de90aj5v

de90aj5v1#

问题在于,您只在while循环之前调用getmessages(),然后在while循环中反复不断地检查同一组下载的消息。
您需要更改while循环以下载最新的(一组)消息。
我还要指出,您的代码没有考虑到接收非命令消息的可能性,这些消息可能会将最新的命令消息推送到“not the latest”槽中(例如,最新的命令消息可能有索引) messages.length - 2 而非命令消息具有 messages.length - 1 索引)也不考虑由于网络中断或服务器断开连接(无论出于何种原因,这种情况随时都会发生)而导致断开连接的可能性。

相关问题