MailConnectException in Java - java

I am try to take unread mails from gmail. There are lots of example code and I used them. The problem is, code doesn't work in every area. For example when I was at home the code works but in working area it is not. I think it is about network settings or different ports etc...
I am using the code that is already implemented in this a link. (First answer)
When I run that script at home it works. In my working are, it doesn't and the error message's is at below.
The question is how can i learn the which port should I use ? or Should I do another thing that I miss ?
Any directions, links or codes are appreciated.
com.sun.mail.util.MailConnectException: Couldn't connect to host, port: smtp.gmail.com, 587; timeout -1;
nested exception is:
java.net.ConnectException: Connection timed out: connect
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1961)
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:654)
at javax.mail.Service.connect(Service.java:345)
at javax.mail.Service.connect(Service.java:226)
at gmailYeni.SendMail.sendFromGMail(SendMail.java:52)
at gmailYeni.SendMail.main(SendMail.java:20)
`Caused by: java.net.ConnectException: Connection timed out: connect
at java.net.TwoStacksPlainSocketImpl.socketConnect(Native Method)
at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:297)
at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:229)
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1927)
... 5 more
`

It seems you are sending mail from gmail. Then please check if your firewall allows access to gmail. If yes, try changing port# from 25 to 465 and try. But it seems an issue that your firewall is blocking access to gmail.

Related

Exception in sending email from java via gmail

I am getting the following exception while trying send an email from java via gmail.
javax.mail.MessagingException: Could not connect to SMTP host: smtp.gmail.com, port: 465;
nested exception is:
java.net.ConnectException: Connection refused: connect
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1391)
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:412)
at javax.mail.Service.connect(Service.java:288)
at javax.mail.Service.connect(Service.java:169)
at Mail.Send(Mail.java:75)
at Mail.main(Mail.java:85)
Caused by: java.net.ConnectException: Connection refused: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at sun.security.ssl.SSLSocketImpl.connect(Unknown Source)
at sun.security.ssl.BaseSSLSocketImpl.connect(Unknown Source)
at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:233)
at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:189)
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1359)
... 5 more
I referred http://www.tutorialspoint.com/javamail_api/javamail_api_gmail_smtp_server.htm and Send email using java but same exception is coming both for SSL and TLS. Any fix for this?
Thanks in advance.
You will need to enable SMTP access in GMAIL, also you need to allow unsecure clients for your account:
https://www.google.com/settings/security/lesssecureapps
https://support.google.com/mail/answer/75726
Be aware that the smtp port you want to use is open in your firewall! I suggest to try a connection check from the machine where your code is running with Thunderbird if possible. Then you know if the client is able to connect.
After you to enable SMTP access in GMAIL, also you need to allow unsecure clients for your account ( as suggested by LucasF)
Try this configuration :
host :smtp.gmail.com
port :587
username :you#gmail.com
password :yourpassword
JavaMailProperties :
mail.transport.protocol : smtp
mail.smtp.auth : true
mail.smtp.starttls.enable :true
mail.debug :true
Me I use this configuration with import org.springframework.mail.SimpleMailMessage class and it's works.

RMI connection refused on localhost

I am trying to learn RMI coding and when I run server side of RMI I get connection refused.
This is my server main method
public static void main(String[] args)throws Exception {
Implementation impl=new Implementation();
Naming.rebind("//localhost:2020/RMI", impl);
System.out.println("Implementation has been bind to the name RMI and is ready for use");
}
I believe that the code for Implementation does not matter as it simply is the implemented interface that will run the code. The exception I am getting is this
Exception in thread "main" java.rmi.ConnectException: Connection refused to host: localhost; nested exception is:
java.net.ConnectException: Connection refused: connect
at sun.rmi.transport.tcp.TCPEndpoint.newSocket(Unknown Source)
at sun.rmi.transport.tcp.TCPChannel.createConnection(Unknown Source)
at sun.rmi.transport.tcp.TCPChannel.newConnection(Unknown Source)
at sun.rmi.server.UnicastRef.newCall(Unknown Source)
at sun.rmi.registry.RegistryImpl_Stub.rebind(Unknown Source)
at java.rmi.Naming.rebind(Unknown Source)
at epl362Project.Server.main(Server.java:10)
Caused by: java.net.ConnectException: Connection refused: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at sun.rmi.transport.proxy.RMIDirectSocketFactory.createSocket(Unknown Source)
at sun.rmi.transport.proxy.RMIMasterSocketFactory.createSocket(Unknown Source)
... 7 more
What am I doing wrong? both client and server sides will run on my laptop as this is just an exercise. Am I missing something or is there something wrong with my code? Please besides giving me the answer explain why it is so since I do not just want to make it work, I am trying to learn.
**EDIT
I found out why my code was not working. I was missing a line of code that said
LocateRegistry.createRegistry(2020);
which I found from other questions here on stackoverflow. But there was no explanation nor did I find one online as to why this code was needed for RMI to work properly. Does anyone have an explanation?
The target of Naming.rebind() is the RMI Registry. It has to be running. It wasn't running. So you couldn't connect to it. So you got a ConnectException.
I believe that the code for Implementation does not matter as it simply is the implemented interface that will run the code.
This is both meaningless and irrelevant. Interfaces do not 'run the code'. Methods in classes run the code, and in this case the implementation class runs the code. However your problem is not in 'run[ning] the code', it is in connecting to the Registry.

java.sql.SQLException: Network error IOException: No route to host?

14:51:34,652 DEBUG [mchange.v2.resourcepool.BasicResourcePool ] - An exception occurred while acquiring a poolable resource. Will retry.
java.sql.SQLException: Network error IOException: No route to host: connect
at net.sourceforge.jtds.jdbc.JtdsConnection.<init>(JtdsConnection.java:436)
at net.sourceforge.jtds.jdbc.Driver.connect(Driver.java:184)
at com.mchange.v2.c3p0.DriverManagerDataSource.getConnection(DriverManagerDataSource.java:134)
at com.mchange.v2.c3p0.WrapperConnectionPoolDataSource.getPooledConnection(WrapperConnectionPoolDataSource.java:182)
at com.mchange.v2.c3p0.WrapperConnectionPoolDataSource.getPooledConnection(WrapperConnectionPoolDataSource.java:171)
at com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool$1PooledConnectionResourcePoolManager.acquireResource(C3P0PooledConnectionPool.java:137)
at com.mchange.v2.resourcepool.BasicResourcePool.doAcquire(BasicResourcePool.java:1014)
at com.mchange.v2.resourcepool.BasicResourcePool.access$800(BasicResourcePool.java:32)
at com.mchange.v2.resourcepool.BasicResourcePool$AcquireTask.run(BasicResourcePool.java:1810)
at com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread.run(ThreadPoolAsynchronousRunner.java:547)
Caused by: java.net.NoRouteToHostException: No route to host: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at net.sourceforge.jtds.jdbc.SharedSocket.createSocketForJDBC3(SharedSocket.java:288)
at net.sourceforge.jtds.jdbc.SharedSocket.<init>(SharedSocket.java:251)
at net.sourceforge.jtds.jdbc.JtdsConnection.<init>(JtdsConnection.java:331)
Can anyone tell me:
How can resolve this issue ?
This is because you get back an ICMP "destination host unreachable" message (type 3, code 1).
This means either of those things:
you don't have a route to the destination host in your routing table (or a router you use doesn't have one);
a firewall sends this message back to you.
In the first case, fix the route if you can (maybe you have forgotten to initiate a VPN?). In the second case, ask the firewall admin why this occurs (luckily, he/she won't answer you "because yes" but give you a real reason :p)
This could occur if you are not connected to the server. Try pinging it to make sure.
check the iptables service is stopped.
"service iptables status"
And verify from your system by the following command,
"telnet server_ip 3306"
ref: https://community.oracle.com/thread/1335778?start=0

Refusing connection while trying to connect localhost

I was trying to develop a p2p file transfer application on java and as for the beginning I decided to run some transfer tests using localhost, as for the server, between local drives by some codes I found on the internet marked as working. The problem is for every port number I tried so far(+20) I got a "connection refused" error. I've installed microsoft loopback adapter as precaution, yet couldn't find any way to solve it. Any help would be appreciated.
just in case, I'm writing some code part related to socket in the client class.
// localhost for testing
Socket sock = new Socket("127.0.0.1",15123);
System.out.println("Connecting...");
and here is the error message
Exception in thread "main" java.net.ConnectException: Connection refused: connect
at java.net.TwoStacksPlainSocketImpl.socketConnect(Native Method)
at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at Client.main(Client.java:12)
line 12 is Socket sock = servsock.accept();
Most often , I saw that this exception comes when there is no service available to listen.Try to use another(free) port and make it sure that your server is running.You can find free port by writing netstat -a -o -n in cmd(on windows)

Java sockets - java.net.ConnectException: Connection refused: connect

I've created a simple chat program which communicates using sockets. Everything works fine when I'm running it on localhost. However, the problems occur when I try to link the client and server programs using my IP.
http://www.canyouseeme.org/ can connect to my server on port 9999 so I know that the server is fine and the port is open. However, my client can't connect.
The error log...
java.net.ConnectException: Connection refused: connect
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(Unknown Source)
at java.net.PlainSocketImpl.connectToAddress(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at Client.connect(Client.java:129)
at Client.main(Client.java:47)
Does anybody have any idea what might be causing this? Thanks in advance.
Edit:
Links to the full source code:
http://pastebin.com/2XftHtn9
Have a look at the answers to: java.net.ConnectException: Connection refused
My first suspicion however would be a firewall issue.....
Is your client on same LAN as your server? I think you should re-check IP address / host name and port number to which your client is connecting.

Categories