0

以下のプログラムはこの問題を引き起こします

編集:

import java.io.*;
import java.net.*;
public class smtpClient {
    public static void main(String[] args) {
// declaration section:
// smtpClient: our client socket
// os: output stream
// is: input stream
        Socket smtpSocket = null;  
        DataOutputStream os = null;
        DataInputStream is = null;
// Initialization section:
// Try to open a socket on port 25 : step 1
// Try to open input and output streams: step 2
        try {
            smtpSocket = new Socket("192.168.1.2", 1024);
            os = new DataOutputStream(smtpSocket.getOutputStream());
            is = new DataInputStream(smtpSocket.getInputStream());
        } catch (UnknownHostException e) {
            System.err.println("Don't know about host: hostname");
        } catch (IOException e) {
            System.err.println("Couldn't get I/O for the connection to: hostname");
        }
// If everything has been initialized then we want to write some data
// to the socket we have opened a connection to on port 25
    if (smtpSocket != null && os != null && is != null) {
            try {
// The capital string before each colon has a special meaning to SMTP
// you may want to read the SMTP specification, RFC1822/3
        os.writeBytes("HELO\n");    
                os.writeBytes("MAIL From: k3is@fundy.csd.unbsj.ca\n");
                os.writeBytes("RCPT To: k3is@fundy.csd.unbsj.ca\n");
                os.writeBytes("DATA\n");
                os.writeBytes("From: k3is@fundy.csd.unbsj.ca\n");
                os.writeBytes("Subject: testing\n");
                os.writeBytes("Hi there\n"); // message body
                os.writeBytes("\n.\n");
        os.writeBytes("QUIT");
// keep on reading from/to the socket till we receive the "Ok" from SMTP,
// once we received that then we want to break.
                String responseLine;
                while ((responseLine = is.readLine()) != null) {
                    System.out.println("Server: " + responseLine);
                    if (responseLine.indexOf("Ok") != -1) {
                      break;
                    }
                }
// clean up:
// close the output stream
// close the input stream
// close the socket
                os.close();
                is.close();
                smtpSocket.close();   
            } catch (UnknownHostException e) {
                System.err.println("Trying to connect to unknown host: " + e);
            } catch (IOException e) {
                System.err.println("IOException:  " + e);
            }
        }
    }           
}

コンソールログ:

Couldn't get I/O for the connection to: hostname

私が取ったプログラムは次のものです: http://www.javaworld.com/jw-12-1996/jw-12-sockets.html?page=4

ポートを 25 から 1024
に変更しようとしましたが、ローカル PC で実行しているため、このシステムの管理者ですが、デフォルトのファイアウォールの問題があるかどうかはわかりません (これを Windows 7 の Eclipse で実行しています)。

以下のあなたのコメントによると: DO私はsmtpクライアントの要求をリッスンするServer Socketを意味するlistnerを作成する必要があります

4

2 に答える 2

1

回答は次のとおりです。提供された詳細によると、指定された IP とポート番号を持つリスナーが実行されていないか、マシンがありません。

UPD: 次に、どこかに接続しようとしています。独自のサーバー コードを記述するか、サード パーティのサーバー/コードを使用してポート番号で特定のサービスを提供することにより、反対側でリッスンするものがあることを確認する必要があります。到達しようとしています。

あなたが提供したアドレスを持つマシン上で実行されているメールサーバーがあると期待するのはなぜですか?

于 2013-08-09T13:46:27.767 に答える
0

他のプログラムがポート 1024 を使用しているようです。

別のポートを試してください。

于 2013-08-09T13:43:12.727 に答える