Use Commons Net POP3Client to
check a POP3 mailbox for incoming mail. The following example connects
to the POP3 server www.discursive.com, logs in as tobrien@discursive.com, and prints each
message in the mailbox:
import org.apache.commons.io.CopyUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.net.pop3.POP3Client;
import org.apache.commons.net.pop3.POP3MessageInfo;
POP3Client client = new POP3Client( );
client.connect("www.discursive.com");
client.login("tobrien@discursive.com", "secretpassword");
POP3MessageInfo[] messages = client.listMessages( );
for (int i = 0; i < messages.length; i++) {
int messageNum = messages[i].number;
System.out.println( "************* Message number: " + messageNum );
Reader reader = client.retrieveMessage( messageNum );
System.out.println( "Message:\n" + IOUtils.toString( reader ) );
IOUtils.closeQuietly( reader );
}
client.logout( );
client.disconnect( );This example calls client.listMessage() to get an array of POP3MessageInfo objects. Each message is
retrieved using the message number contained in each POP3MessageInfo. To retrieve the contents of
an individual message, the message number is passed to retrieveMessage() , which returns a Reader from which the message body is read.
The previous example prints the contents of a POP3 mailbox, as shown
below:
************* Message number: 1
Message:
Return-Path: <jerk@spamheaven.net>
X-Original-To: tobrien@discursive.com
Delivered-To: tobrien@discursive.com
Received: from jerk-net.co.jp (unknown [219.71.255.123])
by pericles.symbiont.net (Postfix) with SMTP id 6FA54543FE
for <tobrien@discursive.com>; Tue, 22 Jun 2004 02:19:13 -0400 (EDT)
Received: from 228.4.65.206 by smtp.cito.nl;
Tue, 22 Jun 2004 06:09:26 +0000
Message-ID: <9b8001c4581f$2524e2e9$d8470c90@jerk-net.co.jp>
From: "Spammer" <jerk@spamheaven.net>
To: tobrien@discursive.com
Subject: Hey, I heard you need a mortgage
Date: Tue, 22 Jun 2004 02:09:21 -0400
MIME-Version: 1.0
Content-Type: text/plain;
charset="iso-8859-1"
Content-Transfer-Encoding: 8bit
Hello,
I heard that you must be a sucker, so I thought I would send you some
unsolicited email about a mortgage. Only 3%, and you can even wire
the money directly to my friend from Nigeria. Did I mention that if
you decide to take this mortgage I'll send you the secret for making
millions by sending useless spam.
---
Mr. Jerk Spammer
"I ruin email for everyone!"This example did not venture into the topic of parsing an email
message body. As shown above, the message read with retrieveMessage( ) is a raw message with SMTP
headers containing the Subject,
Message-ID, and other important
pieces of data associated with a message. The body of a message is
separated from a list of headers by a single blank, and if you are
creating a mail client for a user, you will need to write a parser that
can extract a relevant header—such as Subject and From—from the raw email message.
This recipe used IOUtils.toString(
) and IOUtils.closeQuietly(
) to copy and close a Reader for each email message. These methods
are described in detail in Recipe 10.2 and Recipe 10.3.
For a good overview of SMTP and POP3 from the perspective of a FreeBSD administrator, take a look at Dru Lavigne's article "Understanding E-Mail" from OnLamp.com (http://www.onlamp.com/pub/a/bsd/2000/08/30/FreeBSD_Basics.html).
