Java Mail身份验证和连接

时间:2013-05-02 11:26:50

标签: java javamail

我正在尝试通过java邮件进行连接 服务器正在响应意外

问题在于,有时同一个程序连接并获取邮件,但有时它会抛出

  

javax.mail.AuthenticationFailedException:套接字上的EOF   com.sun.mail.pop3.POP3Store.protocolConnect(POP3Store.java:208)at 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:

我可以连接并获取所有邮件

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();
     }
 } }

2 个答案:

答案 0 :(得分:3)

你应该尝试使用pop3s作为protocal而不是pop3作为以下

Store store = session.getStore("pop3s");

答案 1 :(得分:0)

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