/* * The Mail.java program intends to communicate with SMTP port. * Here is how it works. * Precondition: The SMTP always listens on port 25. * Step 1: connect to port 25 using TCP * Step 2: receive a line from mail server and print it out * The line would read similar to the following * * 220 mesquite. Sendmail SMI-8.6/SMI-SVR4 ready at Mon, 23 Jun 1997 * 11:00:14 -0500 * * Step 3: send in the following lines: * HELO (sending host) * or EHLO (sending host) when an extended SMTP server is running * MAIL FROM: (sender) * RCPT TO: (recipient) * DATA * (mail message) * (any number of lines) * . * QUIT */ import java.io.*; import java.net.*; class MailClient { public static void main(String[] args) throws IOException { if (args.length != 1) { System.out.println(" usage java MailClient host"); System.exit(1); } Socket t = new Socket(args[0], 25); String str; DataInputStream is = new DataInputStream(t.getInputStream()); PrintStream out = new PrintStream(t.getOutputStream()); DataInputStream in = new DataInputStream(System.in); boolean more = true; str = is.readLine(); System.out.println(str); while (more) { str = in.readLine(); out.println(str); if (str.trim().equals("DATA")) more = false; str = is.readLine(); System.out.println(str); } // while // begin to send message body String localStr = ""; str = ""; do { localStr = in.readLine(); str += localStr + "\n"; } while (!localStr.trim().equals(".")); out.println(str); localStr = localStr.trim(); localStr += "\r\n"; out.println(localStr); str = is.readLine(); System.out.println(str); // end of message body // now send a null string to end the process str = in.readLine(); str += "\r\n"; out.println(str); } // main }