[英]Why do i get 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");
}
可能是因為服務器拒絕連接。
嘗試從“telnet”連接。 一旦你可以連接,那么你應該能夠從Java程序連接。
以下是一些故障排除提示:
嘗試這個 :
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");
}
也請訪問這個問題
嘗試改變
store.connect("pop.gmail.com" , "username" , "password");
至
store.connect("pop.gmail.com" , 995, "username" , "password");
免責聲明:我沒有測試過這個。
Gmail需要安全的SSL連接,並且javax.mail.Service可能沒有提供。 我認為更可能的解釋是,您只是沒有連接到正確的端口,因此我明確指定了Gmail POP3服務的正確端口號。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.