2017-06-08 205 views
-1

我試圖使用JavaMail連接到郵件服務器。我收到一個例外。Javamail服務器連接超時

我試過使用POP3,但沒有運氣。

錯誤:

​​3210

這是由引起:

java.net.ConnectException: Connection timed out: connect 

我的代碼:

Properties props = System.getProperties(); 
props.setProperty("mail.store.protocol", "imaps"); 
Session session = Session.getDefaultInstance(props, null); 

try 
{ 
    Store store = session.getStore("imaps"); 
    MailSSLSocketFactory socketFactory = new MailSSLSocketFactory(); 
    socketFactory.setTrustAllHosts(true); 
    props.put("mail.imaps.ssl.socketFactory", socketFactory); 
    store.connect(host,user,password); 
} 
catch (MessagingException ex){ 
    System.out.println(ex.toString()); 
} 

Properties properties = new Properties(); 
properties.put("mail.pop3.host", host); 
properties.put("mail.pop3.port", port); 
// SSL setting 
properties.setProperty("mail.pop3.socketFactory.class", 
"javax.net.ssl.SSLSocketFactory"); 
properties.setProperty("mail.pop3.socketFactory.fallback", "false"); 
properties.setProperty("mail.pop3.socketFactory.port", String.valueOf(port)); 

Session session = Session.getDefaultInstance(properties); 
// connects to the message store 
Store store = session.getStore("pop3"); 
store.connect(user, password); 
System.out.println(store.isConnected()); 
System.out.println("connection succesful"); 

// opens the inbox folder 
Folder folderInbox = store.getFolder("INBOX"); 
folderInbox.open(Folder.READ_ONLY); 
+0

首先,解決所有這些[共同的JavaMail錯誤](https://javaee.github.io/javamail/FAQ#commonmistakes),然後按照下列[連接調試技巧(HTTPS://javaee.github .IO/JavaMail的/ FAQ#condebug)。 –

回答

0

我能去使用下建立連接。我之前使用過不合適的主機,這很糟糕。

Properties props = System.getProperties(); 
    props.setProperty("mail.store.protocol", "imaps"); 

    Session session = Session.getInstance(props, null); 
    session.setDebug(true); 
    Store store = session.getStore("imaps"); 

    MailSSLSocketFactory socketFactory = new MailSSLSocketFactory(); 
    socketFactory.setTrustAllHosts(true); 

    props.put("mail.imaps.ssl.socketFactory", socketFactory); 
    System.out.println("store is "+store.isConnected()); 

    store.connect("mail.abc.com","[email protected]", "******"); 
    System.out.println("Now the store is "+store.isConnected()); 
相關問題