8

I'm doing application for send email from localhost in jsp & i found error like Could not connect to SMTP host: localhost, port: 25; nested exception is: java.net.ConnectException: Connection refused: connect plz check and give me solution or idea if you have .for that i'm using below code . Thanking you in advance.

 <%@ page language="java" import="javax.naming.*,java.io.*,javax.mail.*, javax.mail.internet.*,com.sun.mail.smtp.*"%> <html> <head> <title>Mail</title> </head> <body> <% try{ Session mailSession = Session.getInstance(System.getProperties()); Transport transport = new SMTPTransport(mailSession,new URLName("localhost")); transport.connect("localhost",25,null,null); MimeMessage m = new MimeMessage(mailSession); m.setFrom(new InternetAddress(%><%request.getParameter("from")%><%)); Address[] toAddr = new InternetAddress[] { new InternetAddress(%><%request.getParameter("to")%><%) }; m.setRecipients(javax.mail.Message.RecipientType.TO, toAddr ); m.setSubject(%><%request.getParameter("subject")%><%); m.setSentDate(new java.util.Date()); m.setContent(%><%request.getParameter("description")%><%, "text/plain"); transport.sendMessage(m,m.getAllRecipients()); transport.close(); out.println("Thanks for sending mail!"); } catch(Exception e){ out.println(e.getMessage()); e.printStackTrace(); } %> </body> </html> 
2
  • 25 may not be the port. many smtp servers use a different port. For example eatj java hosting uses 487. Commented Apr 1, 2011 at 20:59
  • make sure your transport instance is properly closed, put it in try finally block to be sure of it. Commented Dec 16, 2016 at 11:00

2 Answers 2

15

First you have to ensure that there is a SMTP server listening on port 25.

To look whether you have the service, you can try using TELNET client, such as:

C:\> telnet localhost 25 

(telnet client by default is disabled on most recent versions of Windows, you have to add/enable the Windows component from Control Panel. In Linux/UNIX usually telnet client is there by default.

$ telnet localhost 25 

If it waits for long then time out, that means you don't have the required SMTP service. If successfully connected you enter something and able to type something, the service is there.

If you don't have the service, you can use these:

  • A mock SMTP server that will mimic the behavior of actual SMTP server, as you are using Java, it is natural to suggest Dumbster fake SMTP server. This even can be made to work within JUnit tests (with setup/tear down/validation), or independently run as separate process for integration test.
  • If your host is Windows, you can try installing Mercury email server (also comes with WAMPP package from Apache Friends) on your local before running above code.
  • If your host is Linux or UNIX, try to enable the mail service such as Postfix,
  • Another full blown SMTP server in Java, such as Apache James mail server.

If you are sure that you already have the service, may be the SMTP requires additional security credentials. If you can tell me what SMTP server listening on port 25 I may be able to tell you more.

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

4 Comments

I installed mercury email server but still nothing... telnet cannot open connection
can you run "nslookup localhost". I am interested on which IP address it maps to (IPv4 or IPv6)?
What was your solution? Please share
The idea is to have a server side application to talk to. Now, as this has been an old answer to the question (2011 back then), I am updating this answer, as there seemed to be a lot of people still wondering how to do this.
1

The mail server on CentOS 6 and other IPv6 capable server platforms may be bound to IPv6 localhost (::1) instead of IPv4 localhost (127.0.0.1).

Typical symptoms:

[root@host /]# telnet 127.0.0.1 25 Trying 127.0.0.1... telnet: connect to address 127.0.0.1: Connection refused [root@host /]# telnet localhost 25 Trying ::1... Connected to localhost. Escape character is '^]'. 220 host ESMTP Exim 4.72 Wed, 14 Aug 2013 17:02:52 +0100 [root@host /]# netstat -plant | grep 25 tcp 0 0 :::25 :::* LISTEN 1082/exim 

If this happens, make sure that you don't have two entries for localhost in /etc/hosts with different IP addresses, like this (bad) example:

[root@host /]# cat /etc/hosts 127.0.0.1 localhost.localdomain localhost localhost4.localdomain4 localhost4 ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6 

To avoid confusion, make sure you only have one entry for localhost, preferably an IPv4 address, like this:

[root@host /]# cat /etc/hosts 127.0.0.1 localhost localhost.localdomain localhost4.localdomain4 localhost4 ::1 localhost6 localhost6.localdomain6 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.