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:
Java Program to Construct an Expression Tree for an Infix Expression
Hướng dẫn Java Design Pattern – Transfer Object
Extract links from an HTML page
Thực thi nhiều tác vụ cùng lúc như thế nào trong Java?
Java Program to Compute the Volume of a Tetrahedron Using Determinants
Spring Boot - Rest Controller Unit Test
Java Program to Implement Queue
Java Program to Generate All Subsets of a Given Set in the Lexico Graphic Order
Cachable Static Assets with Spring MVC
Guide to Guava Multimap
Entity To DTO Conversion for a Spring REST API
Java TreeMap vs HashMap
Send email with authentication
CharSequence vs. String in Java
Java Program to Generate a Sequence of N Characters for a Given Specific Case
OAuth2 for a Spring REST API – Handle the Refresh Token in Angular
Tránh lỗi NullPointerException trong Java như thế nào?
Java Program to Check whether Directed Graph is Connected using BFS
Java Program to Implement Singly Linked List
Java Program to Print the Kind of Rotation the AVL Tree is Undergoing
Spring MVC Setup with Kotlin
Spring’s RequestBody and ResponseBody Annotations
Java Program to Describe the Representation of Graph using Incidence List
Java Program to Implement Brent Cycle Algorithm
Spring Boot - Scheduling
Jackson – Bidirectional Relationships
Java Program to Implement Fenwick Tree
Intro to Inversion of Control and Dependency Injection with Spring
Posting with HttpClient
Java Web Services – Jersey JAX-RS – REST và sử dụng REST API testing tools với Postman
Basic Authentication with the RestTemplate
Guide to java.util.concurrent.Locks