Failed to connect to the SMTP host: localhost, port: 25; Nested exception: java.net.ConnectException: connection rejected: connect

I am making an application to send email from localhost to jsp and I found an error, for example Failed to connect to the SMTP host: localhost, port: 25; nested exception: java.net.ConnectException: connection rejected: connect plz check and give me a solution or idea if you have one. I use the code below for this. Thank 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> 
+7
source share
2 answers

First you need to make sure that the SMTP server is listening on port 25.

To find out if you have a service, you can try using the TELNET client, for example:

 C:\> telnet localhost 25 

(the telnet client is disabled by default in recent versions of Windows, you need to add / enable the Windows component from the control panel. On Linux / UNIX, the telnet client is usually the default.

 $ telnet localhost 25 

If it waits a long time, it means that you do not have the necessary SMTP service. If you are successfully connected, you enter something and you can enter something, the service is there.

If you do not have a service, you can use the following functions:

  • A mock SMTP server that will mimic the behavior of a real SMTP server since you use Java, it is natural to offer Dumbster a fake SMTP server. This can even be done to work in JUnit tests (with installation / shutdown / verification) or independently run as a separate process for the integration test.
  • If your host is Windows, you can try installing the Mercury mail server (also comes with WAMPP from Apache friends) on your local computer before working on the code.
  • If your host is Linux or UNIX, try enabling an email service such as Postfix ,
  • Another full-blown SMTP server in Java, such as the Apache James mail server .

If you are sure that you already have a service, maybe SMTP requires additional security credentials. If you can tell me that the SMTP server is listening on port 25, I can tell you more.

+13
source

The mail server on CentOS 6 and other server platforms that support IPv6 can 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 you do not have two entries for localhost in /etc/hosts with different IP addresses, for example 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 that you have only one entry for localhost , preferably an IPv4 address, for example:

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

All Articles