通过Java中的Pop3阅读Outlook 365的INBOX时面临错误

问题描述:

我有要求从Outlook 365帐户的INBOX文件夹中读取电子邮件。 我已经安装了所有必需的证书,并且可以通过我的机器远程登录outlook.office365.com主机。通过Java中的Pop3阅读Outlook 365的INBOX时面临错误

我使用的是JDK 1.6.0.29版本,我的Outlook 365对POP3和IMAP使用TLS 1.0加密。

不过还是我得到下面的错误 -

javax.mail.AuthenticationFailedException: EOF on socket 
    at com.sun.mail.pop3.POP3Store.protocolConnect(POP3Store.java:209) 
    at javax.mail.Service.connect(Service.java:386) 
    at client.ConnectToOffice365REST.check(ConnectToOffice365REST.java:56) 
    at client.ConnectToOffice365REST.main(ConnectToOffice365REST.java:96) 

这里是我完整的代码 -

package client; 




    import java.util.Properties; 

import javax.mail.Authenticator; 
import javax.mail.Folder; 
    import javax.mail.Message; 
    import javax.mail.MessagingException; 
    import javax.mail.NoSuchProviderException; 
import javax.mail.PasswordAuthentication; 
import javax.mail.Session; 
    import javax.mail.Store; 

    public class ConnectToOffice365REST{ 
     public static String username =null; 
     public static String password1 =null; 
     public static void check(String host, String storeType, String user, 
      String password) 
     { username= user; 
      password1 = 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.setProperty("mail.pop3.socketFactory.fallback", "false"); 
       properties.setProperty("mail.pop3.socketFactory.port",  
         String.valueOf("995")); 
       properties.put("mail.pop3.auth", "true"); 
       properties.put("mail.debug.auth", "true"); 
      Session emailSession = Session.getDefaultInstance(properties); 





      //create the POP3 store object and connect with the pop server 

      Store store = emailSession.getStore("pop3"); 
     // store.connect(host, user, password); 
      // store.connect(host, 995, user, password); 

      //create the folder object and open it 
      Folder emailFolder = store.getFolder("INBOX"); 
      emailFolder.open(Folder.READ_ONLY); 

      // retrieve the messages from the folder in an array and print it 
      Message[] messages = emailFolder.getMessages(); 
      System.out.println("messages.length---" + messages.length); 

      for (int i = 0, n = messages.length; i < n; i++) { 
      Message message = messages[i]; 
      System.out.println("---------------------------------"); 
      System.out.println("Email Number " + (i + 1)); 
      System.out.println("Subject: " + message.getSubject()); 
      System.out.println("From: " + message.getFrom()[0]); 
      System.out.println("Text: " + message.getContent().toString()); 

      } 

      //close the store and folder objects 
      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 = "outlook.office365.com";// change accordingly 
      String mailStoreType = "pop3"; 
      String username = "username";// change accordingly 
      String password = "password";// change accordingly 

      check(host, mailStoreType, username, password); 

     } 

    } 

请让我知道在哪里的问题。 1)我必须安装更多证书吗? 2)用户名和密码非常好。 3)这是由于TLS而不是SSL加密?

我试过用Google搜索这个错误,但没有找到确切的根本原因?

预先感谢您!

+0

使用较新的JDK所有必需的步骤,因为是的Java6长时间以来 – Jens

+0

@Jens过时,它iwll很难升级到JDK 7在生产中,因为这是耗时的过程。我读过JDK 6支持TLS 1.0。 你能告诉我如何消除这个错误? –

+0

你好,你可以帮我吗? –

最后我得到了突破,我已经发布在我的论坛http://kushalkariaofm.blogspot.in/2017/03/how-to-read-emails-from-inbox-folder_28.html