问题
While I am trying to connect through java mail the server is responding unexpected
the problem is that sometimes the same program connects and get the mails but some times it throws
javax.mail.AuthenticationFailedException: EOF on socket at com.sun.mail.pop3.POP3Store.protocolConnect(POP3Store.java:208) at javax.mail.Service.connect(Service.java:295) at javax.mail.Service.connect(Service.java:176) at newpackage.PmsPOP3Client.main(PmsPOP3Client.java:44)
error
when server says
S: +OK Hello there. C: CAPA S: +OK Here's what I can do:
i can connect and fetch all mails
S: EOF
i cant even connect to server
my code
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();
}
} }
回答1:
You should probably try using pop3s as the protocal instead of pop3 as the following
Store store = session.getStore("pop3s");
回答2:
You may need to check whether there is some port contention on your server. It may be either due to some other program using the same port sometimes and clashing with your own mail server. Check the server machine for firewall/virus scans/ etc which may block port temporarily and stopping the clients to connect.
来源:https://stackoverflow.com/questions/16336886/java-mail-authentication-and-connection