title: JavaMail Connection using Main Method and Some other Thread
body: <p>I am trying to connect to a gmail inbox to listen to messages using the JavaMail api. I can connect fine to the mailbox when I run my program through the main method. However when I create a separate thread and run it using some other class it seems that I get the following exception.</p>
  
  <pre><code>javax.mail.MessagingException: null; nested exception is:
      java.io.IOException
      at com.sun.mail.imap.IMAPStore.protocolConnect(IMAPStore.java:298)
      at javax.mail.Service.connect(Service.java:234)
      at javax.mail.Service.connect(Service.java:135)
      at com.emdi.sl3.server.emailConnector.EmailListenerAcknowledge.run(Email ListenerAcknowledge.java:92)
      at java.lang.Thread.run(Unknown Source)
      at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
      at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
      at java.lang.Thread.run(Unknown Source)`
  </code></pre>
  
  <p>My code to connect to the mailbox looks like,</p>
  
  <pre><code>import java.util.*;
  import javax.mail.*;
  import javax.mail.event.*;
  
  import com.sun.mail.imap.*;
  
  public class EmailListenerAcknowledge implements Runnable {
  
      private Store store;
      private Folder folder;
      String host;
      String user;
      String password;
  
      public void run() {
  
          try {
              // TODO: Set email protocol, username and password.
              host = "imap.gmail.com";
              user = "fdsfds";
              password = "sdfsff";
  
              String mbox = "inbox";
  
              String frequency = "1";
  
              System.out.println("\nTesting monitor\n");
  
              Properties props = new Properties();
              props.setProperty("mail.imap.ssl.enable", "true");
              props.setProperty("mail.imap.port", "993"); 
  
              // Get a Session object
              Session session = Session.getInstance(props);
  
              // Get a Store object
              store = session.getStore("imap");
  
              // Connect
              store.connect(host, user, password);
              System.out.println("Connected!!!");
  
              // Open a Folder
              folder = store.getFolder(mbox);
              if (folder == null || !folder.exists()) {
                  System.out.println("Invalid folder");
                  System.exit(1);
              }
  
              folder.open(Folder.READ_ONLY);
          } catch (Exception ex) {
              ex.printStackTrace();
          }
      }
  }
  </code></pre>
  
  <p>I also notice that when I remove the line <code>props.setProperty("mail.imap.port", "993")</code>, I get a different exception (again running the program from the main method works fine),</p>
  
  <pre><code>javax.mail.MessagingException: Connection timed out: connect; nested exception is:java.net.ConnectException: Connection timed out: connect
      at com.sun.mail.imap.IMAPStore.protocolConnect(IMAPStore.java:298)
      at javax.mail.Service.connect(Service.java:234)
      at javax.mail.Service.connect(Service.java:135)
      at com.emdi.sl3.server.emailConnector.EmailListener.run(Email ListenerAcknowledge.java:92)
      at java.lang.Thread.run(Unknown Source)
      at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
      at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
      at java.lang.Thread.run(Unknown Source)`
  </code></pre>
  
  <p>I seem not to understand why running the program from the main method works fine whereas running the program from a separate thread don't work. </p>