首页 > 解决方案 > 如何阻止java邮件打开这么多连接

问题描述

我正在创建软件来通过继电器控制我的灯,它通过 SSH 与我的树莓派对话,但要从我制作它的任何地方获得控制,所以这个 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);

    }

}

标签: javaemailwhile-loopjakarta-mail

解决方案


推荐阅读