Below is the syntax highlighted version of Mail.java
from §8.4 Operating Systems.
/****************************************************************************** * Compilation: javac Mail.java * Execution: java Mail * * Sends email to wayne@cs.princeton.edu addressed from nobody@cnn.com * according to the Simple Mail Transfer Protocol (SMTP). * * DO NOT USE this program unless you have permission from the * person receiving the email. * * % java Mail * * Replace "smtp.princeton.edu" with your local mail server in the code below. * * % telnet smtp.princeton.edu 25 * HELO localhost * MAIL FROM: nobody@cnn.com * RCPT TO: support@about.com * DATA * Message-ID: <19970817184815.33912@princeton.edu> * Date: Sun, 17 Aug 1997 18:48:15 +0200 * From: Nobody <nobody@cnn.com> * To: Kevin Wayne <wayne@cs.princeton.edu> * X-Mailer: Spam Mailer X * Hello, how are you? * This is the body of the message. * . * quit * ******************************************************************************/ import java.io.*; import java.net.*; public class Mail { public static void main(String[] args) throws Exception { Socket socket = new Socket("smtp.princeton.edu", 25); BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream())); PrintWriter out = new PrintWriter(socket.getOutputStream(), true); // the return path out.println("HELO localhost"); StdOut.println(in.readLine()); out.println("MAIL From: nobody@cnn.com"); StdOut.println(in.readLine()); // send to this address out.println("RCPT To: wayne@cs.princeton.edu"); StdOut.println(in.readLine()); out.println("DATA"); StdOut.println(in.readLine()); // write message, and end with a . on a line by itself // out.println("Message-ID: <19970817184815.33912@princeton.edu>"); out.println("Date: Wed, 27 Aug 2003 11:48:15 -0400"); out.println("From: Nobody Here <nobody@cnn.com>"); out.println("Subject: Just wanted to say hi"); out.println("To: wayne@cs.princeton.edu"); out.println("X-Mailer: Spam Mailer X"); out.println("This is the message body."); out.println("Here is a second line."); out.println("."); StdOut.println(in.readLine()); // clean up out.close(); in.close(); socket.close(); } }