2012-09-03 78 views
1

我想從雅虎讀未讀郵件mail.so我用下面的代碼,但它給了我錯誤如何閱讀從雅虎未讀郵件在java中

如下

javax.mail.MessagingException: Connect failed; 
    nested exception is: 
    java.io.IOException: Connect failed 
    at com.sun.mail.pop3.POP3Store.protocolConnect(POP3Store.java:148) 
    at javax.mail.Service.connect(Service.java:275) 
    at javax.mail.Service.connect(Service.java:156) 
    at ReadYahooMail.<init>(ReadYahooMail.java:20) 
    at ReadYahooMail.main(ReadYahooMail.java:56) 
Caused by: java.io.IOException: Connect failed 
    at com.sun.mail.pop3.Protocol.<init>(Protocol.java:104) 
    at com.sun.mail.pop3.POP3Store.getPort(POP3Store.java:201) 
    at com.sun.mail.pop3.POP3Store.protocolConnect(POP3Store.java:144) 
    ... 4 more 

我使用下面的代碼

import java.io.*; 
import java.util.*; 
import javax.mail.*; 
public class ReadYahooMail { 
    public ReadYahooMail(){ 
     String host = "smtp.mail.yahoo.com"; 
    String user = "1234"; 
    String password = "*******"; 
try{ 
    // Get system properties 
    Properties properties = System.getProperties(); 

    // Get the default Session object. 
    Session session = Session.getDefaultInstance(properties); 

    // Get a Store object that implements the specified protocol. 
    Store store = session.getStore("pop3"); 

    //Connect to the current host using the specified username and password. 
    store.connect(host, user, password); 

    //Create a Folder object corresponding to the given name. 
    Folder folder = store.getFolder("inbox"); 

    // Open the Folder. 
    folder.open(Folder.READ_ONLY); 

    Message[] message = folder.getMessages(); 

    // Display message. 
    for (int i = 0; i < message.length; i++) { 

    System.out.println("------------ Message " + (i + 1) + " ------------"); 

    System.out.println("SentDate : " + message[i].getSentDate()); 
    System.out.println("From : " + message[i].getFrom()[0]); 
    System.out.println("Subject : " + message[i].getSubject()); 
    System.out.print("Message : "); 

    InputStream stream = message[i].getInputStream(); 
    while (stream.available() != 0) { 
    System.out.print((char) stream.read()); 
    } 
    System.out.println(); 
    } 

    folder.close(true); 
    store.close(); 
} 
catch(Exception e){ 
    e.printStackTrace(); 
} 

    } 
    public static void main(String args[]){ 
     ReadYahooMail r=new ReadYahooMail(); 
    } 

} 

回答

0

與其他電子郵件提供商不同,雅虎不提供免費的POP或IMAP服務。所以,這可能是IOException的原因。

+0

Quynh Ngugen適用於IMAP服務。通過GarciaPL給出的上述鏈接 – Sumit