在Java中通过Pop3读取Outlook 365的INBOX时遇到错误

时间:2017-03-23 14:08:49

标签: java email ssl outlook tls1.0

我要求从Outlook 365帐户的INBOX文件夹中读取电子邮件。 我已经安装了所有必需的证书,我可以从我的机器远程登录outlook.office365.com主机。

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

但我仍然低于错误 -

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加密吗?

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

提前谢谢!

1 个答案:

答案 0 :(得分:0)

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

中发布了所有必需的步骤