如何通过javamail api查看我的hotmail帐户中有多少未读邮件?

时间:2017-04-12 10:42:44

标签: java javamail

我正在尝试从我的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 = "abc@hotmail.com";// change accordingly
              String password = "abc";// change accordingly

              check(host, mailStoreType, username, password);

           }

        }

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

1 个答案:

答案 0 :(得分:0)

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