我想阅读来自雅虎邮件的未读邮件。所以我使用了以下代码,但它给了我错误
如下
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();
}
}