title: Unable to read Email using IMAP in JAVA.
body: <p>I am trying to read email from Mail Server using JAVA. I've used IMAP protocol for that.</p>
  
  <p>Everytime when I send request to read an email it throws me exception saying: <em>javax.mail.MessagingException: Unrecognized SSL message, plaintext connection?</em></p>
  
  <p>Here's my Code;</p>
  
  <pre><code>public void readEmail() throws Exception {
      sessionIMAP = setupImap();
      Store store = sessionIMAP.getStore("imap");
      store.connect("localhost", 1143, "username", "password");
      if (store.isConnected()) {
          System.out.println("IMAP is connected");
      } else {
          System.out.println("IMAP is not connected");
      }
  
  }
  
  private Session setupImap() {
      System.out.println("in setupImap");
      Session session1;
      Properties props = new Properties();
      props.put("mail.imap.starttls.enable", "true");
      //props.put("mail.imaps.ssl.enable", "true");
      //props.put("mail.imaps.auth", "true");
      //props.put("mail.imaps.ssl.trust", "*");
      props.put("mail.imap.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
      props.put("mail.imap.socketFactory.fallback", "false");
      props.put("mail.imap.host", "localhost");
      props.put("mail.imap.port", 1143);
      props.put("mail.imap.socketFactory.port", 1143);
      props.setProperty("mail.store.protocol", "imap");
      props.put("mail.debug", "true");
      //props.put("mail.imaps.class", "com.sun.mail.imap.IMAPSSLStore");
      session1 = Session.getInstance(props, null);
      //setupIMAPMsgTypeSetupForReportedIssue();
      return session1;
  }
  </code></pre>
  
  <p>Here's the full Exception I am getting:</p>
  
  <pre><code>    DEBUG: JavaMail version 1.4.6
  DEBUG: successfully loaded resource: /META-INF/javamail.default.providers
  DEBUG: Tables of loaded providers
  DEBUG: Providers Listed By Class Name: {com.sun.mail.smtp.SMTPSSLTransport=javax.mail.Provider[TRANSPORT,smtps,com.sun.mail.smtp.SMTPSSLTransport,Oracle], com.sun.mail.smtp.SMTPTransport=javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Oracle], com.sun.mail.imap.IMAPSSLStore=javax.mail.Provider[STORE,imaps,com.sun.mail.imap.IMAPSSLStore,Oracle], com.sun.mail.pop3.POP3SSLStore=javax.mail.Provider[STORE,pop3s,com.sun.mail.pop3.POP3SSLStore,Oracle], com.sun.mail.imap.IMAPStore=javax.mail.Provider[STORE,imap,com.sun.mail.imap.IMAPStore,Oracle], com.sun.mail.pop3.POP3Store=javax.mail.Provider[STORE,pop3,com.sun.mail.pop3.POP3Store,Oracle]}
  DEBUG: Providers Listed By Protocol: {imaps=javax.mail.Provider[STORE,imaps,com.sun.mail.imap.IMAPSSLStore,Oracle], imap=javax.mail.Provider[STORE,imap,com.sun.mail.imap.IMAPStore,Oracle], smtps=javax.mail.Provider[TRANSPORT,smtps,com.sun.mail.smtp.SMTPSSLTransport,Oracle], pop3=javax.mail.Provider[STORE,pop3,com.sun.mail.pop3.POP3Store,Oracle], pop3s=javax.mail.Provider[STORE,pop3s,com.sun.mail.pop3.POP3SSLStore,Oracle], smtp=javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Oracle]}
  DEBUG: successfully loaded resource: /META-INF/javamail.default.address.map
  DEBUG: getProvider() returning javax.mail.Provider[STORE,imap,com.sun.mail.imap.IMAPStore,Oracle]
  DEBUG IMAP: mail.imap.fetchsize: 16384
  DEBUG IMAP: mail.imap.ignorebodystructuresize: false
  DEBUG IMAP: mail.imap.statuscachetimeout: 1000
  DEBUG IMAP: mail.imap.appendbuffersize: -1
  DEBUG IMAP: mail.imap.minidletime: 10
  DEBUG IMAP: enable STARTTLS
  DEBUG IMAP: trying to connect to host "localhost", port 1143, isSSL false
  Invalid Args: read_Email
  java.lang.reflect.InvocationTargetException
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at java.lang.reflect.Method.invoke(Method.java:498)
      at com.thalesifec.framework.tool.support.FwkAnnotatedTools$FwkToolClassInfo.execute(FwkAnnotatedTools.java:103)
      at com.thalesifec.framework.tool.support.interactive.ConsoleInterface.handleToolSelected(ConsoleInterface.java:217)
      at com.thalesifec.framework.tool.support.interactive.ConsoleInterface.interactiveCommandLoop(ConsoleInterface.java:113)
      at com.thalesifec.framework.tool.support.interactive.ConsoleInterface.noArgsStart(ConsoleInterface.java:271)
      at com.thalesifec.framework.tool.support.interactive.ConsoleInterface.delegateCommand(ConsoleInterface.java:295)
      at com.thalesifec.framework.connectivitymanager.simulator.Main.main(Main.java:28)
  Caused by: javax.mail.MessagingException: Unrecognized SSL message, plaintext connection?;
    nested exception is:
      javax.net.ssl.SSLException: Unrecognized SSL message, plaintext connection?
      at com.sun.mail.imap.IMAPStore.protocolConnect(IMAPStore.java:670)
      at javax.mail.Service.connect(Service.java:295)
      at com.thalesifec.framework.connectivitymanager.simulator.plugins.EMBATLTestTool.readEmail(EMBATLTestTool.java:136)
      ... 10 more
  Caused by: javax.net.ssl.SSLException: Unrecognized SSL message, plaintext connection?
      at sun.security.ssl.InputRecord.handleUnknownRecord(InputRecord.java:710)
      at sun.security.ssl.InputRecord.read(InputRecord.java:527)
      at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:973)
      at sun.security.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1375)
      at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1403)
      at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1387)
      at com.sun.mail.util.SocketFetcher.configureSSLSocket(SocketFetcher.java:549)
      at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:354)
      at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:211)
      at com.sun.mail.iap.Protocol.&lt;init&gt;(Protocol.java:116)
      at com.sun.mail.imap.protocol.IMAPProtocol.&lt;init&gt;(IMAPProtocol.java:115)
      at com.sun.mail.imap.IMAPStore.newIMAPProtocol(IMAPStore.java:685)
      at com.sun.mail.imap.IMAPStore.protocolConnect(IMAPStore.java:636)
      ... 12 more
  </code></pre>
  
  <p>Could you please let me know what I am doing wrong?</p>
  
  <p>Thanks in Advance</p>