2015-09-23 67 views
1

我一直在試圖從我的微軟交換服務器使用Java代碼檢索電子郵件。閱讀郵件附帶Microsoft郵件交換服務器使用Java

這裏是我的代碼

Properties properties = new Properties(); 
    properties.put("mail.pop3.host", host); 
    properties.put("mail.pop3.port", port); 
    // SSL setting 
    properties.setProperty("mail.pop3.socketFactory.class", 
      "javax.net.ssl.SSLSocketFactory"); 
    properties.setProperty("mail.pop3.socketFactory.fallback", "false"); 
    properties.setProperty("mail.pop3.socketFactory.port",String.valueOf(port)); 

    properties.setProperty("mail.auth.debug","true"); 
    String pid = ""; 
    Boolean chkFlag = false; 
    Session session = Session.getInstance(properties); 
    try { 
     properties.setProperty("mail.store.protocol", "pop3"); 
     session = Session.getDefaultInstance(properties, null); 
     Store store = session.getStore("pop3"); 
     System.out.println("host,userName, password"+host +" " +userName + " "+password); 
     store.connect(host,userName, password); 
     Folder folderInbox = store.getFolder("Inbox"); 
     folderInbox.open(Folder.READ_WRITE); 
     System.out.println("bffhb"); 
     String MailsaveDirectory = saveDirectory; 
     Message[] arrayMessages = folderInbox.search(new FlagTerm(new Flags(
       Flags.Flag.SEEN), false)); 
     System.out.println("No. of Unread Messages : " + arrayMessages.length); 
     for (int i = 0; i < arrayMessages.length; i++) { 
      System.out.println("msgcnt"); 
      Message message = arrayMessages[i]; 
      Address[] fromAddress = message.getFrom(); 
      Address[] toAddress = message.getRecipients(Message.RecipientType.TO); 
      String from = fromAddress[0].toString(); 
      String to = toAddress[0].toString(); 
      String subject = message.getSubject(); 
      String queueId = ""; 
      String sentDate = message.getSentDate().toString(); 
      String contentType = message.getContentType(); 
      String messageContent = ""; 
      String attachFiles = ""; 
      MailMht mh = new MailMht(); 
      saveDirectory = MailsaveDirectory + File.separator + "Message-" + (i + 1); 
      System.out.println("saveDirectory "+saveDirectory); 

       pid = subject.substring(subject.lastIndexOf(":") + 1); 
       mh.parseMessages(message, folderInbox, saveDirectory); 
       if (contentType.contains("multipart")) { 

       } 
       if (true) { 

        File dir = new File(saveDirectory); 
        System.out.println("dir to be created:" + saveDirectory); 
        if (dir.exists() == false) { 
         System.out.println("Creating Dir..."); 
         dir.mkdirs(); 
         System.out.println("Created Dir..."); 
        } 
        if (contentType.contains("multipart")) { 
        } 

      } else { 
       chkFlag = false; 
      } 

     try { 
      Folder folderI = null; 
      if (chkFlag) { 

       folderInbox.setFlags(arrayMessages, new Flags(Flags.Flag.DELETED), true); 
       folderInbox.close(true); 

       store.close(); 
       chkFlag = true; 
      } 
     } catch (Exception e) { 
      chkFlag = false; 
      e.printStackTrace(); 
      folderInbox.close(false); 
      store.close(); 
     } 
    } 

    } catch (MessagingException ex) { 
     System.out.println("Could not connect to the message store"); 
     ex.printStackTrace(); 
     chkFlag = false; 
    } 
    return chkFlag; 
} 

我已經做了幾乎所有和我收到的驗證例外

javax.mail.AuthenticationFailedException:AUTH]認證 失敗。

但我可以通過live.com網站登錄。

有沒有人有建議擺脫這一點?

回答

0

是:獲取郵件會話時,您錯過了認證

Authenticator authenticator=new Authenticator() 
{ 
    @Override 
    protected PasswordAuthentication getPasswordAuthentication() 
    { 
     return new PasswordAuthentication(userName, password); 
    } 
}; 
properties.setProperty("mail.pop3.auth", "true"); 
Session session=Session.getInstance(props, authenticator); 

順便說一句,你有會話初始化:刪除其中的一個。