1
我有要求從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搜索這個錯誤,但沒有找到確切的根本原因?
預先感謝您!
使用較新的JDK所有必需的步驟,因爲是的Java6長時間以來 – Jens
@Jens過時,它iwll很難升級到JDK 7在生產中,因爲這是耗時的過程。我讀過JDK 6支持TLS 1.0。 你能告訴我如何消除這個錯誤? –
你好,你可以幫我嗎? –