Sending HTML based email using Java
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.
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.
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.
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.
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.
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.
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.
01 String from = "sender@domain.com";
02 String to = "recipient@domain.com";
03 String subject = "multipart mixed email.";
04 String plainText = "This is plain text";
05 String htmlText = "<b>This is HTML text</b>";
06
07 try {
08 EmailMessage email. = new EmailMessage();
09 email.setContentType("multipart/mixed");
10 // create email. message
11 email.setFrom(from);
12 email.setTo(to);
13 email.setSubject(subject);
14
15 MimeMessage contents = new MimeMessage();
16 contents.setContentType("multipart/alternative");
17
18 try {
19 MimeMessage plainMessage = new MimeMessage();
20 plainMessage.setContentType("text/plain");
21 plainMessage.setBody(plainText);
22 contents.addPart(plainMessage);
23 } catch (Exception e) {
24 System.out.println(e);
25 }
26
27 try {
28 MimeMessage htmlMessage = new MimeMessage();
29 htmlMessage.setContentType("text/html");
30 htmlMessage.setBody(htmlText);
31 contents.addPart(htmlMessage);
32 } catch (Exception e) {
33 System.out.println(e);
34 }
35 email.addPart(contents);
36
37 Smtp smtp = new Smtp("smtp.west.cox.net");
38 smtp.setDebug(true);
39 smtp.connect();
40 smtp.send(email.);
41 smtp.disconnect();
The code example above demonstrates how to create
the multipart email. shown. The process for creating this message
is as follows:
- Ensure that the following packages are imported in your Java
code. com.jscape.inet.email.*; com.jscape.inet.mime.*; com.jscape.inet.smtp.*;
- Line 8. Create an EmailMessage instance and set its Content-Type
header to multipart/mixed.
- Line 15. Create a MimeMessage instance that will contain both
the plain text and HTML parts of the message.
- Line 19. Create a MimeMessage instance representing the plain
text part of the message and add to the MimeMessage instance
created in Line 15.
- Line 28. Create a MimeMessage instance representing the HTML
part of the message and add to the MimeMessage instance created
in Line 15.
- Line 35. Add the MimeMessage instance created in Line 15 to
the EmailMessage instance created in Line 8.
- Line 37. Establish connection to SMTP server and send email.
message.
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. To see what else
Secure iNet Factory has to offer Download a
FREE 30 day Secure iNet Factory Evaluation.
|