我正在尝试连接到本地托管的电子邮件 POP3 收件箱并在邮箱中显示电子邮件,但我不断收到错误:
Exception in thread "main" javax.mail.MessagingException: Connect failed; nested exception is:
java.net.ConnectException: Connection refused
at com.sun.mail.pop3.POP3Store.protocolConnect(POP3Store.java:209)
at javax.mail.Service.connect(Service.java:295)
at javax.mail.Service.connect(Service.java:176)
at com.kami.utils.MailClient.checkInbox(MailClient.java:33)
at com.kami.Main.main(Main.java:38)
我的类(class)是这样的:
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 的本地托管电子邮件服务器
最佳答案
首先在你的机器上telnet 110端口来检查该服务是否正在运行。在我的笔记本电脑中,我没有运行 pop3 服务器,结果如下:
hans@andes:~$ telnet localhost 110
Trying 127.0.0.1...
telnet: Unable to connect to remote host: Connection refused
如果连接成功,则用自己的数据进行pop3的协议(protocol)认证:
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 配置上,请检查库中的文档和示例。
关于java - 连接到本地 POP3 收件箱 Java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35702840/