2

我正在编写一个 Java 程序,它通过套接字接口与邮件服务器建立 TCP 连接,并发送电子邮件消息。我遇到的问题是,当我在命令行上运行它时,它会在写入“MAIL FROM:”后停止。我没有收到任何错误,它只是在那一点停止。我不知道我做错了什么,所以任何帮助将不胜感激

import java.io.*;
import java.net.*;

public class EmailSender{

public static void main(String[] args) throws Exception{

    // Establish a TCP connection with the mail server.
     Socket socket = new Socket("" + "hostname", 25);
    // Create a BufferedReader to read a line at a time.
    InputStream is = socket.getInputStream();
    InputStreamReader isr = new InputStreamReader(is);
    BufferedReader br = new BufferedReader(isr);

    // Read greeting from the server.
    String response = br.readLine();
    System.out.println(response);
    if (!response.startsWith("220")) {
        socket.close();
        throw new Exception("220 reply not received from server.");
    }

    // Get a reference to the socket's output stream.
    OutputStream os = socket.getOutputStream();

    // Send HELO command and get server response.
    String command = "HELO alice\r\n";
    System.out.print(command);
    os.write(command.getBytes("US-ASCII"));
    response = br.readLine();
    System.out.println(response);
    if (!response.startsWith("250")) {
        socket.close();
        throw new Exception("250 reply not received from server.");
    }

    // Send MAIL FROM command.
    String mailFrom = "MAIL FROM: <email>";
    System.out.print(mailFrom);
    os.write(mailFrom.getBytes("US-ASCII"));
    response = br.readLine();
    System.out.println(response);
    if (!response.startsWith("250")) {
        socket.close();
        throw new Exception("250 reply not received from server.");
    }

    // Send RCPT TO command.
    String commandRCPT = "RCPT TO: <email>";
    System.out.print(commandRCPT);
    os.write(commandRCPT.getBytes("US-ASCII"));
    response = br.readLine();
    System.out.println(response);
    if (!response.startsWith("250")) {
        socket.close();
        throw new Exception("250 reply not received from server.");
    }

    // Send DATA command.
    String commandDATA = "DATA";
    System.out.print(commandDATA);
    os.write(commandDATA.getBytes("US-ASCII"));
    response = br.readLine();
    System.out.println(response);
    if (!response.startsWith("354")) {
        socket.close();
        throw new Exception("354 reply not received from server.");
    }

    // Send message data.
    String msgLine1 = "email sent";
    System.out.print(msgLine1);
    os.write(msgLine1.getBytes("US-ASCII"));

    // End with line with a single period.
    String msgLine2 = ".";
    System.out.print(msgLine2);
    os.write(msgLine2.getBytes("US-ASCII"));
    response = br.readLine();
    System.out.println(response);
    if (!response.startsWith("250")) {
        socket.close();
        throw new Exception("250 reply not received from server.");
    }

    // Send QUIT command.
    String commandQUIT = "QUIT";
    System.out.print(commandQUIT);
    os.write(commandQUIT.getBytes("US-ASCII"));
    response = br.readLine();
    System.out.println(response);
    if (!response.startsWith("221")) {
        socket.close();
        throw new Exception("221 reply not received from server.");
    }

    socket.close();
}
}
4

1 回答 1

8

你正在写"MAIL FROM: <johnstoy@uwindsor.ca>",但最后没有换行。HELO请注意这与您发送的命令之间的区别。

来自RFC 2821,第 2.4.7 节:

SMTP 命令和消息数据(除非被服务扩展更改)以“行”的形式传输。行由零个或多个数据字符组成,以序列 ASCII 字符“CR”(十六进制值 0D)结尾,紧接着是 ASCII 字符“LF”(十六进制值 0A)。

您没有终止命令,因此服务器仍在等待更多数据。

顺便说一句,你的其余命令有同样的问题。

(当然,您确实应该使用诸如 JavaMail 之类的邮件库,除非这纯粹是出于教育目的。)

于 2012-10-04T22:57:00.437 回答