当我尝试通过 java 邮件连接时,服务器响应意外
问题是有时相同的程序会连接并获取邮件,但有时会抛出
javax.mail.AuthenticationFailedException:在 javax.mail.Service 的 javax.mail.Service.connect(Service.java:295) 的 com.sun.mail.pop3.POP3Store.protocolConnect(POP3Store.java:208) 的套接字上的 EOF。在 newpackage.PmsPOP3Client.main(PmsPOP3Client.java:44) 处连接(Service.java:176)
错误
当服务器说
S: +OK Hello there. C: CAPA S: +OK Here's what I can do:
我可以连接并获取所有邮件
S: EOF
我什至无法连接到服务器
我的代码
import java.util.*;
import javax.mail.*;
import javax.mail.event.ConnectionEvent;
import javax.mail.event.ConnectionListener;
import javax.mail.internet.*;
public class PmsPOP3Client {
public static void main(String[] args) throws Exception {
try {
String host = "host";
String user = "user";
String password = "pass";
Properties props = System.getProperties();
Session session = Session.getDefaultInstance(props);
session.setDebug(true);
Store store = session.getStore("pop3");
System.out.println("store.getURLName() = " + store.getURLName());
store.connect(host, user, password);
Folder folder = store.getFolder("inbox");
folder.open(Folder.READ_ONLY);
Message[] messages = folder.getMessages();
for (int i = 0; i < messages.length; i++) {
System.out.println("------------ Message " + (i + 1) + " ------------");
String from = InternetAddress.toString(messages[i].getFrom());
if (from != null) {
System.out.println("From: " + from);
}
String replyTo = InternetAddress.toString(
messages[i].getReplyTo());
if (replyTo != null) {
System.out.println("Reply-to: " + replyTo);
}
String to = InternetAddress.toString(
messages[i].getRecipients(Message.RecipientType.TO));
if (to != null) {
System.out.println("To: " + to);
}
String cc = InternetAddress.toString(
messages[i].getRecipients(Message.RecipientType.CC));
if (cc != null) {
System.out.println("Cc: " + cc);
}
String bcc = InternetAddress.toString(
messages[i].getRecipients(Message.RecipientType.BCC));
if (bcc != null) {
System.out.println("Bcc: " + to);
}
String subject = messages[i].getSubject();
if (subject != null) {
System.out.println("Subject: " + subject);
}
Date sent = messages[i].getSentDate();
if (sent != null) {
System.out.println("Sent: " + sent);
}
Date received = messages[i].getReceivedDate();
if (received != null) {
System.out.println("Received: " + received);
}
System.out.println();
}
folder.close(true);
store.close();
} catch (Exception e) {
e.printStackTrace();
}
} }