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:
How to Manually Authenticate User with Spring Security
Java Program to Implement Queue using Linked List
A Comparison Between Spring and Spring Boot
String Joiner trong Java 8
Java Program to Implement Fibonacci Heap
Java Program to Create a Minimal Set of All Edges Whose Addition will Convert it to a Strongly Conne...
Spring Cloud Bus
Giới thiệu Google Guice – Dependency injection (DI) framework
Java Program to Implement ConcurrentHashMap API
Java Program to Implement Flood Fill Algorithm
Java Program to Implement an Algorithm to Find the Global min Cut in a Graph
Java Program to Implement Kosaraju Algorithm
Convert String to Byte Array and Reverse in Java
A Guide to JUnit 5 Extensions
Apache Commons Collections MapUtils
Guide to the Java ArrayList
Java Program to Check if an UnDirected Graph is a Tree or Not Using DFS
Hướng dẫn Java Design Pattern – Proxy
Service Registration with Eureka
XML-Based Injection in Spring
Lớp Properties trong java
Java Program to Implement ScapeGoat Tree
Giới thiệu Google Guice – Injection, Scope
Java Program to Check whether Graph is Biconnected
Hướng dẫn kết nối cơ sở dữ liệu với Java JDBC
Java Program to Implement Skip List
Assertions in JUnit 4 and JUnit 5
Guide to Java 8’s Collectors
Show Hibernate/JPA SQL Statements from Spring Boot
Java Program to Check if a Matrix is Invertible
Các kiểu dữ liệu trong java
Java Program to Implement Insertion Sort