chevron_left

Problem :

I am facing the below error every time :

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:1706)
    at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:525)
    at javax.mail.Service.connect(Service.java:291)
    at javax.mail.Service.connect(Service.java:172)
    at javax.mail.Service.connect(Service.java:121)
    at javax.mail.Transport.send0(Transport.java:190)
    at javax.mail.Transport.send(Transport.java:120)
    at MailSendClass.main(MailSendClass.java:58)
Caused by: java.net.ConnectException: Connection refused: connect

I am unable to understand the error why this is happening to my code. Please let me know how to fix the above error.

Solution :

The Error you are getting is self explanatory: javax.mail.MessagingException: Could not connect to SMTP host: localhost, port: 25;

It seems you have no SMTP server on localhost, but you are configuring it there :

 // Assuming that you are sending the email from localhost

  String host = "localhost";

  ...

  // Setup the mail server

  properties.setProperty("mail.smtp.host", host);

So you must do the below things:

  • Either configure the local SMTP server as the relay on the local
    system (the Postfix or sendmail are the two very well knows servers)
  • Or Configure the dummy server that simply traces your mail request
    but does not even try to deliver the mail (The Python is known to
    have such dummy servers out of a box)
  • · Or configure the application with the server that you are allowed
    to use so contact your system admin in your corporate environment, or
    the ISP in the individual one.

Another Soluaton:

When TDI 7.1.1 or SDI 7.2 uses the IBM JRE 7, then the following
exception will appear. When either system.sendMail() or SendEMailFC
are used to send emails.

Symptoms:
One assembly line execution, the system.SendMail() produce the following error;

DEBUG SMTP: trying to connect to host "na.relay.ibm.com", port 25, isSSL false
CTGDIS399I Unable to send mail.Exception occurred: javax.mail.MessagingException: Could not connect to SMTP host: na.relay.ibm.com, port: 25;
nested exception is:
java.net.SocketException: Network is unreachable: connect

On AssemblyLine execution, the SendMailFC produces the following error;

javax.mail.MessagingException: Could not connect to SMTP host: 10.10.10.1, port: 25;
nested exception is:
java.net.SocketException: Network is unreachable: connect
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1282)
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:370)
at javax.mail.Service.connect(Service.java:275)
at javax.mail.Service.connect(Service.java:156)
at javax.mail.Service.connect(Service.java:105)
at javax.mail.Transport.send0(Transport.java:168)
at javax.mail.Transport.send(Transport.java:98)
at com.ibm.di.fc.SendEMailFC.perform(SendEMailFC.java:519)
at com.ibm.di.server.AssemblyLineComponent.executeOperation(AssemblyLineComponent.java:3370)
at com.ibm.di.server.FunctionComponent.callreply(FunctionComponent.java:304)
at com.ibm.di.server.AssemblyLine.msExecuteNextConnector(AssemblyLine.java:3813)
at com.ibm.di.server.AssemblyLine.executeMainStep(AssemblyLine.java:3404)
at com.ibm.di.server.AssemblyLine.executeMainLoop(AssemblyLine.java:3012)
at com.ibm.di.server.AssemblyLine.executeMainLoop(AssemblyLine.java:2995)
at com.ibm.di.server.AssemblyLine.executeAL(AssemblyLine.java:2964)
at com.ibm.di.server.AssemblyLine.run(AssemblyLine.java:1328)
Caused by: java.net.SocketException: Network is unreachable: connect

Causes:
It is possible that the IBM JVM is defaulting to the IPv6 protocol.

Resolving the error:
Implement the available options;

In solution.properties file, find, un-comment and set ‘java.net.preferIPv4Stack=true’

Or

Call system.setProperty (“java.net.preferIPv$Stack”, “true”) before calling the system.sendMail()

Or

In the SendEMailFC / After Initialize Hook, add system.setProperty(“java.net.preferIP4Stack”, “true”).

More Posts

javax.mail.messagingexception could not connect to SMTP host usmanhashmi - Aug 3
Trying to connect to host "smtp.gmail.com", port 465, isssl false. Alecxe01 - Nov 9
How to make exe file in java using netbeans with database amna - Jul 11
How to make a calculator in java netbeans amna - Jul 3
Curl: (7) failed to connect to localhost port 9200: connection refused Alecxe01 - Nov 22
Host is not allowed to connect to this mysql server amna - Jun 30
The message could not be sent. mailer error: Could not instantiate mail function. Farkhanda Athar - Apr 16, 2020
Could not extract response: no suitable httpmessageconverter found for response type Alecxe01 - Nov 14
Error :Cannot connect to the Docker daemon. Is the docker daemon running on this host? Sabbir Hossain - Mar 31
SMTP error: failed to connect to server: Network is unreachable (101). Farkhanda Athar - Apr 21, 2020