Java Mail身份验证和连接 [英] Java Mail authentication and connection
问题描述
当我尝试通过Java Mail连接时 服务器正在响应意外
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位于 com.sun.mail.pop3.POP3Store.protocolConnect(POP3Store.java:208)位于 javax.mail.Service.connect(Service.java:295)在 javax.mail.Service.connect(Service.java:176)在 newpackage.PmsPOP3Client.main(PmsPOP3Client.java:44)
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)
错误
服务器说出
S: +OK Hello there.
C: CAPA
S: +OK Here's what I can do:
我可以连接并提取所有邮件
i can connect and fetch all mails
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();
}
} }
推荐答案
您应该尝试使用pop3作为协议,而不是以下所述的pop3
You should probably try using pop3s as the protocal instead of pop3 as the following
Store store = session.getStore("pop3s");
这篇关于Java Mail身份验证和连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!