when i tried to send an email to an account it does not sent. the error result will be displayed. i got an error in the apache tomcat bin log like this:
javax.mail.MessagingException: Could not connect to SMTP host: localhost, port:25;
nested exception is:
java.net.ConnectException: Connection refused: connect
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1934)
at com.sun.mail.smtp.SmtpTransport.protocolConnect(SMTPTransport.java:638)

at javax.mail.Service.connect(Service.java:295)
at javax.mail.Service.connect(Service.java:176)
at javax.mail.Service.connect(Service.java:125)
at javax.mail.Transport.send0(Transport.java:194)
at javax.mail.Transport.send(Transport.java:124)
at org.apache.jsp.mailer_jsp._jspService(mailer_jsp.java:87)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:98)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:371)


................


caused by: java.net.ConnectException: Connection refuse: connect
at java.net.TwoStacksPlainSocketImpl.socketConect(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:288)
at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:231)
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1990)
... 27 more

What is the solution of this error.? i need your help buddies..

Dani AI

Generated

The stack trace shows a network-level failure: the mail client tried to contact an SMTP server on localhost:25 and the TCP connection was refused. That means either (a) the JVM was actually configured to use a local MTA that isn’t running, or (b) the JavaMail Session in use did not pick up the intended SMTP host/port and fell back to the default (localhost:25). In practice this commonly happens when a shared Session is returned instead of a new one, or when required auth/TLS settings for an external provider (Yahoo/Gmail) are missing.

Practical fixes and checks:

  • Make a fresh Session with the real SMTP settings (use Session.getInstance(...), not the shared default session) and supply an Authenticator when the server requires login. Set the correct port and enable STARTTLS or SSL depending on the provider. Enable JavaMail debug to see which host/port and security mode are actually used.
  • Test basic connectivity from the Tomcat host (telnet or openssl s_client to the SMTP host:port) to rule out firewall/ISP blocking (port 25 is commonly blocked; 587 or 465 may be required).
  • For public providers, require username/password and encryption — setting only a host name without auth/TLS will usually fail or be refused.
  • Avoid sending credentials or transport logic directly from a JSP. Move mail logic to a servlet or helper class and keep credentials in protected configuration (environment variables, servlet context, or a secure keystore).

Enable session debug and capture the full debug output; it will show the host/port attempts and TLS handshakes and make it easy to confirm whether the properties were applied or the app fell back to localhost. ’s stack trace points at a connectivity issue rather than message construction, so focus first on session configuration and network reachability.

<%@ page import="java.io.*,java.util.*,javax.mail.*"%>
<%@ page import="javax.mail.internet.*,javax.activation.*"%>
<%@ page import="javax.servlet.http.*,javax.servlet.*" %>
<%
   String result;
   // Recipient's email ID needs to be mentioned.
   String to = request.getParameter("to");

   // Sender's email ID needs to be mentioned
   String from = request.getParameter("from");

   String host = "smtp.mail.yahoo.com";

   // Get system properties object
   Properties properties = System.getProperties();

   // Setup mail server
   properties.setProperty("mail.smtp.host", host);

   // Get the default Session object.
   Session mailSession = Session.getDefaultInstance(properties);

   try{
      // Create a default MimeMessage object.
      MimeMessage message = new MimeMessage(mailSession);
      // Set From: header field of the header.
      message.setFrom(new InternetAddress(from));
      // Set To: header field of the header.
      message.addRecipient(Message.RecipientType.TO,
                               new InternetAddress(to));
      // Set Subject: header field
      message.setSubject("This is the Subject Line!");
     
      // Send the actual HTML message, as big as you like
      message.setContent("<h1>This is actual message</h1>",
                            "text/html" );
      // Send message
      Transport.send(message);
      result = "Sent message successfully....";
   }catch (MessagingException mex) {
      mex.printStackTrace();
      result = "Error: unable to send message....";
   }
%>
<html>
<head>
<title>Send HTML Email using JSP</title>
</head>
<body>
<center>
<h1>Send Email using JSP</h1>
</center>
<p align="center">
<% 
   out.println("Result: " + result + "\n");
%>
</p>
</body>
</html>

that is the code.. what is the problem with the code?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.