Indranil Ghosh
Indranil Ghosh

Reputation: 149

how to read email using java?

I can send email, but I am unable to read the emails.

Here is my code to connect to the mail server:

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

Whenever I try to read email using the code,it throws the following error:

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

Can someone tell me what I'm doing incorrectly, or if more information is needed?

Upvotes: 1

Views: 1655

Answers (1)

Alan Barber
Alan Barber

Reputation: 983

there are multiple email protocols (pop3, imap, exchange, etc) and depending on which protocol you want you will need to find a library (or roll your own) to speak the protocol of choice to access and download emails from a server.

I would suggest looking at the JavaMail API

Upvotes: 2

Related Questions