Connection Exception : Refused

import java.io.*;

import java.util.*;

import javax.mail.*;

import javax.mail.internet.*;

import java.util.*;

import org.apache.axis.AxisFault;

publicclass Sample{

//mail session state related

private String m_Username;

private String m_PostOfficeHost;

private Session m_MailSession;

private Store m_Store;

private Folder m_Inbox;

publicvoid displayDet()throws Exception{

String resultString="";

try{

m_MailSession = Session.getInstance(System.getProperties(),null);

//setup store and connect to it

String proto ="imap";

m_Store = m_MailSession.getStore(proto);

m_Username="ramki";

m_PostOfficeHost="mymail.com";

m_Store.connect(m_PostOfficeHost,m_Username,"password");

System.out.println(m_Username+"@"+m_PostOfficeHost+"SUCCESSFUL LOGIN");

m_Inbox = m_Store.getFolder("INBOX");

}catch (Exception e){

e.printStackTrace();

}

String messageCountString;

//List messageCountList = new ArrayList();

StringBuffer sbf =new StringBuffer();

sbf.append("Total Messages:" + m_Inbox.getMessageCount());

sbf.append(",");

sbf.append("New Messages:" + m_Inbox.getNewMessageCount());

sbf.append(",");

sbf.append("UnreadMessages:" + m_Inbox.getUnreadMessageCount());

messageCountString=sbf.toString();

System.out.println(messageCountString);

try{

//close store

m_Store.close();

System.out.println(m_Username+"@"+m_PostOfficeHost+"SUCCESS LOGOUT");

}catch (Exception ex){

ex.printStackTrace();

}

}//displayDet

publicstaticvoid main(String args[])throws Exception{

Sample s=new Sample();

s.displayDet();

}// main

}// Sample

The error is :

[root@mydomain webmail]# javac Sample.java

[root@mydomain webmail]# java Sample

javax.mail.MessagingException: Connection refused;

nested exception is:

java.net.ConnectException: Connection refused

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 Sample.displayDet(Sample.java:26)

at Sample.main(Sample.java:57)

Exception in thread"main" java.lang.NullPointerException

at Sample.displayDet(Sample.java:37)

at Sample.main(Sample.java:57)

[root@igb10033 webmailuserservice]#

Could any one have an idea as to why we have such an exception?. I have another program with similar lines which is working fine(without being changing the username and password credentials.)

Appreciate any suggestion

[4643 byte] By [passion_for_javaa] at [2007-11-27 7:08:17]
# 1
The JavaMail FAQ has tips on debugging connection problems.Start there.
bshannona at 2007-7-12 18:59:46 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 2

Hi,

when i tried with

telnet localhost 143 the connection has worked fine.

but when i tried using

telnet mymail.com 143 i got an error "connection refused".

I am sure the imap server is running fine as i am able to login through webmail interface and able to fetch mails in the inbox.

passion_for_javaa at 2007-7-12 18:59:46 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 3

Web mail and IMAP are completely different things. A server can easily

support one without the other.

It looks like your server isn't supporting IMAP. If you think it should be,

you'll need to talk to your network administrator or system administrator

to find out what's going wrong.

bshannona at 2007-7-12 18:59:46 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 4

Thanks for your reply. I have figured out the problem. I was wrongly passing the value for parameter m_PostOfficeHost. I have passed the hostname mymail.com instead of complete hoststring like (FQDN). for method

m_Store.connect(m_PostOfficeHost,m_Username,"password");

One more thing i wanted to know is whether the following usage is correct or not.

I have tried and it did not work out. Am I not understanding the usage of Message object correctly?

Please help.

Message msg[] = m_Inbox.getMessages();

for(int index=0;index<msg.length;index++)

{

sbf.append("Message no. "+index+" Subject :"+msg[index].getSubject());

sbf.append(",");

}>

passion_for_javaa at 2007-7-12 18:59:46 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 5

[root@igb10033 webmailuserservice]# java Sample

Connection status: false

Get URL name :imap:

ramki@mymail.com@ind123433.in.companyname.comSUCCESSFUL LOGIN

Exception in thread "main" java.lang.IllegalStateException: Folder not open

at javax.mail.Folder.getMessages(Folder.java:853)

at Sample.displayDet(Sample.java:51)

at Sample.main(Sample.java:72)

I got this exception. "Folder not open" what does it mean?

passion_for_javaa at 2007-7-12 18:59:46 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 6

I have modified the code :

m_Inbox.open(Folder.READ_ONLY);

System.out.println("Folder is open? :"+m_Inbox.isOpen());

Message msg=m_Inbox.getMessage(1);

System.out.println("Subject: "+msg.getFrom());

m_Inbox.close(false);

System.out.println("Folder is open? :"+m_Inbox.isOpen());

Now I get this exception:

[root@ind123433 webmailuserservice]# javac Sample.java

[root@ind123433 webmailuserservice]# java Sample

Connection status: false

Get URL name :imap:

ramki@mymail.com@ind123433.in.companyname.comSUCCESSFUL LOGIN

Exception in thread "main" java.lang.NoClassDefFoundError: javax/activation/Data

Source

at com.sun.mail.imap.IMAPFolder.open(IMAPFolder.java:790)

at Sample.displayDet(Sample.java:50)

at Sample.main(Sample.java:77)

[root@ind123433 webmailuserservice]#

passion_for_javaa at 2007-7-12 18:59:46 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 7
I keep hoping if I wait long enough you'll figure this all out yourself! :-)At this point it's pretty clear that you don't have activation.jar in your class path.
bshannona at 2007-7-12 18:59:46 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 8

Bshannon,

Ya... Its been a good day debugging step by step. I always love learning. And learning Java is passion.

I was trying to compile program on a stand alone basis (instead of putting it in webapps/webfolder/classes/lib). Now that I have the class file in place its working fine. Thanks for all your patience and ideas.

passion_for_javaa at 2007-7-12 18:59:46 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...