Recipe 19.4 Sending MIME Mail


You need to send a multipart, MIME-encoded message.


Use the Part.


Way back in the old days when the Internet was being invented, most email was composed using the seven-bit ASCII character set. You couldn't send messages containing characters from international character sets. Then some enterprising soul got the idea to convert non-ASCII files into ASCII using a form of encoding known as UUENCODE (the UU is a reference to UUCP, one of the main transport protocols used for email and file transfer at a time when Internet access was prohibitively expensive for the masses). But this was pretty cumbersome, so eventually the Multimedia Internet Mail Extensions, or MIME, was born. MIME has grown over the years to support, as its name implies, a variety of multimedia types in addition to supporting odd characters. MIME typing has become very pervasive due to its use on the Web. As you probably know, every file that your web browser downloads and a typical web page may contain from 1 to 20, 40, or more files depending on how hog-wild the graphics are is classified by the web server; this "MIME type" tells the browser how to display the contents of the file. Normal HTML pages are given a type of text/html. Plain text is, as you might guess, text/plain. Images have types such as image/gif, image/jpeg, image/png, and so on. Other types include application/ms-word, application/pdf, audio/au, etc.

Mail attachments are files attached to a mail message. MIME is used to classify attachments so that they can be deciphered by a mail reader the same way that a browser decodes files it downloads. Plain text and HTML text are the two most popular, but something called Visual Basic Script, or VBS, was popularized (along with major weaknesses in the design of a certain desktop operating system) by several famous viruses including the so-called "love bug" virus.

The point of all this? The JavaMail extension is designed to make it easy for you to send and receive all normal types of mail, including mail containing MIME-typed data. For example, if you wish to encode a stream containing audio data, you can do so. And, as importantly for Java, if you wish to encode a Reader containing characters in an 8- or 16-bit character encoding, you can do that, too.

The API makes you specify each separate MIME-encoded portion of your message as a Part . A Part represents a chunk of data that may need special handling by MIME encoders when being sent, and MIME decoders (in your email client) when being read. Example 19-5 is an example of sending a text/html attachment along with plain text.

Example 19-5. (partial listing)
/** The text/plain message body */ protected String message_body =     "I am unable to attend to your message, as I am busy sunning " +     "myself on the beach in Maui, where it is warm and peaceful. " +     "Perhaps when I return I'll get around to reading your mail. " +     "Or perhaps not."; /* The text/html data. */ protected String html_data =      "<HTML><HEAD><TITLE>My Goodness</TITLE></HEAD>" +     "<BODY><P>You <EM>do</EM> look a little " +     "<font color=green>GREEN </FONT>" +     "around the edges..." +     "</BODY></HTML>"; /** Do the work: send the mail to the SMTP server.  */ public void doSend( ) throws IOException, MessagingException {         // create a session and message as before         // Addresses, Subject set as before         // Now the message body.         Multipart mp = new MimeMultipart( );         BodyPart textPart = new MimeBodyPart( );         textPart.setText(message_body);    // sets type to "text/plain"         BodyPart pixPart = new MimeBodyPart( );         pixPart.setContent(html_data, "text/html");         // Collect the Parts into the MultiPart         mp.addBodyPart(textPart);         mp.addBodyPart(pixPart);         // Put the MultiPart into the Message         mesg.setContent(mp);                  // Finally, send the message as before         Transport.send(mesg);

Java Cookbook
Java Cookbook, Second Edition
ISBN: 0596007019
EAN: 2147483647
Year: 2003
Pages: 409
Authors: Ian F Darwin

Similar book on Amazon © 2008-2017.
If you may any questions please contact us: