Sun Java Solaris Communities My SDN Account Join SDN
 
Tutorials

jGuru: Fundamentals of the JavaMail API

 


[Help | API Docs | Short Course| Exercises]

In this exercise, create a program that displays the from address and subject for each message and prompts to display the message content.

Prerequisites

Skeleton Code

Tasks

1. Starting with the skeleton code, get or create a Properties object.
2. Get a Session object based on the Properties.
3. Get a Store for your email protocol, either pop3 or imap.
4. Connect to your mail host's store with the appropriate username and password.
5. Get the folder you want to read. More than likely, this will be the INBOX.
6. Open the folder read-only.
7. Get a directory of the messages in the folder. Save the message list in an array variable named message.
8. For each message, display the from field and the subject.
9. Display the message content when prompted.
10. Close the connection to the folder and store.
11. Compile and run the program, passing your mail server, username, and password on the command line. Answer YES to the messages you want to read. Just hit ENTER if you don't. If you want to stop reading your mail before making your way through all the messages, enter QUIT.

Where help exists, the task numbers above are linked to the step-by-step help page.

Solution Source

Demonstration

After executing the following command (replacing the mail server, username, and password):

java GetMessageExample POP.Server username password

You'll be prompted to read the messages in your INBOX. Enter YES to see the message content.

0: president@whitehouse.gov     Thanks.
Do you want to read message? [YES to read/QUIT to end]
YES
Blah Blah Blah
...


1: billg@microsoft.com     No Thanks.
Do you want to read message? [YES to read/QUIT to end]
YES
Blah Blah Blah Blah
...

Next Exercise

Exercises

About This Short Course

Copyright 1996-2001 jGuru.com. All Rights Reserved.