In this exercise, create a program that sends an HTML message with an image attachment where the image is displayed within the HTML message.
Prerequisites
Skeleton Code
Tasks
1. The skeleton code already includes the code to get the initial mail session, create the main message, and fill its headers (to, from, subject).
|
2. Create a BodyPart for the HTML message content.
|
3. Create a text string of the HTML content. Include a reference in the HTML to an image (<img src="...">) that is local to the mail message.
|
4. Set the content of the message part. Be sure to specify the MIME type is text/html .
|
5. Create a Multipart to combine the main content with the attachment. Be sure to specify that the parts are related.
Add the main content to the multipart.
|
6. Create a second BodyPart for the attachment.
|
7. Get the attachment as a DataSource , and set the DataHandler for the message part to the data source.
|
8. Set the Content-ID header for the part to match the image reference specified in the HTML.
|
9. Add the second part of the message to the multipart, and set the content of the message to the multipart.
|
10. Send the message.
|
11. Compile and run the program, passing your SMTP server, from address, to address, and filename on the command line. This will send the images as an inline image within the HTML text.
|
12. Check if your mail reader recognizes the message as HTML and displays the image within the message, instead of as a link to an external attachment file.
|
Where help exists, the task numbers above are linked to the step-by-step help page.
|
Solution Source
Demonstration
Executing the following command (replacing the mail server, from address, to address, and an image filename) sends an HTML message with an image attachment referenced directly in the message:
java HtmlImageExample SMTP.Server from@address to@address filename
Copyright 1996-2001 jGuru.com. All Rights Reserved.
|