Java Reference
In-Depth Information
The setter methods all throw a MessagingException if there's some problem while
changing the message. They can also throw an IllegalWriteException if the relevant
attribute of the part cannot be modified or an IllegalStateException if the part be‐
longs to a read-only folder.
The setDisposition() method determines whether the part is to be viewed in-line or
as an attachment. Although it's declared to take a String as an argument, this String
should be one of the two named constants, Part.INLINE or Part.ATTACHMENT . Parts
that are attachments generally have a filename included in their metainformation. This
name can be set with the setFileName() method. Finally, the setDescription()
method can take any String at all to add a description to the part.
Example 7-3 is a simple program that connects to a mail server and reads the attributes
of the messages in the mailbox. Since each message is itself a part (even if it contains
other parts), we can invoke these methods on the entire message.
Example 7-3. A program to read mail attributes
import javax.mail.* ;
import javax.mail.internet.* ;
import java.util.* ;
public class AttributeClient {
public static void main ( String [] args ) {
if ( args . length == 0 ) {
System . err . println (
"Usage: java AttributeClient 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 )
+ " ------------" );
String from = InternetAddress . toString ( messages [ i ]. getFrom ());
Search WWH ::




Custom Search