【问题标题】:Connect to local POP3 inbox Java连接到本地 POP3 收件箱 Java
【发布时间】:2016-06-12 16:28:12
【问题描述】:

我正在尝试连接到本地托管的电子邮件 POP3 收件箱并在邮箱中显示电子邮件,但我不断收到错误消息:

线程“主”javax.mail.MessagingException 中的异常:连接失败; 嵌套异常是:
java.net.ConnectException:连接被拒绝
在 com.sun.mail.pop3.POP3Store.protocolConnect(POP3Store.java:209)
在 javax.mail.Service.connect(Service.java:295)
在 javax.mail.Service.connect(Service.java:176)
在 com.kami.utils.MailClient.checkInbox(MailClient.java:33)
在 com.kami.Main.main(Main.java:38)

我的班级是这样的:

public class MailClient {
    private String host;
    private String username;
    private String password;
    private String provider;
    protected Session session;

    public MailClient() {
        Properties props = new Properties();

        this.host = "localhost";
        this.username = "unix-user";
        this.password = "unix-password";
        this.provider = "pop3";

        this.session = Session.getDefaultInstance(props, null);
    }

    public void checkInbox() throws MessagingException, IOException {
        Store store = session.getStore(provider);
        store.connect(host, username, password); //This is line 33
        Folder inbox = store.getFolder("inbox");
        inbox.open(Folder.READ_ONLY);
        Message[] messages = inbox.getMessages();

        for(Message message : messages){
            System.out.println(message.getReceivedDate());
            System.out.println(message.getSubject());
        }

        inbox.close(true);
        store.close();
    }
}

它是本地托管的电子邮件服务器,使用 Dovecot IMAP/POP3 服务器版本 2.2.9 和 Postfix 邮件服务器 Postfix 版本 2.11.0

【问题讨论】:

  • AFAIK java pop3 实现适用于连接的邮箱。您是否正在尝试连接已下载的 pop3 邮箱?
  • 连接到本地托管的,就像我可以从它发送电子邮件一样:props.put("mail.smtp.host", "localhost");
  • 本地托管让我很困惑。你有一个在你的 linux 机器上运行的邮件服务器(pop3/pop3s 服务正在运行)吗?
  • 正确,Dovecot IMAP/POP3 服务器版本 2.2.9 和 Postfix 邮件服务器 Postfix 版本 2.11.0
  • Hans Poo 的回答在这种情况下是有意义的。如果 IP 地址和端口正确,请检查您的代码。

标签: java email jakarta-mail pop3 dovecot


【解决方案1】:

您机器上的第一个 telnet 110 端口检查服务是否在那里运行。在我的笔记本电脑中,我没有运行 pop3 服务器,结果如下:

hans@andes:~$ telnet localhost 110
Trying 127.0.0.1...
telnet: Unable to connect to remote host: Connection refused

如果连接成功,用自己的数据按照pop3的协议认证:

hans@andes:~$ telnet mail.foo.com 110
Trying X.X.X.X...
Connected to mail.foo.com.
Escape character is '^]'.
+OK mail.foo.com POP3 server ready
user fooUser
+OK hello fooUser, please enter your password
pass fooPassword
+OK server ready

在你的情况下 telnet localhost;另请注意,您只应发出以下命令:telnet、user 和 pass。剩下的就是服务器的响应了。

如果一切正常,问题出在您的 java 配置上,请检查库中的文档和示例。

【讨论】:

  • 只是一个关于这个的问题,它确实回答了我,但它立即关闭了连接:telnet localhost 110 Trying 127.0.0.1... Connected to localhost. Escape character is '^]'. Connection closed by foreign host. 这是正常的吗?我什至无法检查用户。
  • 如果你花的时间太长,服务器会超时并关闭连接,你必须立即开始输入,如果它一直关闭连接,请查看服务器日志文件。在我的 linux 机器上这很好用,windows 终端不是那么友好。
  • 感谢日志检查建议,显然我收到了Error: service(pop3-login): command startup failed, throttling for 2 secs
  • 所有归结为我的证书由于某种原因不匹配,我不得不重新生成它们。修复了我的 POP3 连接立即超时。感谢您的帮助 =]
【解决方案2】:

以下方法将从弹出邮箱中获取消息(给定 _Host=localhost、_User=unix-user、_Password=unix-password、_Protocol="pop3")。但是,您必须确定以下几点: 1)“localhost”运行的是“pop3”服务器,而不是“pop3s”(安全协议)服务器; 2)“localhost”上的“pop3”服务器正在侦听默认端口 3)“unix-user”有一个pop3邮箱

根据您的跟进情况,您似乎希望能够从 pop3 帐户发送邮件。这不是它的工作方式,因为 pop3 只是一种检索消息的方法,而不是发送它们。要发送邮件,您需要与 SMTP 服务器建立单独的连接。

  public Message[] getMessages(int maxCount)
      throws MessagingException
  {
    // Get a Session object
    Properties props = new Properties();
    Session session = Session.getInstance(props);

    // Get a Store object
    Store store = session.getStore(_protocol);

    // Connect
    store.connect(_host,_user,_password);

    // Open a Folder
    Folder folder = store.getFolder(_mailbox);
    if (folder == null || !folder.exists())
        throw new ApplicationException("Invalid mailbox");

    //Gets up to maxCount messages from the pop box
    folder.open(Folder.READ_WRITE);
    Message[] messages = Monitor.EMPTY_MESSAGE_ARRAY;
    int toMessageIndex=folder.getMessageCount();
    if (toMessageIndex > 0) {
      if (toMessageIndex > maxCount)
        toMessageIndex = maxCount;
      messages = folder.getMessages(1,toMessageIndex);
    }

    // Go through all the new messages and make sure they are loaded. Use the outputStream
    //to force all information to be downloaded.
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    for (int i = 0; i < messages.length && shouldRun(); i++) {
      try {
        //Force the download of all message information
        bos.reset();
        messages[i].writeTo(bos);
        getLog().enter(
          this,
          "[readAndClearInBox] Read message to " + messages[i].getAllRecipients()[0].toString());

      } catch (Exception mex) {
        getLog().error(this, mex, "[readAndClearInBox] Message exception");
        StringWriter sw = new StringWriter();
        PrintWriter pw = new PrintWriter(sw, true);
        try {
          Monitor.dumpEnvelope(getLog(), pw, messages[i]);
        } catch (Exception ex) {
          getLog().error(this, mex, "[readAndClearInBox] Could only display faulty message.");
        } finally {
          pw.flush();
          getLog().enter(this, "[readAndClearInBox]" + sw.toString());
        }
      } finally {
        //Mark the message for deletion
        messages[i].setFlag(Flags.Flag.DELETED, true);
      }
    }

    //Close folder and expunge all deleted messages, unless the read was aborted
    if (shouldRun()) {
      getLog().enter(this,"Found " + messages.length + " messages; closing inbox.");
      folder.close(true);
      store.close();
      return messages;
    } else {
      getLog().enter(this,"Found " + messages.length + " messages; closing inbox without expunging.");
      folder.close(false);
      store.close();
      _bShouldRun = true;
      return Monitor.EMPTY_MESSAGE_ARRAY;
    }
  }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多