java 为什么我会收到此异常?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6802208/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why do i get this exception?
提问by saplingPro
This method gives the number of emails in the inbox.But it gives me this exception :
此方法提供收件箱中的电子邮件数量。但它给了我这个例外:
javax.mail.MessagingException: Connect failed;
nested exception is:
java.net.ConnectException: Connection timed out: connecterror
-
——
Session session = Session.getInstance(new Properties());
try {
Store store = session.getStore("pop3");
store.connect("pop.gmail.com" , "username" , "password");
Folder fldr = store.getFolder("INBOX");
fldr.open(Folder.READ_WRITE);
int count = fldr.getMessageCount();
System.out.println(count);
} catch(Exception exc) {
System.out.println(exc + "error");
}
回答by paulsm4
Probably because the server refuses to connect.
可能是因为服务器拒绝连接。
Try connecting from "telnet". Once you can connect at all, then you should be able to connect from your Java program.
尝试从“telnet”连接。一旦您完全可以连接,那么您应该能够从您的 Java 程序进行连接。
Here are some troubleshooting tips:
以下是一些故障排除提示:
回答by Suhail Gupta
Try this :
试试这个 :
Properties props = new Properties();
props.put("mail.pop3.host" , "pop.gmail.com");
props.put("mail.pop3.user" , "username");
// Start SSL connection
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( "username" , "password");
}
});
try {
Store store = session.getStore("pop3");
store.connect("pop.gmail.com" , "username" , "password");
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");
}
Also visit this question
也访问这个问题
回答by Gravity
Try changing
尝试改变
store.connect("pop.gmail.com" , "username" , "password");
to
到
store.connect("pop.gmail.com" , 995, "username" , "password");
Disclaimer: I have not tested this.
免责声明:我没有测试过这个。
Gmail requires a secure SSL connection, and maybe javax.mail.Service isn't providing that. I think the more likely explanation, though, is that you're simply not connecting to the right port, so I've explicitly specified the correct port number for Gmail's POP3 service.
Gmail 需要安全的 SSL 连接,而 javax.mail.Service 可能没有提供。不过,我认为更可能的解释是您没有连接到正确的端口,所以我已经明确指定了 Gmail 的 POP3 服务的正确端口号。
回答by Ryan Stewart
Try following a "how to use gmail as an smtp server" tutorial. Google also has a configuration pagewith all the settings you'll need.
尝试遵循“如何使用 gmail 作为 smtp 服务器”教程。Google 还有一个配置页面,其中包含您需要的所有设置。