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 Patricia Trie
Java Program to Find Nearest Neighbor Using Linear Search
Spring Cloud – Bootstrapping
Java Program to Implement Iterative Deepening
Spring WebClient and OAuth2 Support
Tạo chương trình Java đầu tiên sử dụng Eclipse
Hướng dẫn Java Design Pattern – Object Pool
Java Program to Implement Coppersmith Freivald’s Algorithm
A Guide to HashSet in Java
Flattening Nested Collections in Java
Introduction to the Functional Web Framework in Spring 5
Spring WebFlux Filters
Tránh lỗi ConcurrentModificationException trong Java như thế nào?
Spring Security Login Page with React
Giới thiệu Java 8
A Custom Data Binder in Spring MVC
Java Program to Implement Pairing Heap
Java Program to Perform Complex Number Multiplication
Spring Boot - OAuth2 with JWT
Immutable Map Implementations in Java
Java Program to Implement Gift Wrapping Algorithm in Two Dimensions
Java Program to Implement the Binary Counting Method to Generate Subsets of a Set
Different Ways to Capture Java Heap Dumps
Spring Webflux and CORS
Từ khóa this và super trong Java
Introduction to Apache Commons Text
Spring Boot - Sending Email
Java Program to Implement Regular Falsi Algorithm
Các kiểu dữ liệu trong java
Consuming RESTful Web Services
Using the Not Operator in If Conditions in Java
Jackson vs Gson