2

我需要从 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 加密吗?

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

先感谢您 !

4

1 回答 1

0

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

于 2017-04-04T13:29:49.337 回答