Retrieving email using Java POP Library

Demonstrates how to retrieve emails from a POP3 server using Java POP library.
  1. Blog

java pop and java email

Email messages are made of required parts, such as the bunch-of-exclamation-marks part ["HOO!!!!!!!"], the emoticon part [;-)], and the abbreviations part [ROTFL]. All emails are required to contain these parts (at least, all the emails I get have them), as well as other parts such as the Subject, the From, the To and the Body. The JSCAPE Secure iNet Factory library lets you use Post Office Protocol 3 (POP3) to write a client program that will retrieve your email and look at the parts. (That is, the parts in the second list!!!!!!! LMAO :-)).

There are two ways to authenticate a client to the POP3 mail server, username and password (described here) or APOP, which is described in the Securely Retrieving Email from GMail using Java POP Library. Each email message creates an EmailMessage instance, and various get methods are used in this example to extract and print the messages.

Retrieving Emails using Java POP3

import com.jscape.inet.email.*;
import com.jscape.inet.mime.*;
import java.util.Enumeration;
import java.io.*;

public class POPMail {
    private String hostname = "mail.someserver.com";
        private String username = "rufus@mailinator.com";
        private String password = "Gee1Dad2Its3A4Wurlitzer";
        
    // Get all your messages
    public void getMessages() throws PopException, MimeException, IOException {
                Pop pop = new Pop(hostname,username,password);
        pop.connect();
        System.out.println("Retrieving messages...");
        
        // get message count
        int messageCount = pop.getMessageCount();
        
        if(messageCount < 1) {
            System.out.println("No messages found");
        }

        // Print something for each message. Note that the getMessage() method
        // (not used here) brings back all the parts.
        for(int i = 1; i <= messageCount; ++i) {
          EmailMessage msg = (EmailMessage)pop.getMessage(i);
          System.out.println("-----------------");
          System.out.println("Subject: " + msg.getSubject());
                  System.out.println("Date: " + msg.getDate());
                  System.out.println("Body: " + msg.getBody());
          System.out.println("-----------------");
                }
        pop.disconnect();
    }
    
    public static void main(String[] args) {
        String hostname;
        String username;
        String password;
        
        try {
            POPMail example = new POPMail();
            example.getMessages();
        }
        catch(Exception e) {
            e.printStackTrace();
        }
    }
}

This mail retriever is insecure because it transmits the username and password in clear text. For a secure method consider using APOP authentication in combination with SSL/TLS as described in the article Securely Retrieving Email from GMail using Java POP Library. This article is specific to GMail but can be applied to any POP3 server that supports APOP and SSL/TLS connections.

By the way, POP3 is the only PO protocol in use. There was some effort years ago to define a POP4 but it never amounted to anything.

Download Secure iNet Factory