无法使用 POP API 从 java 代码读取 Gmail 收件箱..给出错误 java.net.ConnectException: 连接超时: 连接错误
props.put("mail.pop3.host", "pop.gmail.com");
props.put("mail.pop3.user", "[email protected]");
props.put("mail.pop3.socketFactory", 995);
props.put("mail.pop3.socketFactory.class",
"javax.net.ssl.SSLSocketFactory");
props.put("mail.pop3.port", 995);
Session session = Session.getDefaultInstance(props,
new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("xxx",
"xxx");
}
});
try {
Store store = session.getStore("pop3");
store.connect("pop.gmail.com", "xxxxx", "xxx");
Folder fldr = store.getFolder("INBOX");
fldr.open(Folder.HOLDS_MESSAGES);
int count = fldr.getMessageCount();
System.out.println(count);
} catch (Exception exc) {
System.out.println(exc + " error");
}
// TODO Auto-generated method stub
}
错误:
javax.mail.MessagingException:连接失败;嵌套异常是:java.net.ConnectException:连接超时:连接错误
因为我在此应用程序中添加了代理设置 并关闭防火墙。仍然给出上述错误。
props.put("mail.pop3.host", "pop.gmail.com");
props.put("mail.pop3.user", "[email protected]");
props.put("mail.pop3.socketFactory", 995);
props.put("mail.pop3.socketFactory.class",
"javax.net.ssl.SSLSocketFactory");
props.put("mail.pop3.port", 995);
Session session = Session.getDefaultInstance(props,
new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("xxx",
"xxx");
}
});
try {
Store store = session.getStore("pop3");
store.connect("pop.gmail.com", "xxxxx", "xxx");
Folder fldr = store.getFolder("INBOX");
fldr.open(Folder.HOLDS_MESSAGES);
int count = fldr.getMessageCount();
System.out.println(count);
} catch (Exception exc) {
System.out.println(exc + " error");
}
// TODO Auto-generated method stub
}
Error:
javax.mail.MessagingException: Connect failed; nested exception is: java.net.ConnectException: Connection timed out: connect error
As I have added Proxy setting in this application
And set firewalls off. Still its is giving the above error.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
JavaMail FAQ 具有更简单的代码 连接到 Gmail,以及 调试连接问题的提示 和 使用代理服务器的说明。
The JavaMail FAQ has simpler code for connecting to Gmail, as well as tips for debugging connection problems and instructions for using proxy servers.