import java.io.*; import java.net.*; import java.util.*; /** * MailStat is used to check if mail is waiting at the given POP3 post * office. * * @author Thornton Rose * @version 1.0 */ public class MailStat { private static final int POP3_PORT = 110; public static void main(String[] args) { String host; InetAddress hostAddress; String username; String password; Socket mailSocket; BufferedReader socketInput; DataOutputStream socketOutput; // Check args. if (args.length < 3) { System.out.println("Usage: MailStat [host] [username] [password]"); System.out.println(""); System.out.println("Parameters:"); System.out.println(" host = Name or IP address of mail host."); System.out.println(" username = Account on mail host."); System.out.println(" password = Password on mail host."); } else { // Get host, username, and password. host = args[0]; username = args[1]; password = args[2]; try { // Open the connection to the host. hostAddress = InetAddress.getByName(host); System.out.println("Opening connection to " + hostAddress + "..."); mailSocket = new Socket(host, POP3_PORT); try { // Get references to the input and output streams. socketInput = new BufferedReader( new InputStreamReader(mailSocket.getInputStream()) ); socketOutput = new DataOutputStream(mailSocket.getOutputStream()); // Get the initial reply from the server. readReply(socketInput); // Authenticate: send username and password. sendCommand(socketOutput, "USER " + username); readReply(socketInput); sendCommand(socketOutput, "PASS " + password); readReply(socketInput); // Get the status. sendCommand(socketOutput, "STAT"); readReply(socketInput); // End the session. sendCommand(socketOutput, "QUIT"); readReply(socketInput); } finally { mailSocket.close(); } } catch(Exception theException) { System.out.println(theException); } } System.exit(0); } /** * sendCommand() sends an POP3 command to the POP3 server. An * POP3 command is a string that looks like: *
* [keyword] [data][CR][LF] ** *
Example:
USER trose* * @param out The output stream to which to write the command. * @param command The command to write. * * @exception IOException No special handling is done; the exception is * passed back up the call chain. */ private static void sendCommand(DataOutputStream out, String command) throws IOException { System.out.println(command); out.writeBytes(command + "\r\n"); } /** * readReply() reads the reply from the POP3 server. * * @param reader The input reader from which to read the reply. * */ /* @exception IOException If an IOException occurs because of an error * with socket IO, no special handling is done, and the exception is * passed back up the call chain. If the reply from the server * indicates that an error occurred, an IOException containing * "POP3: " + the reply is thrown. */ private static String readReply(BufferedReader reader) throws IOException, Exception { String reply; reply = reader.readLine(); System.out.println(reply); if (reply.substring(0, 4).equalsIgnoreCase("-ERR")) { throw (new Exception("POP3: " + reply)); } return reply; } }