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:
Exception Handling in Java
Spring Boot with Multiple SQL Import Files
Vấn đề Nhà sản xuất (Producer) – Người tiêu dùng (Consumer) và đồng bộ hóa các luồng trong Java
Using Spring ResponseEntity to Manipulate the HTTP Response
Sort a HashMap in Java
Introduction to Spring Security Expressions
Java Program to Implement Jarvis Algorithm
Java Program to Implement ArrayDeque API
Java Program to Use rand and srand Functions
Java Program to Encode a Message Using Playfair Cipher
A Guide to Apache Commons Collections CollectionUtils
Java Program to Find the Peak Element of an Array O(n) time (Naive Method)
Collect a Java Stream to an Immutable Collection
Jackson – Unmarshall to Collection/Array
Java – String to Reader
Apache Tiles Integration with Spring MVC
Java Program to Emulate N Dice Roller
Java Program to Implement Find all Back Edges in a Graph
Java – Write an InputStream to a File
@Before vs @BeforeClass vs @BeforeEach vs @BeforeAll
Java Program to Implement Bucket Sort
Object Type Casting in Java
How to Add a Single Element to a Stream
A Guide to Concurrent Queues in Java
Introduction to Spliterator in Java
Spring Security and OpenID Connect
Java Program to Implement Control Table
Logging in Spring Boot
Java Program to Implement Horner Algorithm
Java Program to Perform Preorder Non-Recursive Traversal of a Given Binary Tree
Java Program to Implement Karatsuba Multiplication Algorithm
Java Program to Implement Depth-limited Search