简体   繁体   English

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

[英]Java program that establishes a TCP connection with a mail server through socket interface and sends an email

I'm writing a Java program that establishes a TCP connection with a mail server through the socket interface, and sends an email message. 我正在编写一个Java程序,该程序通过套接字接口与邮件服务器建立TCP连接,并发送电子邮件。 The problem I'm having is when I run it on a command line it stops after writing "MAIL FROM: ". 我遇到的问题是,当我在命令行上运行它时,它会在编写“ MAIL FROM:”后停止。 I'm not getting any errors it just kind of stops at that point. 我没有收到任何错误,只是到那时为止。 I can't figure out what I'm doing wrong so any help would be greatly appreciated 我无法弄清楚自己在做什么错,因此我们将不胜感激任何帮助

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();
}
}

You're writing "MAIL FROM: <johnstoy@uwindsor.ca>" but without a new line at the end. 您正在编写"MAIL FROM: <johnstoy@uwindsor.ca>"但最后没有换行。 Note the difference between that and the HELO command you sent. 请注意这与您发送的HELO命令之间的区别。

From RFC 2821 , section 2.4.7: 根据RFC 2821第2.4.7节:

SMTP commands and, unless altered by a service extension, message data, are transmitted in "lines". SMTP命令以及消息数据(除非通过服务扩展进行了更改)均以“行”形式传输。 Lines consist of zero or more data characters terminated by the sequence ASCII character "CR" (hex value 0D) followed immediately by ASCII character "LF" (hex value 0A). 行由零个或多个数据字符组成,这些数据字符以ASCII字符“ CR”(十六进制值0D)结束,紧接着是ASCII字符“ LF”(十六进制值0A)。

You're not terminating your command, so the server is still waiting for more data. 您没有终止命令,因此服务器仍在等待更多数据。

The rest of your commands have the same problem, btw. 其余的命令也有同样的问题,顺便说一句。

(Of course, you really should be using a mail library such as JavaMail unless this is purely for educational purposes.) (当然,除非纯粹出于教育目的,否则您确实应该使用诸如JavaMail之类的邮件库。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM