JavaMail--Emails let you send both an HTML version and a text version in the same email


In the old days of the Internet, when it was called Arpanet, you could send and receive any kind of email you liked -- as long as it was plain text. Today people have enormously more computing power at their disposal than in the days of Arpanet, yet for the most part, the primary type of email is still text.

Text email is excellent for interpersonal communication, but many organizations can benefit from being able to email other types of content. For example, a magazine publisher might want to email the electronic content of a magazine in HTML. However there are problems sending email content in HTML. Some Web-based email systems show email in text-only mode, and for the large number of recipients who still read email using text-only clients, receiving HTML email can be annoying.

The below section describes how to use the JavaMail API to send email that can be displayed as text or as HTML, depending on the recipient's email client. The tip does not discuss the many, valuable, and important arguments for and against HTML email. Instead, it assumes you've decided to use HTML email, and want also to support text email readers.

The sample code for this tip sends a multipart/alternative email message to a specified email address. The application transmits email using configuration information that is defined in a java.util.Properties file

Below is how the property file looks like.

# The hostname or IP address of your SMTP server

# You get lots of debugging information if
# this is turned on. Comment this line to turn
# it off.

# The To email address

# The From email address

# The subject of the email
mymailsubject.subject=The Geek Head Blog Rocks

# The name of the TEXT file to send
# This file is included in the distribution

# The name of the HTML file to send
# This file is included in the distribution

Change the hostname and IP address of the SMTP server in the Properties file as appropriate for your environment. Also change the debug and to lines as needed.

The program creates an email containing both files, and sends it to the "to" address. Check that email with one reader that supports HTML, and you'll see a formatted message. Check with a text-only email client, and you'll see text.

Here is how the sample code . In class TwoKindsOfEmail, method sendmail() does virtually all of the work. The first part of the code loads the properties file, and creates and initializes a new MimeMessage:

Properties props = new Properties();
props.load(new FileInputStream(propfile));

Session session = Session.getInstance(props, null);
MimeMessage msg = new MimeMessage(session);

InternetAddress from =
new InternetAddress((String)props.get

InternetAddress to =
new InternetAddress((String)props.get

msg.addRecipient(Message.RecipientType.TO, to);

The next line creates a multipart message. Note that the multipart message is explicitly marked as "alternative". Without this string, you would see both the HTML and text emails in your email browser:

// Create an "Alternative" Multipart message
Multipart mp = new MimeMultipart("alternative");

The next section of code loads the text file into memory. It creates a java.mail.BodyPart object with the text file content, and a Content-type of "text/plain". (It uses method getFileBodyPart(), which simply reads a file's contents, constructs a BodyPart object, and sets its content and content-type.)

// Read text file, load it into a BodyPart, and add it to the
// message.
String textfile = (String)props.get
BodyPart bp1 = getFileBodyPart
(textfile, "text/plain");

The following code does the same thing for the HTML body part:

// Do the same with the HTML part
String htmlfile = (String)props.get
BodyPart bp2 = getFileBodyPart
(htmlfile, "text/html");

Finally, the method sets the message content to the multipart object, and tells the Transport object to send it:

// Set the content for the message and transmit

Note that the text part is set first. The MIME definition for multipart/alternative recommends ordering the alternatives from the least sophisticated to the most sophisticated.


:)) ;)) ;;) :D ;) :p :(( :) :( :X =(( :-o :-/ :-* :| 8-} :)] ~x( :-t b-( :-L x( =))

Post a Comment