Java Reference
In-Depth Information
Example 7-1 is a simple example program that follows the basic pattern of the last
several mail-reading programs. However, this one no longer uses writeTo() . Instead,
it uses the methods in this section to print just the headers. Furthermore, it prints them
in a particular order regardless of their order in the actual message on the server. Finally,
it ignores the less important headers such as X-UIDL: and Status:. The static
InternetAddress.toString() method converts the arrays that most of these methods
return into simple, comma-separated strings.
Example 7-1. A program to read mail headers
import javax.mail.* ;
import javax.mail.internet.* ;
import java.util.* ;
public class HeaderClient {
public static void main ( String [] args ) {
if ( args . length == 0 ) {
System . err . println (
"Usage: java HeaderClient protocol://username@host/foldername" );
return ;
}
URLName server = new URLName ( args [ 0 ]);
try {
Session session = Session . getInstance ( new Properties (),
new MailAuthenticator ( server . getUsername ()));
// Connect to the server and open the folder
Folder folder = session . getFolder ( server );
if ( folder == null ) {
System . out . println ( "Folder " + server . getFile () + " not found." );
System . exit ( 1 );
}
folder . open ( Folder . READ_ONLY );
// Get the messages from the server
Message [] messages = folder . getMessages ();
for ( int i = 0 ; i < messages . length ; i ++) {
System . out . println ( "------------ Message " + ( i + 1 )
+ " ------------" );
// Here's the big change...
String from = InternetAddress . toString ( messages [ i ]. getFrom ());
if ( from != null ) System . out . println ( "From: " + from );
String replyTo = InternetAddress . toString (
messages [ i ]. getReplyTo ());
if ( replyTo != null ) System . out . println ( "Reply-to: "
+ replyTo );
String to = InternetAddress . toString (
messages [ i ]. getRecipients ( Message . RecipientType . TO ));
Search WWH ::




Custom Search