19.10 MIME Messages

     

MIME was designed mainly for Internet email and specifically organized to be backward-compatible with existing protocols and software. Therefore, a typical Internet email message is in fact a MIME message. The only concrete subclass of Message in the JavaMail API is javax.mail.internet.MimeMessage :

 public class MimeMessage extends Message implements MimePart 

This class declares almost 70 public and protected methods. However, with the natural exception of the constructors, almost all of these either override methods from the Message superclass or implement methods declared by the Part interface. The only new methods are a baker's dozen declared in the MimePart interface, a subinterface of Part :

 public interface MimePart extends Part 

Most of these methods are very similar to methods in Part or Message . However, they have features that are unlikely to be found in non-MIME messages. For instance, a MIME part may have an MD5 digest, which would be encoded as an extra header inside the part. Thus, the MimePart interface declares and the MimeMessage class implements two methods to set and get this digest:

 public String getContentMD5( ) throws MessagingException public void   setContentMD5(String md5) throws MessagingException,   IllegalWriteException, IllegalStateException 

The addHeaderLine() method adds a string of text to the header of the message. It's up to you to make sure that this string will actually make sense in the header:

 public void addHeaderLine(String line) throws   MessagingException, IllegalWriteException, IllegalStateException 

The getHeader( ) method returns the value of every header in the message with the given name. If there are multiple headers with this name , the string separates the values of the different headers with the specified delimiter string:

 public String getHeader(String name, String delimiter)   throws MessagingException 

The getAllHeaderLines() method returns a java.util.Enumeration containing every header in the message. The Enumeration contains String objects, one per header. Each String contains the full name and value; for example, "Subject: Re: Java 5 support". It is not divided into a separate name and value:

 public Enumeration getAllHeaderLines( ) throws MessagingException 

The getMatchingHeaderLines() method returns all header lines with names given in the names argument array. The getNonMatchingHeaderLines( ) method does the reverse; it returns the header lines with a name not mentioned in the names argument:

 public Enumeration getMatchingHeaderLines(String[] names)   throws MessagingException public Enumeration getNonMatchingHeaderLines(String[] names)   throws MessagingException 

The getEncoding( ) method returns the encoding of this MIME part as a String as given by the Content-transfer-encoding: header. The typical encoding for a plain-text email is seven-bit or perhaps eight-bit or quoted-printable. The typical encoding for a file attachment is Base64:

 public String getEncoding( ) throws MessagingException 

The getContentID() method returns a string that uniquely identifies this part as given by the part's Content-ID: field. A typical ID might look like <Pine.LNX.4. 10.9912290930220.8058@akbar.nevex.com> . It returns null if the part doesn't have a content ID:

 public String getContentID( ) throws MessagingException  IllegalWriteException, IllegalStateException 

The getContentLanguage() method returns the value of the Content-language: header. This is a comma-separated list of two (or more) letter abbreviations for languages, as defined by RFC 1766. For example, English is "en" and French is "fr". It returns null if the part doesn't have a Content-language: header.

 public String[] getContentLanguage( ) throws MessagingException 

There's also a setContentLanguage() method that you might use when sending a message:

 public void setContentLanguage(String[] languages) throws   MessagingException, IllegalWriteException, IllegalStateException 

Finally, the two setText() methods set the content of the part with the MIME type text/plain . The second setText( ) method also lets you specify the character setfor example, us-ascii or ISO 8859-1:

 public void setText(String text) throws MessagingException public void setText(String text, String charset)   throws MessagingException 



Java Network Programming
Java Network Programming, Third Edition
ISBN: 0596007213
EAN: 2147483647
Year: 2003
Pages: 164

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net