import java.net.*; import java.io.*; // A pure 1.1 variation of a class that appears in // Core Web Programming from Prentice Hall Publishers. // May be freely used or adapted. // 1998 Marty Hall, http://www.apl.jhu.edu/~hall/java/. //---------------------------------------------------- /** Given an email address of the form user@host, * connect to port 25 of the host and issue an * 'expn' request for the user. Print the results. */ public class AddressVerifier extends NetworkClient { private String username; //---------------------------------------------------- public static void main(String[] args) { if (args.length != 1) usage(); MailAddress address = new MailAddress(args[0]); AddressVerifier verifier = new AddressVerifier(address.getUsername(), address.getHostname(), 25); verifier.connect(); } //---------------------------------------------------- public AddressVerifier(String username, String hostname, int port) { super(hostname, port); this.username = username; } //---------------------------------------------------- /** NetworkClient, the parent class, automatically * establishes the connection and then passes * the Socket to handleConnection. So this method * does all the real work of talking to the mail * server. */ // You can't use readLine, since it blocks. // Blocking IO via readLine is only appropriate // when you know how many lines to read (but mail // servers send a varying number of lines when // you first connect) or when they close the // connection when done (as HTTP servers do, // yielding null for readLine. Also, we'll // be lazy and assume that 1000 bytes is // more than enough to handle any server // welcome message and the actual EXPN response. protected void handleConnection(Socket client) { try { PrintWriter out = SocketUtil.getPrintWriter(client); InputStream in = client.getInputStream(); byte[] response = new byte[1000]; // Clear out mail server's welcome message. in.read(response); out.println("EXPN " + username); // Read the response to the EXPN command. int numBytes = in.read(response); // The 0 means to use normal ASCII encoding. System.out.write(response, 0, numBytes); out.println("QUIT"); client.close(); } catch(IOException ioe) { System.out.println("Couldn't make connection: " + ioe); } } //---------------------------------------------------- /** Warn user if they supplied the wrong arguments. */ public static void usage() { System.out.println ("You must supply an email address " + "of the form 'username@hostname'."); System.exit(-1); } }