Receive email by java client

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 8 Stream API Analogies in Kotlin
Java Program to Implement Interpolation Search Algorithm
Java Program for Douglas-Peucker Algorithm Implementation
An Intro to Spring Cloud Vault
Java Program to Apply Above-Below-on Test to Find the Position of a Point with respect to a Line
Java Program to Implement an Algorithm to Find the Global min Cut in a Graph
Từ khóa this và super trong Java
Convert Time to Milliseconds in Java
Java Program to Find kth Smallest Element by the Method of Partitioning the Array
Giới thiệu Google Guice – Dependency injection (DI) framework
Custom JUnit 4 Test Runners
A Guide to Spring Boot Admin
How to Get a Name of a Method Being Executed?
Java Program to Implement Pollard Rho Algorithm
Java Program to Implement LinkedList API
Mapping Nested Values with Jackson
Java Program to Describe the Representation of Graph using Adjacency List
ETags for REST with Spring
Java Program to Implement Bloom Filter
Java Program to Implement Borwein Algorithm
Request a Delivery / Read Receipt in Javamail
Pagination and Sorting using Spring Data JPA
Java Program to Check whether Graph is a Bipartite using 2 Color Algorithm
JUnit 5 for Kotlin Developers
Java 8 – Powerful Comparison with Lambdas
Converting a List to String in Java
Check If a String Is Numeric in Java
How to Manually Authenticate User with Spring Security
Using Java Assertions
Introduction to Spring Cloud Netflix – Eureka
Spring Boot - Zuul Proxy Server and Routing
Java Program to Implement Gift Wrapping Algorithm in Two Dimensions