import java.net.*; import java.io.*; public class DisplayMail { public static void main(String arg[]) { // // usage : // DisplayMail [mailServer] [user] [password] // (will not delete mail on the server) // try { // connect on port 110 (POP3) System.out.println("Connect to " + arg[0] + ":110"); Socket s = new Socket(arg[0], 110); BufferedReader in = new BufferedReader( new InputStreamReader(s.getInputStream())); BufferedWriter out = new BufferedWriter( new OutputStreamWriter(s.getOutputStream())); DisplayMail mail = new DisplayMail(); mail.login(in, out, arg[1], arg[2]); int i = mail.check(in,out); if (i==0) { System.out.println("No mail waiting."); } else { for (int j=1; j <= i; j++) { String msg = mail.get(in, out, j); System.out.println("*****"); System.out.println(msg); System.out.println("*****"); } // // If the mail was removed from the server // (see getMail()) then we must COMMIT with // the "QUIT" command : // send(out, "QUIT"); // } } catch (Exception e) { e.printStackTrace(); } } public String get (BufferedReader in, BufferedWriter out, int i) throws IOException { String s = ""; String t = ""; send(out, "RETR "+i); while (((s = in.readLine()) != null) &&(!(s.equals(".")))) { t += s + "\n"; } // // To remove the mail on the server : // send(out, "DELE "+i); // receive(in); // return t; } private void send(BufferedWriter out, String s) throws IOException { System.out.println(s); out.write(s+"\n"); out.flush(); } private String receive(BufferedReader in) throws IOException { String s = in.readLine(); System.out.println(s); return s; } private void login (BufferedReader in, BufferedWriter out, String user, String pass) throws IOException { receive(in); send(out, "HELO theWorld"); receive(in); send(out, "USER " + user); receive(in); send(out, "PASS " + pass); receive(in); } private int check (BufferedReader in, BufferedWriter out) throws IOException { return getNumberOfMessages(in, out); } public int getNumberOfMessages (BufferedReader in, BufferedWriter out) throws IOException { int i = 0; String s; send(out, "LIST"); receive(in); while((s = receive(in)) != null) { if (!(s.equals("."))) { i++; } else { return i; } } return 0; } }
Done! Happy Coding!
Related posts:
Java Program to Implement Warshall Algorithm
Apache Commons Collections Bag
Java Program to Compute Discrete Fourier Transform Using the Fast Fourier Transform Approach
Spring WebClient Filters
Java Program to Perform Insertion in a BST
Immutable Map Implementations in Java
Java – Random Long, Float, Integer and Double
How to Remove the Last Character of a String?
Java Program to Implement Graham Scan Algorithm to Find the Convex Hull
A Guide to EnumMap
Spring Boot - Internationalization
Spring Boot - Cloud Configuration Server
Hướng dẫn kết nối cơ sở dữ liệu với Java JDBC
Java Program to Check Cycle in a Graph using Topological Sort
ETL with Spring Cloud Data Flow
A Guide to TreeSet in Java
Working with Network Interfaces in Java
Guide to CopyOnWriteArrayList
Java Program to Find a Good Feedback Edge Set in a Graph
Serialize Only Fields that meet a Custom Criteria with Jackson
Java Program to Check Whether a Directed Graph Contains a Eulerian Cycle
Java Program to implement Sparse Vector
Spring Security Form Login
Java Program to Implement Quick sort
Java Program to Solve a Matching Problem for a Given Specific Case
Uploading MultipartFile with Spring RestTemplate
Inject Parameters into JUnit Jupiter Unit Tests
Join and Split Arrays and Collections in Java
Thực thi nhiều tác vụ cùng lúc như thế nào trong Java?
Java Program to Implement Stack using Two Queues
Removing all duplicates from a List in Java
A Guide to JPA with Spring