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:
Testing in Spring Boot
Java Program to Implement the Alexander Bogomolny’s UnOrdered Permutation Algorithm for Elements Fro...
Guide to Java OutputStream
Running Spring Boot Applications With Minikube
Spring RestTemplate Error Handling
Java Program to Delete a Particular Node in a Tree Without Using Recursion
Spring 5 WebClient
How to Manually Authenticate User with Spring Security
Supplier trong Java 8
Hướng dẫn kết nối cơ sở dữ liệu với Java JDBC
Sử dụng Fork/Join Framework với ForkJoinPool trong Java
Period and Duration in Java
Intersection of Two Lists in Java
New Features in Java 13
Java Program to implement Sparse Vector
Java Program to Implement Graph Coloring Algorithm
Converting a Stack Trace to a String in Java
Spring WebClient vs. RestTemplate
Spring Boot - Hystrix
Java Program to do a Depth First Search/Traversal on a graph non-recursively
Logout in an OAuth Secured Application
Collection trong java
Java Perform to a 2D FFT Inplace Given a Complex 2D Array
Prevent Brute Force Authentication Attempts with Spring Security
Generating Random Dates in Java
Configuring a DataSource Programmatically in Spring Boot
Testing an OAuth Secured API with Spring MVC
Java Program to Implement Disjoint Sets
Java Program to Find k Numbers Closest to Median of S, Where S is a Set of n Numbers
How to Round a Number to N Decimal Places in Java
Spring – Injecting Collections
Registration with Spring Security – Password Encoding