Saturday, January 10, 2009

JavaMail API Sending HTML Messages with Images

JavaMail API Sending HTML Messages with Images

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 () 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.

import java.util.Properties;

import javax.mail.*;

import javax.mail.internet.*;

import javax.activation.*;

public class HtmlImageExample {

public static void main (String args[]) throws Exception {

String host = args[0];

String from = args[1];

String to = args[2];

String file = args[3];

// Get system properties

Properties props = System.getProperties();

// Setup mail server

props.put("mail.smtp.host", host);

// Get session

Session session = Session.getDefaultInstance(props, null);

// Create the message

Message message = new MimeMessage(session);

// Fill its headers

message.setSubject("Embedded Image");

message.setFrom(new InternetAddress(from));

message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));

// Create your new message part

BodyPart messageBodyPart = new MimeBodyPart();

// Set the HTML content, be sure it references the attachment

String htmlText = "

Hello

" +

"";

// Set the content of the body part

messageBodyPart.setContent(htmlText, "text/html");

// Create a related multi-part to combine the parts

MimeMultipart multipart = new MimeMultipart("related");

// Add body part to multipart

multipart.addBodyPart(messageBodyPart);

// Create part for the image

messageBodyPart = new MimeBodyPart();

// Fetch the image and associate to part

DataSource fds = new FileDataSource(file);

messageBodyPart.setDataHandler(new DataHandler(fds));

// Add a header to connect to the HTML

messageBodyPart.setHeader("Content-ID","");

// Add part to multi-part

multipart.addBodyPart(messageBodyPart);

// Associate multi-part with message

message.setContent(multipart);

// Send message

Transport.send(message);

}

}

Solution Source....

logo.gif

HtmlImageExample.java

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




*****************************************************************

No comments:

Post a Comment