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 MimePartThis 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 PartMost 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 MessagingExceptionThe addHeaderLine() method adds a string of text to the
public void setContentMD5(String md5) throws MessagingException,
IllegalWriteException, IllegalStateException
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) throwsThe getHeader( ) method returns the value of every
MessagingException, IllegalWriteException, IllegalStateException
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)The getAllHeaderLines() method returns a
throws MessagingException
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 MessagingExceptionThe 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)The getEncoding( ) method returns
throws MessagingException
public Enumeration getNonMatchingHeaderLines(String[] names)
throws MessagingException
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 MessagingExceptionThe 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 MessagingExceptionThe getContentLanguage() method returns the value of the
IllegalWriteException, IllegalStateException
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 MessagingExceptionThere's also a setContentLanguage() method that you might use when
sending a message:
public void setContentLanguage(String[] languages) throwsFinally, the two setText() methods set the content of the part
MessagingException, IllegalWriteException, IllegalStateException
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