problems connecting to an IMAP server using JavaMail API?

Posted on 2009-04-17
Last Modified: 2013-11-24
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: 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!


import javax.mail.*;


import javax.mail.internet.*;

import java.util.*;



public class SearchClient {


  public static void main(String[] args) {


    if (args.length == 0) {


       "Usage: java SearchClient protocol://username@host/foldername");




    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.");





      BufferedReader reader = new BufferedReader (

      new InputStreamReader(;


	    // 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



	      } else if ("QUIT".equals(line)) {





      // Close the connection 





    catch (Exception ex) {




    // Since we may have brought up a GUI to authenticate,

    // we can't rely on returning from main() to exit







Open in new window

Question by:Genus1

    Author Comment

    Please Help ME!
    LVL 92

    Accepted Solution

    you need to connect to the Store

    Author Comment

    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?

    Featured Post

    Maximize Your Threat Intelligence Reporting

    Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity
    countClumps  challenge 10 70
    Return ZipOutputStream using Servlet 2 27
    Enhanced For loop vs ForEach Loop 4 44
    Updating Java 9 62
    After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
    For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
    Video by: Michael
    Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
    This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

    746 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now