在Java中通过Pop365读取Outlook 365的收件箱时遇到错误



我需要从Outlook 365帐户的收件箱文件夹中阅读电子邮件。我已经安装了所有必需的证书,并且能够从我的计算机上远程登录 outlook.office365.com 主机。

我使用的是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加密吗?

我尝试在谷歌上搜索此错误,但没有找到确切的根本原因?

提前谢谢你!

最后我取得了突破,我已经在我的论坛 http://kushalkariaofm.blogspot.in/2017/03/how-to-read-emails-from-inbox-folder_28.html 中发布了所有必需的步骤

最新更新