Below is the syntax highlighted version of Mail.java
from §8.4 Operating Systems.
/****************************************************************************** * Compilation: javac Mail.java * Execution: java Mail * * Sends email to [email protected] addressed from [email protected] * 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: [email protected] * RCPT TO: [email protected] * DATA * Message-ID: <[email protected]> * Date: Sun, 17 Aug 1997 18:48:15 +0200 * From: Nobody <[email protected]> * To: Kevin Wayne <[email protected]> * 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: [email protected]"); StdOut.println(in.readLine()); // send to this address out.println("RCPT To: [email protected]"); 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: <[email protected]>"); out.println("Date: Wed, 27 Aug 2003 11:48:15 -0400"); out.println("From: Nobody Here <[email protected]>"); out.println("Subject: Just wanted to say hi"); out.println("To: [email protected]"); 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(); } }