O'Reilly logo

Java Examples in a Nutshell, 3rd Edition by David Flanagan

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

A POP Client

Example 5-7 is a useful network client that connects to a POP3 mailbox and lists or deletes messages there based on their size and optionally their Subject lines. I wrote this program in response to a particularly virulent Internet virus that was sending hundreds of infected messages each day. At the peak of the viral outbreak, my mailbox was filling up with 50 megabytes of junk overnight. This program allowed me to delete the bogus messages without having to download them first.

The networking code in this example is straightforward, but the example is valuable because it is the most real-world one we've seen so far. The details of the POP3 protocol encoded in the example are also quite interesting. Please note that this example deletes messages from your mailbox—be sure you understand exactly what it does before trying to use it. One new feature demonstrated by this example is the Java 1.4 regular expression matching capability of the java.util.regex package. Look up the Pattern and Matcher classes of that package for details. Regular expressions are defined as part of the New I/O API, and we'll see more examples of their use in Chapter 6.

Example 5-7. PopClean.java

package je3.net; import java.io.*; import java.net.*; import java.util.*; import java.util.regex.*; /** * A simple utility program for deleting messages from a POP3 mailbox based on * message size and Subject line. Don't run this program unless you understand * what it is doing. It deletes e-mail without ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required