如何使用java阅读电子邮件?

时间:2011-04-27 13:12:16

标签: java email

我可以发送电子邮件,但我无法阅读电子邮件。

以下是我连接到邮件服务器的代码:

    String host = "na-*****.*****.****.ea.com";
    String username = "*****@*******.ea.com";
    String password = "********";

    Properties properties = System.getProperties();
    Session session = Session.getDefaultInstance(properties);
    session.setDebug(true);
    Store store = session.getStore("pop3");
    store.connect(host, username, password);

每当我尝试使用代码阅读电子邮件时,都会引发以下错误:

javax.mail.MessagingException: Connect failed;
nested exception is:
java.net.SocketException: Connection reset
at com.sun.mail.pop3.POP3Store.protocolConnect(POP3Store.java:210)
at javax.mail.Service.connect(Service.java:295)
at javax.mail.Service.connect(Service.java:176)
at DisplayMail.main(DisplayMail.java:18)
Caused by: java.net.SocketException: Connection reset
at java.net.SocketInputStream.read(Unknown Source)
at java.io.BufferedInputStream.fill(Unknown Source)
at java.io.BufferedInputStream.read(Unknown Source)
at java.io.DataInputStream.readLine(Unknown Source)
at com.sun.mail.pop3.Protocol.readResponse(Protocol.java:683)
at com.sun.mail.pop3.Protocol.simpleCommand(Protocol.java:656)
at com.sun.mail.pop3.Protocol.<init>(Protocol.java:109)
at com.sun.mail.pop3.POP3Store.getPort(POP3Store.java:261)
at com.sun.mail.pop3.POP3Store.protocolConnect(POP3Store.java:206)
... 3 more

有人可以告诉我我做错了什么,或者是否需要更多信息?

1 个答案:

答案 0 :(得分:2)

有多种电子邮件协议(pop3,imap,交换等),根据您希望找到一个库(或自己动手)所需的协议来说出访问和下载电子邮件的选择协议服务器

我建议查看JavaMail API