3

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
2
  • 2
    Answering pings doesn't mean the SMTP port is open... Did you check if the port is open? Commented Apr 30, 2015 at 17:40
  • You must change properties with your own properties.It will be better if you post your code and we can see what is problem. Commented Apr 30, 2015 at 17:43

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Oh, thank you very much, I am making progress now! When I start instance of FakeSMTP, it is recieving mails. So,does this mean I must somehow configure my Tomcat server, if I want to send actual mails?
You need an SMTP-server to actually send mail. Beware of sending mail from your localhost - most of the time these are marked as Spam. It would be better to choose a known mail provider.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.