• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 694
  • Last Modified:

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

0
Genus1
Asked:
Genus1
  • 2
1 Solution
 
Genus1Author Commented:
Please Help ME!
0
 
objectsCommented:
you need to connect to the Store

http://brandon.zarzoza.com/?p=14
0
 
Genus1Author Commented:
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?
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now