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

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

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

  1. import java.util.Properties;
  2. import javax.mail.Folder;
  3. import javax.mail.Message;
  4. import javax.mail.MessagingException;
  5. import javax.mail.NoSuchProviderException;
  6. import javax.mail.Session;
  7. import javax.mail.Store;
  8. public class GetEmails {
  9. public static void on(){
  10. SSH on = new SSH();
  11. on.command = "python3 on.py";
  12. on.run();
  13. }
  14. public static void off(){
  15. SSH off = new SSH();
  16. off.command = "python3 off.py";
  17. off.run();
  18. }
  19. public static void check(String host, String storeType, String user,
  20. String password)
  21. {
  22. try {
  23. //create properties field
  24. Properties properties = new Properties();
  25. properties.put("mail.pop3.host", host);
  26. properties.put("mail.pop3.port", "995");
  27. properties.put("mail.pop3.starttls.enable", "true");
  28. Session emailSession = Session.getDefaultInstance(properties);
  29. //create the POP3 store object and connect with the pop server
  30. Store store = emailSession.getStore("pop3s");
  31. store.connect(host, user, password);
  32. //create the folder object and open it
  33. Folder emailFolder = store.getFolder("INBOX");
  34. emailFolder.open(Folder.READ_ONLY);
  35. // retrieve the messages from the folder in an array
  36. Message[] messages = emailFolder.getMessages();
  37. boolean power = true;
  38. while(true){
  39. int i = messages.length - 1;
  40. Message message = messages[i];
  41. String subject = message.getSubject();
  42. // System.out.println(subject);
  43. if(subject.equals("+myRoom") & power == false){
  44. on();
  45. power = true;
  46. System.out.println("Light on");
  47. }
  48. else if (subject.equals("-myRoom") & power == true){
  49. off();
  50. power = false;
  51. System.out.println("Light Off");
  52. }
  53. else{
  54. continue;
  55. }
  56. }
  57. } catch (NoSuchProviderException e) {
  58. e.printStackTrace();
  59. } catch (MessagingException e) {
  60. e.printStackTrace();
  61. } catch (Exception e) {
  62. e.printStackTrace();
  63. }
  64. }
  65. public static void main(String[] args) {
  66. String host = "pop.gmail.com";// change accordingly
  67. String mailStoreType = "pop3";
  68. String username = "EMAIL@gmail.com";// change accordingly
  69. String password = "PASSWORD";// change accordingly
  70. check(host, mailStoreType, username, password);
  71. }
  72. }
de90aj5v

de90aj5v1#

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

相关问题