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:
LinkedList trong java
Redirect to Different Pages after Login with Spring Security
New Features in Java 14
Bootstrap a Web Application with Spring 5
Java Program to Implement Flood Fill Algorithm
Inheritance with Jackson
Java IO vs NIO
Loại bỏ các phần tử trùng trong một ArrayList như thế nào?
Java Program to Implement a Binary Search Algorithm for a Specific Search Sequence
Hướng dẫn sử dụng Lớp FilePermission trong java
Java Program to Generate Random Numbers Using Middle Square Method
Java Program to Implement Fenwick Tree
Quick Guide to Spring Bean Scopes
Apache Camel with Spring Boot
Guide to ThreadLocalRandom in Java
Using the Map.Entry Java Class
Spring RestTemplate Request/Response Logging
Bootstrapping Hibernate 5 with Spring
Testing in Spring Boot
Java Program to Find the Edge Connectivity of a Graph
Using the Not Operator in If Conditions in Java
Using Spring ResponseEntity to Manipulate the HTTP Response
Java Program to Implement Randomized Binary Search Tree
File Upload with Spring MVC
Circular Dependencies in Spring
Entity To DTO Conversion for a Spring REST API
Custom Exception trong Java
Guide to DelayQueue
Tìm hiểu về xác thực và phân quyền trong ứng dụng
Java Program to Implement Bellman-Ford Algorithm
Java Program to Check whether Graph is a Bipartite using 2 Color Algorithm
Serve Static Resources with Spring