Java.Net.Connectexception: Connection Refused

java.net.ConnectException: Connection refused

This exception means that there is no service listening on the IP/port you are trying to connect to:

  • You are trying to connect to the wrong IP/Host or port.
  • You have not started your server.
  • Your server is not listening for connections.
  • On Windows servers, the listen backlog queue is full.

How to fix 'java.net.ConnectException: Connection refused: connect'

I was able to solve the problem with the following code.
My friend told me that java requires a valid username and password to connect to smtp server after its deployed on apache, so I added them but my problem was solved after I used sslsocket to connect not normal socket. (t.connect(new Socket(host, 25));) //old way
//the new code use (t.connect(new SSLSocket(SMTP_SERVER, 25)).

private static final String SMTP_SERVER = "smtp server";
private static final String USERNAME = "username";
private static final String PASSWORD = "password";

private static final String EMAIL_FROM = "email from";
private static final String EMAIL_TO = "email to";
private static final String EMAIL_TO_CC = "cc";

private static final String EMAIL_SUBJECT = "Subject";

public void sendReplicationCheckResult() {

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

// Setup mail server
properties.setProperty("mail.transport.protocol", "smtp");
properties.setProperty("mail.smtp.host", SMTP_SERVER);
properties.setProperty("mail.smtp.port", "25");
properties.put("mail.smtp.starttls.enable", "true");
properties.put("mail.smtp.auth", "false");

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

try {
// Create a default MimeMessage object.
MimeMessage message = new MimeMessage(session);

// Set From: header field of the header.
message.setFrom(new InternetAddress(EMAIL_FROM));

// Set To: header field of the header.
message.addRecipient(Message.RecipientType.TO, new InternetAddress(EMAIL_TO));

// Set Subject: header field
message.setSubject(EMAIL_SUBJECT);

// Create the message part
BodyPart messageBodyPart = new MimeBodyPart();

// Fill the message
// messageBodyPart.setText("This is message body");
// Create a multipart message
Multipart multipart = new MimeMultipart();

// Set text message part
//multipart.addBodyPart(messageBodyPart);
// Part two is attachment
messageBodyPart = new MimeBodyPart();
String filename = "File Path";
DataSource source = new FileDataSource(filename);
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName("File Name");
multipart.addBodyPart(messageBodyPart);

// Send the complete message parts
message.setContent(multipart);

// Send message
SMTPTransport t = (SMTPTransport) session.getTransport("smtp");

// connect
t.connect(new SSLSocket(SMTP_SERVER, 25) {
@Override
public String[] getSupportedCipherSuites() {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}

@Override
public String[] getEnabledCipherSuites() {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}

@Override
public void setEnabledCipherSuites(String[] strings) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}

@Override
public String[] getSupportedProtocols() {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}

@Override
public String[] getEnabledProtocols() {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}

@Override
public void setEnabledProtocols(String[] strings) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}

@Override
public SSLSession getSession() {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}

@Override
public void addHandshakeCompletedListener(HandshakeCompletedListener hl) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}

@Override
public void removeHandshakeCompletedListener(HandshakeCompletedListener hl) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}

@Override
public void startHandshake() throws IOException {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}

@Override
public void setUseClientMode(boolean bln) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}

@Override
public boolean getUseClientMode() {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}

@Override
public void setNeedClientAuth(boolean bln) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}

@Override
public boolean getNeedClientAuth() {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}

@Override
public void setWantClientAuth(boolean bln) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}

@Override
public boolean getWantClientAuth() {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}

@Override
public void setEnableSessionCreation(boolean bln) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}

@Override
public boolean getEnableSessionCreation() {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
});
// send
t.sendMessage(message, message.getAllRecipients());

System.out.println("Response: " + t.toString());

t.close();
FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(null, "Email was sent Successfully"));
} catch (IOException ex) {
Logger.getLogger(SendEmail.class.getName()).log(Level.SEVERE, null, ex);
FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(null, ex.getLocalizedMessage()));
} catch (MessagingException ex) {
FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(null, ex.getLocalizedMessage()));
Logger.getLogger(SendEmail.class.getName()).log(Level.SEVERE, null, ex);
}
}

java.net.ConnectException: Connection refused timeout

There is no such thing as a "connection refused timeout".

"Connection refused" happens when the server sees the connection request, but there is no service listening for connections on the IP + port that the request is directed to. The server then "refuses" the connection. This typically happens instantly, so so no timeout is triggered.

"Connection timed out" happens (typically) when something stops the connection request from reaching the server1, 2. So the client-side will wait for the response from the server, and then resend / wait a few times. And eventually the time allotted for establishing a connection will expire ... and the connection times out.

As you can see these are different scenarios. And they are reported back to the Java client-side differently.

So the reason you are not getting timeouts is that the "connection refused" responses are coming back quick enough that your configured timeout is not exceeded.

That might also explain why setting the connect timeout small might have changed the behavior. There may also be issues with the granularity of the timeout that the OS allows Java to set.

To investigate this further, I think we would need a minimal reproducible example. For example, we need to see how you have implemented the code that manages the server-socket and accepts connections on the server side.


1 - The blockage could be on the server's reply packets.

2 - There are various possible causes for this kind of thing. The most likely are a firewall blocking traffic somewhere, a network routing problem, or using a private IP address on the wrong network.

Connection refused [Exception: java.net.ConnectException]

Found the issue!
I realized I manually set proxy settings in the simulator.

Codename One Simulator Network Proxy Settings

Quarkus test rest client java.net.ConnectException: Connection refused: connect

Found the problem, you should add also this dependency:

    <dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-resteasy-jackson</artifactId>
<scope>test</scope>
</dependency>


Related Topics



Leave a reply



Submit