0

I'm trying to access gmail smtp socket with smtp.gmail.com:587 or smtp.gmail.com:465 with the following the code. It gives me "java.net.ConnectException: Connection refused: connect" error. When I try to reach the ports via Windows telnet client. Windows telnet client is connected to the socket successfully. I'm confused about that. Do you have any idea about the reason?

I was trying to connect to the internet with two different ways. One of them is adsl modem and other is 4g modem. Both of them gives the same error

Thanks for your help in advance.

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;

public class EchoClient {
    public static void main(String[] args) throws IOException {

        Socket pingSocket = null;
        PrintWriter out = null;
        BufferedReader in = null;

        try {
            pingSocket = new Socket("smtp.gmail.com", 465);
            out = new PrintWriter(pingSocket.getOutputStream(), true);
            in = new BufferedReader(new InputStreamReader(pingSocket.getInputStream()));
        } catch (IOException e) {
            e.printStackTrace();
            return;
        }

        out.println("ping");
        System.out.println(in.readLine());
        out.close();
        in.close();
        pingSocket.close();
    }
}
java.net.ConnectException: Connection refused: connect
    at java.base/java.net.PlainSocketImpl.connect0(Native Method)
    at java.base/java.net.PlainSocketImpl.socketConnect(PlainSocketImpl.java:101)
    at java.base/java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:399)
    at java.base/java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:242)
    at java.base/java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:224)
    at java.base/java.net.SocksSocketImpl.connect(SocksSocketImpl.java:403)
    at java.base/java.net.Socket.connect(Socket.java:591)
    at java.base/java.net.Socket.connect(Socket.java:540)
    at java.base/java.net.Socket.<init>(Socket.java:436)
    at java.base/java.net.Socket.<init>(Socket.java:213)
    at EchoClient.main(EchoClient.java:15)

windows telnet client

  • Works for me. Any more info? – President James K. Polk Jul 04 '19 at 21:54
  • I try the code in eclipse and inteliJ in two different computer with two different network with and without vpn. In all case, result is same. Windows telnet client can reach the socket but the code can not. I'm really curious aboult the resoan of it. Do you have any idea about the problem? Or could you give me any suggestion to try? How I can trace the network packet for java and windows client? – Volkan Istek Jul 05 '19 at 09:36
  • More information: I use 4g from my mobile phone (hotspot) and I use adsl network from Asus adsl modem – Volkan Istek Jul 05 '19 at 09:41
  • @VolkanIstek "*How I can trace the network packet for java and windows client?*" - use a packet sniffer, like [Wireshark](https://www.wireshark.org/). – Remy Lebeau Jul 05 '19 at 18:44
  • Problem was solved :) I just install JDK 1.8.0.211 and the code works. Before installing JDK 1.8 I used JDK 12 which is still installed on my machine. When I check the java version from the console. It says that: **java version "1.8.0_211"** Java(TM) SE Runtime Environment (build 1.8.0_211-b12) Java HotSpot(TM) 64-Bit Server VM (build 25.211-b12, mixed mode) – Volkan Istek Jul 06 '19 at 06:39

0 Answers0