import java.net.*;
import java.io.*;
public class DisplayMail {
public static void main(String arg[]) {
//
// usage :
// DisplayMail [mailServer] [user] [password]
// (will not delete mail on the server)
//
try {
// connect on port 110 (POP3)
System.out.println("Connect to " + arg[0] + ":110");
Socket s = new Socket(arg[0], 110);
BufferedReader in = new BufferedReader(
new InputStreamReader(s.getInputStream()));
BufferedWriter out = new BufferedWriter(
new OutputStreamWriter(s.getOutputStream()));
DisplayMail mail = new DisplayMail();
mail.login(in, out, arg[1], arg[2]);
int i = mail.check(in,out);
if (i==0) {
System.out.println("No mail waiting.");
}
else {
for (int j=1; j <= i; j++) {
String msg = mail.get(in, out, j);
System.out.println("*****");
System.out.println(msg);
System.out.println("*****");
}
//
// If the mail was removed from the server
// (see getMail()) then we must COMMIT with
// the "QUIT" command :
// send(out, "QUIT");
//
}
}
catch (Exception e) {
e.printStackTrace();
}
}
public String get
(BufferedReader in, BufferedWriter out, int i)
throws IOException {
String s = "";
String t = "";
send(out, "RETR "+i);
while (((s = in.readLine()) != null)
&&(!(s.equals(".")))) {
t += s + "\n";
}
//
// To remove the mail on the server :
// send(out, "DELE "+i);
// receive(in);
//
return t;
}
private void send(BufferedWriter out, String s)
throws IOException {
System.out.println(s);
out.write(s+"\n");
out.flush();
}
private String receive(BufferedReader in)
throws IOException {
String s = in.readLine();
System.out.println(s);
return s;
}
private void login
(BufferedReader in, BufferedWriter out, String user, String pass)
throws IOException {
receive(in);
send(out, "HELO theWorld");
receive(in);
send(out, "USER " + user);
receive(in);
send(out, "PASS " + pass);
receive(in);
}
private int check
(BufferedReader in, BufferedWriter out)
throws IOException {
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:
Custom Thread Pools In Java 8 Parallel Streams
Removing all Nulls from a List in Java
Spring Cloud – Adding Angular
Java Stream Filter with Lambda Expression
Java Program to Implement Sorted Circular Doubly Linked List
Class Loaders in Java
Java Program to Implement Fibonacci Heap
Hướng dẫn sử dụng Java Annotation
Check If a File or Directory Exists in Java
Java – Convert File to InputStream
Deploy a Spring Boot App to Azure
Java Program to Implement Karatsuba Multiplication Algorithm
Java Program to Implement Selection Sort
Java Program to Perform Postorder Non-Recursive Traversal of a Given Binary Tree
Vòng lặp for, while, do-while trong Java
Java Program to Implement Cubic convergence 1/pi Algorithm
Java Program to Implement Floyd Cycle Algorithm
JUnit5 Programmatic Extension Registration with @RegisterExtension
A Custom Data Binder in Spring MVC
Java Program to Implement ArrayBlockingQueue API
Spring Boot - CORS Support
Vector trong Java
The XOR Operator in Java
Adding Shutdown Hooks for JVM Applications
Java Program to Print the Kind of Rotation the AVL Tree is Undergoing
REST Web service: Upload và Download file với Jersey 2.x
Java Program to Implement CopyOnWriteArraySet API
Java Program to Generate N Number of Passwords of Length M Each
Default Password Encoder in Spring Security 5
Java Program to Implement Sorted Array
Derived Query Methods in Spring Data JPA Repositories
Spring Boot - Admin Client