我正在尝试从我的hotamil帐户中获取未读的邮件号码。我使用但遇到问题,它总是向我返回收件箱中所有现有的电子邮件号码。在这里,我分享我的代码:

public class testEmail {
    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");

             // properties.put("mail.imap.ssl.enable", "true");
              Session emailSession = Session.getInstance(properties, null);
              emailSession.setDebug(false);

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

             //for count the unread email
          FlagTerm ft = new FlagTerm(new Flags(Flags.Flag.SEEN), false);
          Message messages[] = emailFolder.search(ft);
          System.out.println("No. of Unread Messages : " + messages.length);

              emailFolder.close(false);
              store.close();

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

           public static void main(String[] args) {

              String host = "pop-mail.outlook.com";// change accordingly
              String mailStoreType = "pop3";
              String username = "[email protected]";// change accordingly
              String password = "abc";// change accordingly

              check(host, mailStoreType, username, password);

           }

        }


如果我使用FlagTerm(new Flags(Flags.Flag.SEEN), true);,则返回0。这是关于代码或Hotmail帐户的问题。提前致谢。

最佳答案

Flags.Flag.SEEN在POP3中无法正常工作,因此将其更改为IMAP。如果mail.imap.port = 993不起作用,则还有另一件事,请使用25。答案是关于hotmail。

07-24 13:39