1

当我尝试通过 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();
     }
 } }
4

2 回答 2

3

您可能应该尝试使用 pop3s 作为协议而不是 pop3,如下所示

Store store = session.getStore("pop3s");
于 2015-04-06T19:01:11.083 回答
0

您可能需要检查服务器上是否存在某些端口争用。这可能是由于某些其他程序有时使用相同的端口并与您自己的邮件服务器发生冲突。检查服务器机器的防火墙/病毒扫描/等,这可能会暂时阻止端口并停止客户端连接。

于 2013-05-02T12:40:07.360 回答