import java.net.*; import java.io.*; /** Given an e-mail address of the form user@host, * connect to port 25 of the host and issue an * 'expn' request for the user. Print the results. * * Taken from Core Web Programming from * Prentice Hall and Sun Microsystems Press, * . * © 2001 Marty Hall and Larry Brown; * may be freely used or adapted. */ 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. This method does all the real work * of talking to the mail server. */ // You can't use readLine, because it blocks. Blocking I/O // by readLine is only appropriate when you know how many // lines to read. Note that mail servers send a varying // number of lines when you first connect or send no line // closing the connection (as HTTP servers do), yielding // null for readLine. Also, we'll 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.getWriter(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); } } /** If the wrong arguments, thn warn user. */ public static void usage() { System.out.println ("You must supply an email address " + "of the form 'username@hostname'."); System.exit(-1); } } MailAddress.java Separates the user and host components of an email address. import java.util.*; /** Takes a string of the form "user@host" and * separates it into the "user" and "host" parts. * * Taken from Core Web Programming from * Prentice Hall and Sun Microsystems Press, * . * © 2001 Marty Hall and Larry Brown; * may be freely used or adapted. */ public class MailAddress { private String username, hostname; public MailAddress(String emailAddress) { StringTokenizer tokenizer = new StringTokenizer(emailAddress, "@"); this.username = getArg(tokenizer); this.hostname = getArg(tokenizer); } private static String getArg(StringTokenizer tok) { try { return(tok.nextToken()); } catch (NoSuchElementException nsee) { System.out.println("Illegal email address"); System.exit(-1); return(null); } } public String getUsername() { return(username); } public String getHostname() { return(hostname); } } NetworkClient.java Starting point for a network client to communicate with a remote computer. import java.net.*; import java.io.*; /** A starting point for network clients. You'll need to * override handleConnection, but in many cases connect can * remain unchanged. It uses SocketUtil to simplify the * creation of the PrintWriter and BufferedReader. * * Taken from Core Web Programming from * Prentice Hall and Sun Microsystems Press, * . * © 2001 Marty Hall and Larry Brown; * may be freely used or adapted. */ public class NetworkClient { protected String host; protected int port; /** Register host and port. The connection won't * actually be established until you call * connect. */ public NetworkClient(String host, int port) { this.host = host; this.port = port; } /** Establishes the connection, then passes the socket * to handleConnection. */ public void connect() { try { Socket client = new Socket(host, port); handleConnection(client); } catch(UnknownHostException uhe) { System.out.println("Unknown host: " + host); uhe.printStackTrace(); } catch(IOException ioe) { System.out.println("IOException: " + ioe); ioe.printStackTrace(); } } /** This is the method you will override when * making a network client for your task. * The default version sends a single line * ("Generic Network Client") to the server, * reads one line of response, prints it, then exits. */ protected void handleConnection(Socket client) throws IOException { PrintWriter out = SocketUtil.getWriter(client); BufferedReader in = SocketUtil.getReader(client); out.println("Generic Network Client"); System.out.println ("Generic Network Client:\n" + "Made connection to " + host + " and got '" + in.readLine() + "' in response"); client.close(); } /** The hostname of the server we're contacting. */ public String getHost() { return(host); } /** The port connection will be made on. */ public int getPort() { return(port); } } SocketUtil.java Provides utilities for wrapping a BufferedReader and PrintWriter around the Socket's input and output streams, respectively. import java.net.*; import java.io.*; /** A shorthand way to create BufferedReaders and * PrintWriters associated with a Socket. * * Taken from Core Web Programming from * Prentice Hall and Sun Microsystems Press, * . * © 2001 Marty Hall and Larry Brown; * may be freely used or adapted. */ public class SocketUtil { /** Make a BufferedReader to get incoming data. */ public static BufferedReader getReader(Socket s) throws IOException { return(new BufferedReader( new InputStreamReader(s.getInputStream()))); } /** Make a PrintWriter to send outgoing data. * This PrintWriter will automatically flush stream * when println is called. */ public static PrintWriter getWriter(Socket s) throws IOException { // Second argument of true means autoflush. return(new PrintWriter(s.getOutputStream(), true)); } }
AddressVerifier.java Connects to an SMTP server and issues a expn request to display details about a mailbox on the server. Uses the following classes
Permanent link to this article: http://bangla.sitestree.com/addressverifier-java-connects-to-an-smtp-server-and-issues-a-expn-request-to-display-details-about-a-mailbox-on-the-server-uses-the-following-classes/