【问题标题】:How to set Email Client in Java?如何在 Java 中设置电子邮件客户端?
【发布时间】:2011-07-15 19:22:04
【问题描述】:

我正在使用 java 中非常简单的电子邮件客户端。 当我启动程序时,我收到一条错误消息:

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

为什么? 我使用 Gmail 帐户,并启用了 POP 和 IMAP 我的代码中可能出现的错误是什么? 谢谢

代码如下:

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

【问题讨论】:

标签: java email


【解决方案1】:

Oracle 有关于将 javamail 连接到 gmail here 的信息。

具体来说,您在尝试建立连接时似乎失败了,可能是因为您没有指定要连接的用户名/密码。尝试使用以下方式连接:

store.connect(host, "user618111@gmail.com", "[myPassword]");

【讨论】:

  • 我使用了自己的邮箱和密码,我也有错误。
【解决方案2】:

我不相信 gmail 支持 pop3 提供程序;你必须改用pop3s。否则这应该可以正常工作。

【讨论】:

  • 是的,我尝试使用 imap 的 pop3s,我没有错误消息但什么也没有,它只是显示它已成功完成,没有其他任何内容,system.out 什么也不显示。
  • 我不知道你所说的“pop3s with imap”是什么意思——使用 pop3s 作为提供者,pop.gmail.com 作为主机,过去这对我有用。 Stas Kurilin 也可能是对的——你会使用“imap”作为提供者,使用“imap.gmail.com”作为主机——但我从未尝试过。
  • 我试过这个 String host = "pop.gmail.com";字符串提供者 = "pop3s";
  • 我希望得到类似“消息”的结果:......这里我什么也没有,也没有错误
  • 愚蠢的问题,但您确定收件箱中有邮件吗?
猜你喜欢
  • 2011-03-21
  • 1970-01-01
  • 2015-12-15
  • 1970-01-01
  • 1970-01-01
  • 2023-03-08
  • 2013-03-03
  • 2014-08-23
  • 1970-01-01
相关资源
最近更新 更多