[英]Java program that establishes a TCP connection with a mail server through socket interface and sends an email
我正在編寫一個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();
}
}
您正在編寫"MAIL FROM: <johnstoy@uwindsor.ca>"
但最后沒有換行。 請注意這與您發送的HELO
命令之間的區別。
根據RFC 2821第2.4.7節:
SMTP命令以及消息數據(除非通過服務擴展進行了更改)均以“行”形式傳輸。 行由零個或多個數據字符組成,這些數據字符以ASCII字符“ CR”(十六進制值0D)結束,緊接着是ASCII字符“ LF”(十六進制值0A)。
您沒有終止命令,因此服務器仍在等待更多數據。
其余的命令也有同樣的問題,順便說一句。
(當然,除非純粹出於教育目的,否則您確實應該使用諸如JavaMail之類的郵件庫。)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.