javax.mail.MessagingException: Could not connect to SMTP host: localhost, port: 25


I am getting issue while sending email.

javax.mail.SendFailedException: Sending failed;
  nested exception is: 
    javax.mail.MessagingException: Could not connect to SMTP host: localhost, port: 25;
  nested exception is: Connection refused: connect
    at javax.mail.Transport.send0(
    at javax.mail.Transport.send(
    at org.apache.jsp.online_005fScheme_005fSend_005fMail_jsp.sendMail(
    at org.apache.jsp.online_005fScheme_005fSend_005fMail_jsp._jspService(
    at org.apache.jasper.runtime.HttpJspBase.service(
    at javax.servlet.http.HttpServlet.service(
    at org.apache.jasper.servlet.JspServletWrapper.service(
    at org.apache.jasper.servlet.JspServlet.serviceJspFile(
    at org.apache.jasper.servlet.JspServlet.service(
    at javax.servlet.http.HttpServlet.service(
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(
    at org.apache.catalina.core.StandardWrapperValve.invoke(
    at org.apache.catalina.core.StandardContextValve.invoke(
    at org.apache.catalina.core.StandardHostValve.invoke(
    at org.apache.catalina.valves.ErrorReportValve.invoke(
    at org.apache.catalina.core.StandardEngineValve.invoke(
    at org.apache.catalina.connector.CoyoteAdapter.service(
    at org.apache.coyote.http11.Http11Processor.process(
    at org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(
    at org.apache.tomcat.util.threads.ThreadPool$

I use below code snipet.

Properties props = new Properties();
     props.put("", "");

email send program is running on tomcat 5. Some times it is working fine and some times it results above exception. once it satart resulting above exception it results the same on every access. but as soon as i restart tomcat server it starts working fine again.

So i could not find what is reason. as sometimes same is working fine and sometimes results above exception.

Can anybody help me out this issue.

This question is tagged with java jakarta-mail tomcat5.5

~ Asked on 2013-10-21 11:58:30

The Best Answer is


package sn;
import java.util.Date;
import java.util.Properties;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class SendEmail {
  public static void main(String[] args) {
    final String SSL_FACTORY = "";
 // Get a Properties object
    Properties props = System.getProperties();
    props.setProperty("", "");
    props.setProperty("mail.smtp.socketFactory.class", SSL_FACTORY);
    props.setProperty("mail.smtp.socketFactory.fallback", "false");
    props.setProperty("mail.smtp.port", "465");
    props.setProperty("mail.smtp.socketFactory.port", "465");
    props.put("mail.smtp.auth", "true");
    props.put("mail.debug", "true");
    props.put("", "pop3");
    props.put("mail.transport.protocol", "smtp");
    final String username = "[email protected]";//
    final String password = "0000000";
      Session session = Session.getDefaultInstance(props, 
                          new Authenticator(){
                             protected PasswordAuthentication getPasswordAuthentication() {
                                return new PasswordAuthentication(username, password);

   // -- Create a new message --
      Message msg = new MimeMessage(session);

   // -- Set the FROM and TO fields --
      msg.setFrom(new InternetAddress("[email protected]"));
                        InternetAddress.parse("[email protected]",false));
      msg.setText("How are you");
      msg.setSentDate(new Date());
      System.out.println("Message sent.");
    }catch (MessagingException e){ 
      System.out.println("Erreur d'envoi, cause: " + e);

~ Answered on 2014-12-28 05:04:06


It is very clear from your exception that it is trying to connect to localhost and not to

exception snippet : Could not connect to SMTP host: localhost, port: 25;

1.) Please check if there are any null check which is setting localhost as default value

2.) After restarting, if it is working fine, then it means that only at first-run, the proper value is been taken from Properties and from next run the value is set to default. So keep the property-object as a singleton one and use it all-over your project

~ Answered on 2013-10-21 12:05:08

Most Viewed Questions: