开发者

How to set Email Client in Java?

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 == 开发者_如何学Pythonnull) {
      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");
  }
}


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


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]");
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜