Exam code:
import java.net.*; import java.io.*; public class CheckMail { public static void main(String s[]) { // // CheckMail [mailServer] [user] [password] // try { CheckMail t = new CheckMail(); int i = t.checkMyMail(s[0], s[1], s[2]); if (i==0) { System.out.println("No mail waiting."); } else { System.out.println ("There " + (i==1?"is " :"are ") + i + " message" +(i==1?"":"s")+ " waiting."); } } catch (Exception e) { e.printStackTrace(); } } private void send(BufferedWriter out, String s) throws IOException { out.write(s+"\n"); out.flush(); } private String receive(BufferedReader in) throws IOException { return in.readLine(); } private int checkMyMail (String server, String user, String pass) throws IOException { Socket s = new Socket(server, 110); BufferedReader in = new BufferedReader( new InputStreamReader(s.getInputStream())); BufferedWriter out = new BufferedWriter( new OutputStreamWriter(s.getOutputStream())); receive(in); send(out, "USER " + user); receive(in); send(out, "PASS " + pass); receive(in); 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 Rolling Hash
A Custom Data Binder in Spring MVC
Stack Memory and Heap Space in Java
Java Program to Implement TreeSet API
Spring @RequestMapping New Shortcut Annotations
Netflix Archaius with Various Database Configurations
Consuming RESTful Web Services
Configure a RestTemplate with RestTemplateBuilder
Java Program to Implement Circular Singly Linked List
Introduction to the Functional Web Framework in Spring 5
Java Program to Implement Park-Miller Random Number Generation Algorithm
Tổng quan về ngôn ngữ lập trình java
Custom Thread Pools In Java 8 Parallel Streams
Receive email using IMAP
Java Program to Implement Sorted Circular Doubly Linked List
How to Get All Dates Between Two Dates?
Summing Numbers with Java Streams
Java Program to Implement Pagoda
Java Program to Implement Interpolation Search Algorithm
Xử lý ngoại lệ trong Java (Exception Handling)
New Features in Java 12
Java Program to Compute Cross Product of Two Vectors
Java Program to Implement Sorted Circularly Singly Linked List
Using a Mutex Object in Java
Java Program to Implement Sorted Doubly Linked List
Java Program to Find a Good Feedback Vertex Set
Java Program to Implement Meldable Heap
Java Program to Implement Interpolation Search Algorithm
LinkedHashSet trong java
Java Program to Find Second Smallest of n Elements with Given Complexity Constraint
Java Program to Implement Find all Back Edges in a Graph
JWT – Token-based Authentication trong Jersey 2.x