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 Program to Perform Partition of an Integer in All Possible Ways
Java Program to Perform Inorder Non-Recursive Traversal of a Given Binary Tree
Java – InputStream to Reader
How to Round a Number to N Decimal Places in Java
Java Program to Find ith Largest Number from a Given List Using Order-Statistic Algorithm
Ignore Null Fields with Jackson
Java Program to Implement Hopcroft Algorithm
Date Time trong Java 8
Java Program to Find Shortest Path Between All Vertices Using Floyd-Warshall’s Algorithm
HashSet trong Java hoạt động như thế nào?
Một số nguyên tắc, định luật trong lập trình
Guide to Java 8 groupingBy Collector
Java Program to Remove the Edges in a Given Cyclic Graph such that its Linear Extension can be Found
Send email with JavaMail
Reactive WebSockets with Spring 5
Java Program to Implement Fermat Factorization Algorithm
Lập trình mạng với java
Java Program to Implement Solovay Strassen Primality Test Algorithm
Java Program to Implement Hash Tables with Double Hashing
Java Program to Check Whether Topological Sorting can be Performed in a Graph
Java Program to Implement Iterative Deepening
Java Program to Implement Extended Euclid Algorithm
Java Program to Implement Merge Sort Algorithm on Linked List
Java Program to Check whether Graph is a Bipartite using BFS
Exploring the Spring Boot TestRestTemplate
Spring WebClient and OAuth2 Support
Java Program to Perform Right Rotation on a Binary Search Tree
Java Program to Check if any Graph is Possible to be Constructed for a Given Degree Sequence
Exploring the Spring 5 WebFlux URL Matching
RegEx for matching Date Pattern in Java
Using Spring @ResponseStatus to Set HTTP Status Code
Hướng dẫn Java Design Pattern – Memento