« Secure FTP Using Java and FTPS (FTP over SSL) | Main | Password Policies Made Easy »

March 11, 2008

Sending HTML Based Email Using Java

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.

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 and Thunderbird just to name a couple.

Benefits

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

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 Example

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 Example

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 Example

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.

Code Example

01 import com.jscape.inet.smtp.*;
02 import com.jscape.inet.email.*;
03 import com.jscape.inet.mime.MimeException;
04 import java.net.*;
05 import java.io.*;
06
07 public class SmtpHtmlExample extends SmtpAdapter {
08  public void sendMessage(String hostname, String to, String from, String subject, String textbody, String htmlbody)
09  throws SmtpException, MimeException, MalformedURLException {
10  // create instance and add listener
11  Smtp smtp = new Smtp(hostname);
12  smtp.addSmtpListener(this);
13  smtp.setDebug(true);
14  
15  // connect to mail server
16  smtp.connect();
17  
18  // create email message
19  HtmlEmailMessage email = new HtmlEmailMessage()
20  email.setTo(to);
21  email.setFrom(from);
22  email.setSubject(subject);
23  email.setTextBody(textbody);
24  email.setHtmlBody(htmlbody);
25  
26  // send email message
27  smtp.send(email);
28  
29  // disconnect from server
30  smtp.disconnect();
31  }
32  
33  // capture connect event
34  public void connected(SmtpConnectedEvent evt) {
35  System.out.println("Connected to SMTP server: " + evt.getHostname());
36  }
37  
38  // capture disconnect event
39  public void disconnected(SmtpDisconnectedEvent evt) {
40  System.out.println("Disconnected from SMTP server: " + evt.getHostname());
41  }
42  
43  public static void main(String[] args) {
44  String hostname;
45  String to;
46  String from;
47  String subject;
48  String textbody;
49  String htmlbody;
50  try {
51  BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
52  System.out.print("Enter Smtp hostname (e.g. mail.domain.com): ");
53  hostname = reader.readLine();
54  System.out.print("Enter To address (e.g. recipient@domain.com): ");
55  to = reader.readLine();
56  System.out.print("Enter From address (e.g. sender@domain.com): ");
57  from = reader.readLine();            
58  subject = "Secure iNet Factory HTML Example";
59  textbody = "Plain-text message body.";
60  htmlbody = "<b>HTML message body.</b>";
61  SmtpHtmlExample example = new SmtpHtmlExample();
62  example.sendMessage(hostname,to,from,subject,textbody,htmlbody);
63  }
64  catch(Exception e) {
65  e.printStackTrace();
66  }
67  }
68 }

  • Lines 1-5.  Add necessary import statements.
  • Lines 11-16. Create new Smtp instance and connect.
  • Line 19.  Create new HtmlEmailMessage instance.
  • Lines 20-24.  Set to, from, subject and both text and HTML message bodies.
  • Lines 27-30.  Send email message and disconnect.

TrackBack

TrackBack URL for this entry:
http://www.typepad.com/t/trackback/2925084/27556282

Listed below are links to weblogs that reference Sending HTML Based Email Using Java:

Comments

Feed You can follow this conversation by subscribing to the comment feed for this post.

Post a comment

Comments are moderated, and will not appear on this weblog until the author has approved them.

If you have a TypeKey or TypePad account, please Sign In