Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java mail - Couldn't connect to host

I am trying to send an email Java application, following guide on http://www.tutorialspoint.com/java/java_sending_email.htm

When I try to run it, copypasted first code from link above, I get an error that it could not connect to host, which is a localhost. Here is a result when I ping localhost:

Pinging Home-PC [::1] with 32 bytes of data:
Reply from ::1: time<1ms
Reply from ::1: time<1ms
Reply from ::1: time<1ms
Reply from ::1: time<1ms

Since I am greatly inexperienced in communications, I do not know what went wrong, and yet again, I need help. So, is the issue in my localhost not being properly configured, or is something wrong with my code? What could I do?

I am using Tomcat v8.0 server.

I am also adding console output:

com.sun.mail.util.MailConnectException: Couldn't connect to host, port: localhost, 25; timeout -1;
nested exception is:
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:2054)
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:697)
at javax.mail.Service.connect(Service.java:364)
at javax.mail.Service.connect(Service.java:245)
at javax.mail.Service.connect(Service.java:194)
at javax.mail.Transport.send0(Transport.java:253)
at javax.mail.Transport.send(Transport.java:124)
at test.Test.main(Test.java:51)
Caused by: java.net.ConnectException: Connection refused: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:79)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:345)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:589)
at java.net.Socket.connect(Socket.java:538)
at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:329)
at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:236)
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:2020)
... 7 more
like image 969
Sarpy Avatar asked Sep 02 '26 10:09

Sarpy


1 Answers

I assume, that you do not have an SMTP-Server installed on your local machine. For testing purposes, start an instace of FakeSMTP. If it is up and running, programs are able to connect and to drop their mail.

If you want to experiment a little (assuming you have a Windows box), there is Hamstermail (Site is in german - be warned ;-))

Here is a result when I ping localhost

That only shows that a server responds to an Echo Request. This does neither mean, that the server has a SMTP-service running, nor does it mean, if the server does not answer, that the server is down - sometimes, admins block ICMP-Echo-Requests.

Sometimes a telnet $server 25 allows connecting to the mail-server.

Caused by: java.net.ConnectException: Connection refused: connect

This shows, that the Service is not active aka the port is closed.

like image 200
Thomas Junk Avatar answered Sep 04 '26 00:09

Thomas Junk



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!