通过套接字接口与邮件服务器建立TCP连接并发送电子邮件的Java程序

时间:2012-10-04 22:52:34

标签: java sockets email tcp

我正在编写一个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();
}
}

1 个答案:

答案 0 :(得分:7)

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

来自RFC 2821,第2.4.7节:

  

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

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

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

(当然,你真的应该使用JavaMail等邮件库,除非这纯粹是出于教育目的。)