Reading Email

Problem

You need to read mail.

Solution

Use a JavaMail Store.

Discussion

The JavaMail API is designed to be easy to use. Store encapsulates the information and access methods for a particular type of mail storage; the steps for using it are listed in the sidebar.

Sun provides a Store class for the IMAP transport mechanism, and optionally for POP3.[44] In these examples I use the Unix mbox protocol[45] (when I started with Unix there was no POP3 protocol; it was traditional to access your mail spool file directly on a server). However, you could use all these programs with the POP or IMAP stores just by passing the appropriate protocol name where “mbox” appears in the following examples. I’ve tested several of the programs using Sun’s POP store and several POP servers (CUCIpop and PMDF).

I delete most of the email I get on one of my systems, so there were only two messages to be read when I ran my first "mailbox lister” program:

daroad.darwinsys.com$ java MailLister mbox localhost ...

Get Java Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.