Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

java connect socket to POP3

// POP3Demo.java

import java.io.*;
import java.net.*;

class POP3Demo
{
  public static void main (String [] args)
  {
   String POP3Server = "mail.gatewest.net";
   int POP3Port = 110;

   Socket client = null;

   try
   {
     // Attempt to create a client socket connected to the POP3 
     // server program.

     client = new Socket (POP3Server, POP3Port);

     // Create a buffered reader for line-oriented reading from the
     // standard input device.

     BufferedReader stdin;
     stdin = new BufferedReader (new InputStreamReader (System.in));

     // Create a buffered reader for line-oriented reading from the
     // socket.

     InputStream is = client.getInputStream ();
     BufferedReader sockin;
     sockin = new BufferedReader (new InputStreamReader (is));

     // Create a print writer for line-oriented writing to the 
     // socket.

     OutputStream os = client.getOutputStream ();
     PrintWriter sockout;
     sockout = new PrintWriter (os, true); // true for auto-flush

     // Display POP3 greeting from POP3 server program.

     System.out.println ("S:" + sockin.readLine ());

     while (true)
     {
      // Display a client prompt.

      System.out.print ("C:");

      // Read a command string from the standard input device.

      String cmd = stdin.readLine ();

      // Write the command string to the POP3 server program.

      sockout.println (cmd);

      // Read a reply string from the POP3 server program.

      String reply = sockin.readLine ();

      // Display the first line of this reply string.

      System.out.println ("S:" + reply);

      // If the RETR command was entered and it succeeded, keep
      // reading all lines until a line is read that begins with 
      // a . character. These lines constitute an email message.

      if (cmd.toLowerCase ().startsWith ("retr") &&
        reply.charAt (0) == '+')
        do
        {
          reply = sockin.readLine ();
          System.out.println ("S:" + reply);
          if (reply != null && reply.length () > 0)
            if (reply.charAt (0) == '.')
              break;
        }
        while (true);

      // If the QUIT command was entered, quit.

      if (cmd.toLowerCase ().startsWith ("quit"))
        break;
     }
   }
   catch (IOException e)
   {
     System.out.println (e.toString ());
   }
   finally
   {
     try
     {
      // Attempt to close the client socket.

      if (client != null)
        client.close ();
     }
     catch (IOException e)
     {
     }
   }
  }
}
Comment

PREVIOUS NEXT
Code Example
Java :: Java schleifen 
Java :: Set value of input in android webview 
Java :: how to install openjdk 16 
Java :: Implementation of HashMap Class in Java map 
Java :: what does .set do in java 
Java :: zweidimensionales array erstellen java 
Java :: about action Listioner in java Swing 
Java :: setlist arraylist java swing example 
Java :: javafx open alert window 
Java :: spring data rest id missing 
Java :: how-to-use-volley-string-request-in-android 
Java :: open youtube by default in full landscape mode pragmatically 
Java :: Java @SuppressWarnings Annotation Example 
Java :: download sources and javadoc 
Java :: set jtextfield max length 
Java :: check if two characters are equal java 
Java :: java no enum constant 
Java :: Magic square java user input 
Java :: how to print message to console java 
Java :: transpose array in java 
Java :: pojo api testing 
Java :: java regex check if group exists 
Java :: komplettes array ausgeben java 
Java :: asserttrue in selenium java 
Java :: two array structures in java 
Java :: java get if contains else 0 
Java :: java random number generator 6 
Java :: Artemis agent/client auto failover 
Java :: open youtube by default in full screen pragmatically in android 
Java :: Java Creating strings using the new keyword 
ADD CONTENT
Topic
Content
Source link
Name
8+1 =