Reputation: 453
I'm writing a Java program that establishes a TCP connection with a mail server through the socket interface, and sends an email message. The problem I'm having is when I run it on a command line it stops after writing "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();
}
}
Upvotes: 2
Views: 6066
Reputation: 1499860
You're writing "MAIL FROM: <[email protected]>"
but without a new line at the end. Note the difference between that and the HELO
command you sent.
From RFC 2821, section 2.4.7:
SMTP commands and, unless altered by a service extension, message data, are transmitted in "lines". 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).
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.)
Upvotes: 8