0

i'm trying the very simple Email Client in java. When i launch the programe i have an error message:

Exception in thread "main" javax.mail.AuthenticationFailedException: EOF on socket
        at com.sun.mail.pop3.POP3Store.protocolConnect(POP3Store.java:146)
        at javax.mail.Service.connect(Service.java:297)
        at javax.mail.Service.connect(Service.java:156)
        at SimpleEmailClient2.main(SimpleEmailClient2.java:21)
Java Result: 1

Why? i use Gmail account and i set the POP and IMAP enabled What could be the possible error in my code? Thank you

here is the code:

import java.util.Properties;

import javax.mail.Authenticator;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Store;

public class SimpleEmailClient2 {

  public static void main(String[] args) throws Exception {

    Properties props = new Properties();

    String host = "pop.gmail.com";
    String provider = "pop3";

    Session session = Session.getDefaultInstance(props, new MailAuthenticator());
    Store store = session.getStore(provider);
    store.connect(host, null, null);

    Folder inbox = store.getFolder("INBOX");
    if (inbox == null) {
      System.out.println("No INBOX");
      System.exit(1);
    }
    inbox.open(Folder.READ_ONLY);

    Message[] messages = inbox.getMessages();
    for (int i = 0; i < messages.length; i++) {
      System.out.println("Message " + (i + 1));
      messages[i].writeTo(System.out);
    }
    inbox.close(false);
    store.close();
  }
}

class MailAuthenticator extends Authenticator {

  public MailAuthenticator() {
  }

  public PasswordAuthentication getPasswordAuthentication() {
    return new PasswordAuthentication("email@gmail.com", "password");
  }
}
user618111
  • 439
  • 5
  • 15
  • 27
  • Try imap. It perfectly works. See http://stackoverflow.com/questions/61176/getting-mail-from-gmail-into-java-application-using-imap fror details – Stan Kurilin Mar 15 '11 at 13:52

2 Answers2

2

I don't believe gmail supports the pop3 provider; you have to use pop3s instead. Otherwise this should work fine.

Ernest Friedman-Hill
  • 80,601
  • 10
  • 150
  • 186
  • Yes i try pop3s with imap, i have no error message but there is nothing, it just displays that it's finished with success and nothing else, the system.out display nothing. – user618111 Mar 15 '11 at 14:02
  • I don't know what you mean about "pop3s with imap" -- use pop3s as the provider, and pop.gmail.com as the host, and that's worked for me in the past. Stas Kurilin is probably right too -- there you'd use "imap" as the provider, and "imap.gmail.com" as the host -- but I've never tried it. – Ernest Friedman-Hill Mar 15 '11 at 14:08
  • I tried this String host = "pop.gmail.com"; String provider = "pop3s"; – user618111 Mar 15 '11 at 14:11
  • i'm expecting to have a result something like "Message": ......Here i have nothing and no errors – user618111 Mar 15 '11 at 14:12
  • Dumb question, but are you sure there are messages in the Inbox? – Ernest Friedman-Hill Mar 15 '11 at 14:13
  • Sorry Ernest , you are right, my inbox was empty, now it's working, thank you very much for all of you – user618111 Mar 15 '11 at 14:25
1

Oracle has information on connecting javamail to gmail here.

Specifically it looks like you're failing when trying to establish the connection, likely because you don't specify a username/password to connect to. Try connecting using something like:

store.connect(host, "user618111@gmail.com", "[myPassword]");
gcooney
  • 1,689
  • 10
  • 14