Cannot connect to mailserver using javamail

1.1k views Asked by At

I am trying to connect to IMAP server to read mails knowing that the IMAP mailserver is a local APache JAMES Mailserver. When using pop3 protocol, everything succeed, sending / retrieving messages between clients... But when i changed the protocol to imap, connection exceptiion is being thrown i dunno why...

This is My code :

 @Stateless
 public class Mailer{

 @Resouces(name = "mail/javamailsession")
 private Session session;

 public void connect(){
 IMAPStore store = (IMAPStore)session.getStore("imap");
 store.connect("localhost","[email protected]","myPass");
 ...
 ...

The exception is being thrown from here at :

 store.connect(...

Exception is :

    java.mail.MessagingException : Connection refused : connect;
     nested exception is :
     java.net.ConnectionException: Connection refused : connect

Knowing that the result of telnet process is :

   Me  :     telnet localhost imap
   Cmd :     Ok James IMAP4rev1 server server pc_name is ready
   Me  :     01 login test@localhost 123456
   Cmd :     01 Ok login completed

This means in a way or another that the imap is enabled in the server... So why should such exception occur :S Anybody has an idea ?

1

There are 1 answers

4
Bill Shannon On BEST ANSWER

Most likely you have a firewall or anti-virus product that's allowing the connection from telnet but preventing the connection from your application. It looks like your application is a Java EE application. I assume the Java EE server is running on the same machine that the mail server is on, and that you ran telnet from.