Genus1
asked on
problems connecting to an IMAP server using JavaMail API?
I am trying to write code to retrieve messages from an IMAP server such as Gmail.
I have attached my code below but when I run it I get the error :
javax.mail.MessagingExcept ion: Connection timed out: connect;
nested exception is:
java.net.ConnectException: Connection timed out: connect
From my analysis I can't seem to ge past this line in the code:
Folder folder = session.getFolder(server);
Then I get the timeout.
I am not sure what I am doing wrong. Please help!
Thanks.
I have attached my code below but when I run it I get the error :
javax.mail.MessagingExcept
nested exception is:
java.net.ConnectException:
From my analysis I can't seem to ge past this line in the code:
Folder folder = session.getFolder(server);
Then I get the timeout.
I am not sure what I am doing wrong. Please help!
Thanks.
import javax.mail.*;
import javax.mail.search.*;
import javax.mail.internet.*;
import java.util.*;
import java.io.*;
public class SearchClient {
public static void main(String[] args) {
if (args.length == 0) {
System.err.println(
"Usage: java SearchClient protocol://username@host/foldername");
return;
}
URLName server = new URLName(args[0]);
try {
Session session = Session.getDefaultInstance(new Properties(),
new MailAuthenticator(server.getUsername()));
System.out.println("I am here1");
// Connect to the server and open the folder
Folder folder = session.getFolder(server);
System.out.println("I am here2");
if (folder == null) {
System.out.println("Folder " + server.getFile() + " not found.");
System.exit(1);
}
folder.open(Folder.READ_ONLY);
BufferedReader reader = new BufferedReader (
new InputStreamReader(System.in));
// Get directory
Message message[] = folder.getMessages();
for (int i=0, n=message.length; i<n; i++) {
// Display from field and subject
System.out.println(i + ": " + message[i].getFrom()[0]
+ "\t" + message[i].getSubject());
System.out.println("Do you want to read message? [YES to read/QUIT to end]");
String line = reader.readLine();
if ("YES".equals(line)) {
// Display message content
System.out.println(message[i].getContent());
} else if ("QUIT".equals(line)) {
break;
}
}
// Close the connection
folder.close(false);
}
catch (Exception ex) {
ex.printStackTrace();
}
// Since we may have brought up a GUI to authenticate,
// we can't rely on returning from main() to exit
System.exit(0);
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks so much.
It is funny this code I was testing was mostly from a textbook so you think that they would mention the Store?
It is funny this code I was testing was mostly from a textbook so you think that they would mention the Store?
ASKER