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

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

我正在创建一个软件,通过一个继电器控制我的灯光,它通过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次尝试后,我出现了错误。有办法解决这个问题吗?

  1. import java.io.IOException;
  2. import java.util.Properties;
  3. import java.util.concurrent.TimeUnit;
  4. import javax.mail.Folder;
  5. import javax.mail.Message;
  6. import javax.mail.MessagingException;
  7. import javax.mail.NoSuchProviderException;
  8. import javax.mail.Session;
  9. import javax.mail.Store;
  10. public class GetEmails {
  11. public static void on() throws IOException {
  12. String command = "python on.py";
  13. Process process = Runtime.getRuntime().exec(command);
  14. // deal with OutputStream to send inputs
  15. process.getOutputStream();
  16. // deal with InputStream to get ordinary outputs
  17. process.getInputStream();
  18. // deal with ErrorStream to get error outputs
  19. process.getErrorStream();
  20. }
  21. public static void off() throws IOException{
  22. String command = "python off.py";
  23. Process process = Runtime.getRuntime().exec(command);
  24. // deal with OutputStream to send inputs
  25. process.getOutputStream();
  26. // deal with InputStream to get ordinary outputs
  27. process.getInputStream();
  28. // deal with ErrorStream to get error outputs
  29. process.getErrorStream();
  30. }
  31. public static void check(String host, String storeType, String user,
  32. String password)
  33. {
  34. try {
  35. //create properties field
  36. Properties properties = new Properties();
  37. properties.put("mail.pop3.host", host);
  38. properties.put("mail.pop3.port", "995");
  39. properties.put("mail.pop3.starttls.enable", "true");
  40. Session emailSession = Session.getDefaultInstance(properties);
  41. //create the POP3 store object and connect with the pop server
  42. Store store = emailSession.getStore("pop3s");
  43. store.connect(host, user, password);
  44. boolean power = true;
  45. while(true){
  46. //create the folder object and open it
  47. Folder emailFolder = store.getFolder("INBOX");
  48. emailFolder.open(Folder.READ_ONLY);
  49. // retrieve the messages from the folder in an array
  50. Message[] messages = emailFolder.getMessages();
  51. int i = messages.length - 1;
  52. Message message = messages[i];
  53. String subject = message.getSubject();
  54. // System.out.println(subject);
  55. if(subject.equals("+myRoom") && power == false){
  56. System.out.println("Light on");
  57. // on();
  58. power = true;
  59. System.out.println("done");
  60. }
  61. else if (subject.equals("-myRoom") && power == true){
  62. System.out.println("Light Off");
  63. // off();
  64. power = false;
  65. System.out.println("done");
  66. }
  67. else{
  68. continue;
  69. }
  70. store.close();
  71. TimeUnit.SECONDS.sleep(1);
  72. store.connect(host, user, password);
  73. }
  74. } catch (NoSuchProviderException e) {
  75. e.printStackTrace();
  76. } catch (MessagingException e) {
  77. e.printStackTrace();
  78. } catch (Exception e) {
  79. e.printStackTrace();
  80. }
  81. }
  82. public static void main(String[] args) {
  83. String host = "EMAIL SERVER";// change accordingly
  84. String mailStoreType = "pop3";
  85. String username = "EMAIL";// change accordingly
  86. String password = "PASSWORD";// change accordingly
  87. check(host, mailStoreType, username, password);
  88. }
  89. }

暂无答案!

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

相关问题