Link to home
Start Free TrialLog in
Avatar of Genus1
Genus1Flag for Canada

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.MessagingException: 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.


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);     
    
  }
  
  
}

Open in new window

Avatar of Genus1
Genus1
Flag of Canada image

ASKER

Please Help ME!
ASKER CERTIFIED SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Genus1

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?