Overview
This article will demonstrate how using Secure iNet Factory components you can compose an email message that can be displayed by both HTML capable and plain-text only email clients.
To see what else Secure iNet Factory has to offer Download a FREE 30 day Secure iNet Factory Evaluation.
Overview of HTML email
The use of HTML is becoming an increasingly popular format for the communication of messages via email. Largely contributing to the success of HTML based messages is that a majority of users now use HTML capable email. clients. Such popular email clients include Microsoft Outlook, Netscape Communicator and Qualcomm Eudora just to name a few.
Benefits of HTML email
Unlike its plain text counterpart, an HTML email message can include elements such as images, hyperlinks and even web forms. This is a clear benefit to those developing collaborative applications that rely on email.
Problems sending HTML email
Although many users have HTML capable email clients, there are still a large number of users that do not. This for the most part includes a number of universities and large corporations that still rely on text-based email clients. Without support for HTML in the email client how can you ensure that the recipient of an HTML email message can read it in its intended form? To begin let's start with the basic format of email.
Plain-text email
To: recipient@domain.com
From: sender@domain.com
Subject: Plain Text email.
Content-Type: text/plain; charset="us-ascii"
This is plain text
The example above shows a message in plain text format Notice the Content-Type header. The header indicates to an email client the contents of this message are in plain text (text/plain) and uses the US ASCII (us-ascii) character set.
HTML email
To: recipient@domain.com
From: sender@domain.com
Subject: HTML email.
Content-Type: text/html; charset="us-ascii"
<b>This is HTML text</b>
The example above shows a message in HTML format. Notice again the Content-Type header. The header indicates to an email client that the contents of this message are in HTML (text/html) and uses the US ASCII (us-ascii) character set. Also notice the opening and closing <b> HTML tags used in the body of the message.
An HTML capable email client reading this message would know that based on the Content-Type header, to display any HTML markup within the body of the message as rendered HTML. Had the Content-Type header used a value of text/plain instead of text/html then the mail client would have simply displayed the message including any HTML source (non-rendered). Similarly, had a plain-text only client read this message it would have displayed the message including any HTML source. To solve this problem lets look at creating a multipart email.
Multipart email
Date: Tue, 01 Jul 2003 09:48:30 -0700
MIME-Version: 1.0
Message-ID: <2.4.1057078110032@localhost>
Content-Transfer-Encoding: quoted-printable
Content-Type: multipart/mixed; charset=us-ascii;
boundary=___BoUnDaRy_1057078110032.797.291273273055
From: sender@domain.com
To: recipient@domain.com
Subject: multipart mixed email.
--___Boundary_1057078110032.797.291273273055
Content-Type: multipart/alternative;
boundary=___Boundary_1057078110032.355.65337159541633
--___Boundary_1057078110032.355.65337159541633
Content-Type: text/plain
This is plain text
--___Boundary_1057078110032.355.65337159541633
Content-Type: text/html
<b>This is HTML text</b>
--___Boundary_1057078110032.355.65337159541633--
--___Boundary_1057078110032.797.291273273055--
The above is an example of a multipart email. Notice the first Content-Type header. This header indicates to an email client that the message has multiple parts. The second Content-Type header indicates that the message parts are varied in format (multipart/alternative). Given the multipart/alternative Content-Type header, the email client will display the format most suited to its environment, HTML for HTML capable clients, plain-text for text only clients or a suitable error message for email clients that are incapable of reading MIME messages (very unlikely). This format is clearly a solution to our problem as it provides formats which can be viewed on virtually any mail client within a single email message.
The following code snippet demonstrates how to create a multipart email like the one described above.
//import the necessary classes
import com.jscape.inet.smtp.Smtp;
import com.jscape.inet.email.EmailMessage;
import com.jscape.inet.mime.MimeMessage;
...
String from = "sender@domain.com";
String to = "recipient@domain.com";
String subject = "multipart mixed email.";
String plainText = "This is plain text";
String htmlText = "<b>This is HTML text</b>";
try {
EmailMessage email = new EmailMessage();
email.setContentType("multipart/mixed");
// create email message
email.setFrom(from);
email.setTo(to);
email.setSubject(subject);
MimeMessage contents = new MimeMessage();
contents.setContentType("multipart/alternative");
try {
MimeMessage plainMessage = new MimeMessage();
plainMessage.setContentType("text/plain");
plainMessage.setBody(plainText);
contents.addPart(plainMessage);
} catch (Exception e) {
System.out.println(e);
}
try {
MimeMessage htmlMessage = new MimeMessage();
htmlMessage.setContentType("text/html");
htmlMessage.setBody(htmlText);
contents.addPart(htmlMessage);
} catch (Exception e) {
System.out.println(e);
}
email.addPart(contents);
Smtp smtp = new Smtp("smtp.west.cox.net");
smtp.setDebug(true);
smtp.connect();
smtp.send(email);
smtp.disconnect();
}
catch (Exception e) {
e.printStackTrace();
}
Download the full source code here: SmtpMultipartTutorial.java
Summary
In this article you learned how to send HTML based email that can be displayed by both HTML and plain-text only email. clients. The SMTP and MIME components in Secure iNet Factory make this easy removing the complexities of the SMTP and MIME protocols.
References
Smtp Class Overview
Smtp Class Javadoc
MimeMessage Class Javadoc
MIME Wiki
Comments