使用JavaMail读取邮件

时间:2013-02-25 05:03:20

标签: java javamail

尝试使用JavaMail读取邮件时出现此错误。请让我知道如何解决此错误。我已将 activation.jar mail.jar 添加到eclipse中。

DEBUG POP3: server doesn't support TOP, disabling it
javax.mail.AuthenticationFailedException: Command is not valid in this state.
at com.sun.mail.pop3.POP3Store.protocolConnect(POP3Store.java:174)
at javax.mail.Service.connect(Service.java:291)
at javax.mail.Service.connect(Service.java:172)
at library.VerifyEmail.main(VerifyEmail.java:40)

以下是我正在尝试的代码:

package library;

import java.io.IOException;
import java.util.Properties;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.NoSuchProviderException;
import javax.mail.Session;
import com.sun.mail.pop3.POP3Store;

import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.mail.search.SubjectTerm;
import javax.activation.*;
import java.io.*;
public class VerifyEmail {
public static void main(String[] args) throws Exception {
     // SUBSTITUTE YOUR ISP's POP3 SERVER HERE!!!
    String host = "myhost";
    // SUBSTITUTE YOUR USERNAME AND PASSWORD TO ACCESS E-MAIL HERE!!!
    String user = "myuser";
    String password = "mypass";

 // Get a session.  Use a blank Properties object.
    Session session = Session.getInstance(new Properties());
    try {
        // Get a Store object
        Store store = session.getStore("pop3");
        store.connect(host, user, password);

        // Get "INBOX"
        Folder fldr = store.getFolder("INBOX");
        fldr.open(Folder.READ_WRITE);
        int count = fldr.getMessageCount();
        System.out.println(count  + " total messages");

        // Message numebers start at 1
        for(int i = 1; i <= count; i++) {
            // Get  a message by its sequence number
            Message m = fldr.getMessage(i);
         // Get some headers
            Date date = m.getSentDate();
            Address [] from = m.getFrom();
            String subj = m.getSubject();
            String mimeType = m.getContentType();
            System.out.println(date + "\t" + from[0] + "\t" +
                                subj + "\t" + mimeType);
        }

    }catch (MessagingException  ioex) {
        ioex.printStackTrace();
    }
}
}

3 个答案:

答案 0 :(得分:3)

当您收到 javax.mail.AuthenticationException 时,表示您的应用程序无法对邮件服务器进行身份验证。

可能的原因可能是客户端密钥库中缺少邮件服务器的SSL证书。

答案 1 :(得分:0)

根据microsoft对于Exchange 2010,默认情况下,服务器需要客户端使用ssl作为pop3。 没有ssl,服务器响应“ ERR命令在此状态下无效。

以下是如何将javamail与ssl一起使用 - Javamail and Gmail Pop3 SSL

答案 2 :(得分:0)

DEBUG POP3:服务器不支持TOP,禁用它 可以通过将java邮件jar版本更新为1.4.4来禁用此消息