Sending MIME Email


String html =     "<html><head><title>Java Mail</title></head>" +     "<body>Some HTML content.</body></html>"; Multipart mp = new MimeMultipart(); BodyPart textPart = new MimeBodyPart( ); textPart.setText("This is the message body."); BodyPart htmlPart = new MimeBodyPart( ); htmlPart.setContent(html, "text/html"); mp.addBodyPart(textPart); mp.addBodyPart(htmlPart); msg.setContent(mp); Transport.send(msg);



MIME stands for Multimedia Internet Mail Extensions. MIME is supported by all major email clients and is the standard way of including attachments to messages. MIME allows you to attach a variety of media types, such as images, video, and .pdf files to an email message. The JavaMail API also supports MIME messages, and it is nearly as easy to create a message with attachments as a MIME message as it is to create a standard plaintext message.

In this phrase, we create and send a MIME message containing a plaintext body and an HTML attachment. To create a multipart message, we use the MultiPart class, which is a part of the javax.mail package. The MimeMultiPart class, in the javax.mail.internet package, provides a concrete implementation of the MultiPart abstract class and uses MIME conventions for the multipart data. The MimeMultiPart class allows us to add multiple body parts, represented as MimeBodyPart objects. We set a body part's content using the setText() method for plaintext body parts or the setContent() method for other types of body parts. When we use the setContent() method, we pass an object that holds the body part, along with a string specifying the MIME type we are adding. In our phrase, we add an HTML body part, so we specify the MIME type as text/html.

The code in shown in the phrase focusing on the MIME specific parts of sending a MIME message. The example below is a more complete example of sending a MIME email message including all steps necessary to accomplish the task:

Properties props = new Properties( ); props.put("mail.smtp.host", "mail.yourhost.com"); Session session =    Session.getDefaultInstance(props, null); Message msg = new MimeMessage(session); msg.setFrom(new    InternetAddress("tim@timothyfisher.com")); InternetAddress toAddress =    new InternetAddress("kerry@timothyfisher.com"); msg.addRecipient(Message.RecipientType.TO,                   toAddress); msg.setSubject("Test Message"); String html =     "<html><head><title>Java Mail</title></head>" +     "<body>Some HTML content.</body></html>"; Multipart mp = new MimeMultipart(); BodyPart textPart = new MimeBodyPart( ); textPart.setText("This is the message body."); BodyPart htmlPart = new MimeBodyPart( ); htmlPart.setContent(html, "text/html"); mp.addBodyPart(textPart); mp.addBodyPart(htmlPart); msg.setContent(mp); Transport.send(msg);


Note

The Internet Assigned Numbers Authority (IANA) provides a good reference of all the standard MIME media types on their website. The site also provides an application for registering a new MIME type. If you don't feel that any existing MIME types are suitable for your content, you can use this application to request a new standard MIME media type be created that supports your content type. The IANA website is at: http://www.iana.org

The MIME media types can be found on the IANA site at: http://www.iana.org/assignments/media-types/





JavaT Phrasebook. Essential Code and Commands
Java Phrasebook
ISBN: 0672329077
EAN: 2147483647
Year: 2004
Pages: 166

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