{"id":10189,"date":"2015-08-24T00:00:38","date_gmt":"2015-08-24T04:00:38","guid":{"rendered":"http:\/\/bangla.salearningschool.com\/?p=10189"},"modified":"2015-08-01T08:31:59","modified_gmt":"2015-08-01T12:31:59","slug":"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","status":"publish","type":"post","link":"http:\/\/bangla.sitestree.com\/?p=10189","title":{"rendered":"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"},"content":{"rendered":"<pre>import java.net.*;\r\nimport java.io.*;\r\n\r\n\/** Given an e-mail address of the form user@host,\r\n\u00a0*\u00a0 connect to port 25 of the host and issue an\r\n\u00a0*\u00a0 'expn' request for the user. Print the results.\r\n\u00a0*\r\n\u00a0*\u00a0 Taken from Core Web Programming from\r\n\u00a0*\u00a0 Prentice Hall and Sun Microsystems Press,\r\n\u00a0*\u00a0 .\r\n\u00a0*\u00a0 \u00a9 2001 Marty Hall and Larry Brown;\r\n\u00a0*\u00a0 may be freely used or adapted.\r\n\u00a0*\/\r\n\r\npublic class AddressVerifier extends NetworkClient {\r\n\u00a0 private String username;\r\n\r\n\u00a0 public static void main(String[] args) {\r\n\u00a0\u00a0\u00a0 if (args.length != 1) {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 usage();\r\n\u00a0\u00a0 }\r\n\u00a0\u00a0\u00a0 MailAddress address = new MailAddress(args[0]);\r\n\u00a0\u00a0\u00a0 AddressVerifier verifier\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 = new AddressVerifier(address.getUsername(),\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 address.getHostname(), 25);\r\n\u00a0\u00a0\u00a0 verifier.connect();\r\n\u00a0 }\r\n\r\n\u00a0 public AddressVerifier(String username, String hostname,\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 int port) {\r\n\u00a0\u00a0\u00a0 super(hostname, port);\r\n\u00a0\u00a0\u00a0 this.username = username;\r\n\u00a0 }\r\n\r\n\u00a0 \/** NetworkClient, the parent class, automatically establishes\r\n\u00a0\u00a0 *\u00a0 the connection and then passes the Socket to\r\n\u00a0\u00a0 *\u00a0 handleConnection. This method does all the real work\r\n\u00a0\u00a0 *\u00a0 of talking to the mail server.\r\n\u00a0\u00a0 *\/\r\n\r\n\u00a0 \/\/ You can't use readLine, because it blocks. Blocking I\/O\r\n\u00a0 \/\/ by readLine is only appropriate when you know how many\r\n\u00a0 \/\/ lines to read. Note that mail servers send a varying\r\n\u00a0 \/\/ number of lines when you first connect or send no line\r\n\u00a0 \/\/ closing the connection (as HTTP servers do), yielding\r\n\u00a0 \/\/ null for readLine. Also, we'll assume that 1000 bytes\r\n\u00a0 \/\/ is more than enough to handle any server welcome\r\n\u00a0 \/\/ message and the actual EXPN response.\r\n\r\n\u00a0 protected void handleConnection(Socket client) {\r\n\u00a0\u00a0\u00a0 try {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 PrintWriter out = SocketUtil.getWriter(client);\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 InputStream in = client.getInputStream();\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 byte[] response = new byte[1000];\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ Clear out mail server's welcome message.\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 in.read(response);\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 out.println(\"EXPN \" + username);\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ Read the response to the EXPN command.\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 int numBytes = in.read(response);\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ The 0 means to use normal ASCII encoding.\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 System.out.write(response, 0, numBytes);\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 out.println(\"QUIT\");\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 client.close();\r\n\u00a0\u00a0\u00a0 } catch(IOException ioe) {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 System.out.println(\"Couldn't make connection: \" + ioe);\r\n\u00a0\u00a0\u00a0 }\r\n\u00a0 }\r\n\r\n\u00a0 \/** If the wrong arguments, thn warn user. *\/\r\n\r\n\u00a0 public static void usage() {\r\n\u00a0\u00a0\u00a0 System.out.println (\"You must supply an email address \" +\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \"of the form 'username@hostname'.\");\r\n\u00a0\u00a0\u00a0 System.exit(-1);\r\n\u00a0 }\r\n}\r\n\r\n\r\nMailAddress.java\u00a0 Separates the user and host components of an email address. \r\n\r\nimport java.util.*;\r\n\r\n\/** Takes a string of the form \"user@host\" and\r\n\u00a0*\u00a0 separates it into the \"user\" and \"host\" parts.\r\n\u00a0*\r\n\u00a0*\u00a0 Taken from Core Web Programming from\r\n\u00a0*\u00a0 Prentice Hall and Sun Microsystems Press,\r\n\u00a0*\u00a0 .\r\n\u00a0*\u00a0 \u00a9 2001 Marty Hall and Larry Brown;\r\n\u00a0*\u00a0 may be freely used or adapted.\r\n\u00a0*\/\r\n\r\npublic class MailAddress {\r\n\u00a0 private String username, hostname;\r\n\r\n\u00a0 public MailAddress(String emailAddress) {\r\n\u00a0\u00a0\u00a0 StringTokenizer tokenizer\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 = new StringTokenizer(emailAddress, \"@\");\r\n\u00a0\u00a0\u00a0 this.username = getArg(tokenizer);\r\n\u00a0\u00a0\u00a0 this.hostname = getArg(tokenizer);\r\n\u00a0 }\r\n\r\n\u00a0 private static String getArg(StringTokenizer tok) {\r\n\u00a0\u00a0\u00a0 try { return(tok.nextToken()); }\r\n\u00a0\u00a0\u00a0 catch (NoSuchElementException nsee) {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 System.out.println(\"Illegal email address\");\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 System.exit(-1);\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 return(null);\r\n\u00a0\u00a0\u00a0 }\r\n\u00a0 }\r\n\r\n\u00a0 public String getUsername() {\r\n\u00a0\u00a0\u00a0 return(username);\r\n\u00a0 }\r\n\r\n\u00a0 public String getHostname() {\r\n\u00a0\u00a0\u00a0 return(hostname);\r\n\u00a0 }\r\n}\r\n\r\n\r\nNetworkClient.java\u00a0 Starting point for a network client to communicate with a remote computer.\r\n\r\nimport java.net.*;\r\nimport java.io.*;\r\n\r\n\/** A starting point for network clients. You'll need to\r\n\u00a0*\u00a0 override handleConnection, but in many cases connect can\r\n\u00a0*\u00a0 remain unchanged. It uses SocketUtil to simplify the\r\n\u00a0*\u00a0 creation of the PrintWriter and BufferedReader.\r\n\u00a0*\r\n\u00a0*\u00a0 Taken from Core Web Programming from\r\n\u00a0*\u00a0 Prentice Hall and Sun Microsystems Press,\r\n\u00a0*\u00a0 .\r\n\u00a0*\u00a0 \u00a9 2001 Marty Hall and Larry Brown;\r\n\u00a0*\u00a0 may be freely used or adapted.\r\n\u00a0*\/\r\n\r\npublic class NetworkClient {\r\n\u00a0 protected String host;\r\n\u00a0 protected int port;\r\n\r\n\u00a0 \/** Register host and port. The connection won't\r\n\u00a0\u00a0 *\u00a0 actually be established until you call\r\n\u00a0\u00a0 *\u00a0 connect.\r\n\u00a0\u00a0 *\/\r\n\r\n\u00a0 public NetworkClient(String host, int port) {\r\n\u00a0\u00a0\u00a0 this.host = host;\r\n\u00a0\u00a0\u00a0 this.port = port;\r\n\u00a0 }\r\n\r\n\u00a0 \/** Establishes the connection, then passes the socket\r\n\u00a0\u00a0 *\u00a0 to handleConnection.\r\n\u00a0\u00a0 *\/\r\n\r\n\u00a0 public void connect() {\r\n\u00a0\u00a0\u00a0 try {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 Socket client = new Socket(host, port);\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 handleConnection(client);\r\n\u00a0\u00a0\u00a0 } catch(UnknownHostException uhe) {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 System.out.println(\"Unknown host: \" + host);\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 uhe.printStackTrace();\r\n\u00a0\u00a0\u00a0 } catch(IOException ioe) {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 System.out.println(\"IOException: \" + ioe);\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 ioe.printStackTrace();\r\n\u00a0\u00a0\u00a0 }\r\n\u00a0 }\r\n\r\n\u00a0 \/** This is the method you will override when\r\n\u00a0\u00a0 *\u00a0 making a network client for your task.\r\n\u00a0\u00a0 *\u00a0 The default version sends a single line\r\n\u00a0\u00a0 *\u00a0 (\"Generic Network Client\") to the server,\r\n\u00a0\u00a0 *\u00a0 reads one line of response, prints it, then exits.\r\n\u00a0\u00a0 *\/\r\n\r\n\u00a0 protected void handleConnection(Socket client)\r\n\u00a0\u00a0\u00a0 throws IOException {\r\n\u00a0\u00a0\u00a0 PrintWriter out = SocketUtil.getWriter(client);\r\n\u00a0\u00a0\u00a0 BufferedReader in = SocketUtil.getReader(client);\r\n\u00a0\u00a0\u00a0 out.println(\"Generic Network Client\");\r\n\u00a0\u00a0\u00a0 System.out.println\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 (\"Generic Network Client:\\n\" +\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \"Made connection to \" + host +\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \" and got '\" + in.readLine() + \"' in response\");\r\n\u00a0\u00a0\u00a0 client.close();\r\n\u00a0 }\r\n\r\n\u00a0 \/** The hostname of the server we're contacting. *\/\r\n\r\n\u00a0 public String getHost() {\r\n\u00a0\u00a0\u00a0 return(host);\r\n\u00a0 }\r\n\r\n\u00a0 \/** The port connection will be made on. *\/\r\n\r\n\u00a0 public int getPort() {\r\n\u00a0\u00a0\u00a0 return(port);\r\n\u00a0 }\r\n}\r\n\r\n\r\nSocketUtil.java\u00a0 Provides utilities for wrapping a BufferedReader\u00a0 and PrintWriter around the Socket's input and output streams, respectively. \r\n\r\nimport java.net.*;\r\nimport java.io.*;\r\n\r\n\/** A shorthand way to create BufferedReaders and\r\n\u00a0*\u00a0 PrintWriters associated with a Socket.\r\n\u00a0*\r\n\u00a0*\u00a0 Taken from Core Web Programming from\r\n\u00a0*\u00a0 Prentice Hall and Sun Microsystems Press,\r\n\u00a0*\u00a0 .\r\n\u00a0*\u00a0 \u00a9 2001 Marty Hall and Larry Brown;\r\n\u00a0*\u00a0 may be freely used or adapted.\r\n\u00a0*\/\r\n\r\npublic class SocketUtil {\r\n\u00a0 \/** Make a BufferedReader to get incoming data. *\/\r\n\r\n\u00a0 public static BufferedReader getReader(Socket s)\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 throws IOException {\r\n\u00a0\u00a0\u00a0 return(new BufferedReader(\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 new InputStreamReader(s.getInputStream())));\r\n\u00a0 }\r\n\r\n\u00a0 \/** Make a PrintWriter to send outgoing data.\r\n\u00a0\u00a0 *\u00a0 This PrintWriter will automatically flush stream\r\n\u00a0\u00a0 *\u00a0 when println is called.\r\n\u00a0\u00a0 *\/\r\n\r\n\u00a0 public static PrintWriter getWriter(Socket s)\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 throws IOException {\r\n\u00a0\u00a0\u00a0 \/\/ Second argument of true means autoflush.\r\n\u00a0\u00a0\u00a0 return(new PrintWriter(s.getOutputStream(), true));\r\n\u00a0 }\r\n}<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>import java.net.*; import java.io.*; \/** Given an e-mail address of the form user@host, \u00a0*\u00a0 connect to port 25 of the host and issue an \u00a0*\u00a0 &#8216;expn&#8217; request for the user. Print the results. \u00a0* \u00a0*\u00a0 Taken from Core Web Programming from \u00a0*\u00a0 Prentice Hall and Sun Microsystems Press, \u00a0*\u00a0 . \u00a0*\u00a0 \u00a9 2001 Marty Hall &hellip; <\/p>\n<p><a class=\"more-link btn\" href=\"http:\/\/bangla.sitestree.com\/?p=10189\">Continue reading<\/a><\/p>\n","protected":false},"author":130,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[1417,1424],"tags":[706,308,1463,285],"class_list":["post-10189","post","type-post","status-publish","format-standard","hentry","category-code-programming-samples--","category-javaj2eej2me","tag-code","tag-java","tag-smtp","tag-285","item-wrap"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack-related-posts":[{"id":26589,"url":"http:\/\/bangla.sitestree.com\/?p=26589","url_meta":{"origin":10189,"position":0},"title":"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: #Programming Code Examples #Java\/J2EE\/J2ME #Network Programming","author":"Author-Check- Article-or-Video","date":"April 29, 2021","format":false,"excerpt":"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: 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'\u2026","rel":"","context":"In &quot;FromSitesTree.com&quot;","block_context":{"text":"FromSitesTree.com","link":"http:\/\/bangla.sitestree.com\/?cat=1917"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":26489,"url":"http:\/\/bangla.sitestree.com\/?p=26489","url_meta":{"origin":10189,"position":1},"title":"UriRetriever.java  Accepts a host, port, and file from the command line and retrieves the implied URL from the HTTP server by issuing a GET request. Uses the following classes: #Programming Code Examples #NULL #Network Programming","author":"Author-Check- Article-or-Video","date":"April 26, 2021","format":false,"excerpt":"UriRetriever.java Accepts a host, port, and file from the command line and retrieves the implied URL from the HTTP server by issuing a GET request. Uses the following classes: import java.net.*; import java.io.*; \/** Retrieve a URL given the host, port, and file as three * separate command-line arguments. A\u2026","rel":"","context":"In &quot;FromSitesTree.com&quot;","block_context":{"text":"FromSitesTree.com","link":"http:\/\/bangla.sitestree.com\/?cat=1917"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":26591,"url":"http:\/\/bangla.sitestree.com\/?p=26591","url_meta":{"origin":10189,"position":2},"title":"UrlRetriever.java  Accepts an URL from the command line, parses the host, port, and URI components from the URL and then retrieves the document. Requires the following classes: #Programming Code Examples #Java\/J2EE\/J2ME #Network Programming","author":"Author-Check- Article-or-Video","date":"April 29, 2021","format":false,"excerpt":"UrlRetriever.java Accepts an URL from the command line, parses the host, port, and URI components from the URL and then retrieves the document. Requires the following classes: import java.util.*; \/** This parses the input to get a host, port, and file, then * passes these three values to the UriRetriever\u2026","rel":"","context":"In &quot;FromSitesTree.com&quot;","block_context":{"text":"FromSitesTree.com","link":"http:\/\/bangla.sitestree.com\/?cat=1917"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":10192,"url":"http:\/\/bangla.sitestree.com\/?p=10192","url_meta":{"origin":10189,"position":3},"title":"UrlRetriever.java Accepts an URL from the command line, parses the host, port, and URI components from the URL and then retrieves the document. Requires the following classes","author":"","date":"August 24, 2015","format":false,"excerpt":"UrlRetriever.java\u00a0 Accepts an URL from the command line, parses the host, port, and URI components from the URL and then retrieves the document. Requires the following classes: import java.util.*; \/** This parses the input to get a host, port, and file, then \u00a0*\u00a0 passes these three values to the UriRetriever\u2026","rel":"","context":"In &quot;Code . Programming Samples . \u09aa\u09cd\u09b0\u09cb\u0997\u09cd\u09b0\u09be\u09ae \u0989\u09a6\u09be\u09b9\u09b0\u09a8&quot;","block_context":{"text":"Code . Programming Samples . \u09aa\u09cd\u09b0\u09cb\u0997\u09cd\u09b0\u09be\u09ae \u0989\u09a6\u09be\u09b9\u09b0\u09a8","link":"http:\/\/bangla.sitestree.com\/?cat=1417"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":26745,"url":"http:\/\/bangla.sitestree.com\/?p=26745","url_meta":{"origin":10189,"position":4},"title":"RMI Example &#8211; Message, illustrates retrieving a message from an object located on a remote server. Requires the following classes: #Programming Code Examples #Java\/J2EE\/J2ME #Network Programming","author":"Author-Check- Article-or-Video","date":"April 30, 2021","format":false,"excerpt":"RMI Example - Message, illustrates retrieving a message from an object located on a remote server. Requires the following classes: Rem.java Establishes which methods the client can access in the remote object. import java.rmi.*; \/** The RMI client will use this interface directly. The RMI * server will make a\u2026","rel":"","context":"In &quot;FromSitesTree.com&quot;","block_context":{"text":"FromSitesTree.com","link":"http:\/\/bangla.sitestree.com\/?cat=1917"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":10194,"url":"http:\/\/bangla.sitestree.com\/?p=10194","url_meta":{"origin":10189,"position":5},"title":"UrlRetriever2.java Illustrates how the URL class can simplify communication to an HTTP server.","author":"","date":"August 25, 2015","format":false,"excerpt":"UrlRetriever2.java\u00a0 Illustrates how the URL class can simplify communication to an HTTP server. import java.net.*; import java.io.*; \/** Read a remote file using the standard URL class \u00a0*\u00a0 instead of connecting explicitly to the HTTP server. \u00a0* \u00a0*\u00a0 Taken from Core Web Programming from \u00a0*\u00a0 Prentice Hall and Sun Microsystems\u2026","rel":"","context":"In &quot;Code . Programming Samples . \u09aa\u09cd\u09b0\u09cb\u0997\u09cd\u09b0\u09be\u09ae \u0989\u09a6\u09be\u09b9\u09b0\u09a8&quot;","block_context":{"text":"Code . Programming Samples . \u09aa\u09cd\u09b0\u09cb\u0997\u09cd\u09b0\u09be\u09ae \u0989\u09a6\u09be\u09b9\u09b0\u09a8","link":"http:\/\/bangla.sitestree.com\/?cat=1417"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"_links":{"self":[{"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/posts\/10189","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/users\/130"}],"replies":[{"embeddable":true,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=10189"}],"version-history":[{"count":2,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/posts\/10189\/revisions"}],"predecessor-version":[{"id":10191,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/posts\/10189\/revisions\/10191"}],"wp:attachment":[{"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10189"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10189"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10189"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}