无法使用Java连接到gmail smtp套接字,但telnet可以访问该套接字

时间:2019-07-04 21:19:39

标签: java sockets smtp gmail telnet

我正在尝试使用以下代码使用smtp.gmail.com:587或smtp.gmail.com:465访问gmail smtp套接字。它给我“ java.net.ConnectException:连接被拒绝:连接”错误。当我尝试通过Windows telnet客户端访问端口时。 Windows telnet客户端已成功连接到套接字。我对此感到困惑。您对原因有任何想法吗?

我试图用两种不同的方式连接到互联网。其中之一是adsl调制解调器,另一个是4g调制解调器。他们两个都给出相同的错误

谢谢您的帮助。

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

0 个答案:

没有答案