如何阻止java mail打开如此多的连接

2mbi3lxu  于 2021-08-20  发布在  Java
关注(0)|答案(0)|浏览(227)

我正在创建一个软件,通过一个继电器控制我的灯光,它通过ssh与我的raspberry pi通信,但要从我制作的任何地方获得控制权,所以这个java代码每1秒检查一次我的电子邮件,看看命令是否在那里 +myRoom , -myRoom 我遇到的问题是,即使我关闭并重新打开连接,它仍然会显示 java.io.EOFException: [SYS/TEMP] Maximum number of connections from user+IP exceeded (mail_max_userip_connections=5) 我知道这是一个服务器和代码问题,因为它可以通过不增加连接限制来解决,但这是一个共享服务器,可能会导致它们停机。现在,正如您在我的代码中看到的,我尝试关闭连接,然后等待1秒钟并重新连接,但在第5次尝试后,我出现了错误。有办法解决这个问题吗?

import java.io.IOException;
import java.util.Properties;
import java.util.concurrent.TimeUnit;

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() throws IOException {
        String command = "python on.py";

        Process process = Runtime.getRuntime().exec(command);

        // deal with OutputStream to send inputs
        process.getOutputStream();

        // deal with InputStream to get ordinary outputs
        process.getInputStream();

        // deal with ErrorStream to get error outputs
        process.getErrorStream();
    }

    public static void off() throws IOException{
        String command = "python off.py";

        Process process = Runtime.getRuntime().exec(command);

        // deal with OutputStream to send inputs
        process.getOutputStream();

        // deal with InputStream to get ordinary outputs
        process.getInputStream();

        // deal with ErrorStream to get error outputs
        process.getErrorStream();
    }

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

            boolean power = true;
            while(true){

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

                int i = messages.length - 1;
                Message message = messages[i];
                String subject = message.getSubject();

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

                }
                else{
                    continue;
                }
                store.close();
                TimeUnit.SECONDS.sleep(1);
                store.connect(host, user, password);
            }

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

    public static void main(String[] args) {

        String host = "EMAIL SERVER";// change accordingly
        String mailStoreType = "pop3";
        String username = "EMAIL";// change accordingly
        String password = "PASSWORD";// change accordingly

        check(host, mailStoreType, username, password);

    }

}

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题