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
mail.smtp.host=smtp.thexyz.com
# You get lots of debugging information if
# this is turned on. Comment this line to turn
# it off.
mail.debug=true
# The To email address
mymail.to=subscribers@thexyz.com
# The From email address
mymailfrom.from=geek@thegeek.com
# 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
mymail.txtfile=mymail.txt
# The name of the HTML file to send
# This file is included in the distribution
mymail.htmlfile=mymail.html
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
("mymail.from"));
InternetAddress to =
new InternetAddress((String)props.get
("mymail.to"));
msg.setFrom(from);
msg.addRecipient(Message.RecipientType.TO, to);
msg.setSubject((String)props.get
("mymailsubject.subject"));
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
("mymail.txtfile");
BodyPart bp1 = getFileBodyPart
(textfile, "text/plain");
mp.addBodyPart(bp1);
The following code does the same thing for the HTML body part:
// Do the same with the HTML part
String htmlfile = (String)props.get
("mymail.htmlfile");
BodyPart bp2 = getFileBodyPart
(htmlfile, "text/html");
mp.addBodyPart(bp2);
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
msg.setContent(mp);
Transport.send(msg);
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.
0 comments:
Post a Comment