Java and .NET components, FTP, TELNET, SMTP, POP3, IMAP, HTTP, SSH
Home Search Knowledge Base Support

Support

Click here for access to live sales support.

For technical support please submit a ticket to the Help Desk.

 

Java and .NET Help

iPod shuffle offer

For a limited time get the newly re-designed iPod shuffle free with qualifying purchase.

Click for details.


News

Secure iNet Factory 8.0 Released
04/04/2008 02:24 PM

Updates to Email Factory for .NET and Secure iNet Factory
03/29/2008 04:06 PM

JSCAPE Secure FTP Server 3.9 Preview
03/14/2008 12:19 PM

AnyClient Service and Application Launched
03/12/2008 03:41 PM

JSCAPE Secure FTP Server 3.8 Released
02/12/2008 10:50 AM


Tutorials

Email Validation with Java
04/15/2008 02:04 PM

Sending HTML Based Email Using Java
03/11/2008 02:47 PM

Secure FTP Using Java and FTPS (FTP over SSL)
03/10/2008 04:08 PM

FTP Directory Listing Using Java
03/10/2008 03:57 PM

Sending Email Using Java
03/09/2008 03:43 PM

SSH Using Java
03/09/2008 02:53 PM


Articles

DMZ File Transfer Streaming
03/28/2008 11:57 AM

Phishing looks to FTP to distribute malware
03/13/2008 05:14 PM

Ad Hoc File Transfer Explained
03/13/2008 09:16 AM

Password Policies Made Easy
03/12/2008 03:03 PM


Feedback

Request a feature or component

Request a Java or .NET component


 

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.

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.

Example

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:

  1. Ensure that the following packages are imported in your Java code. com.jscape.inet.email.*; com.jscape.inet.mime.*; com.jscape.inet.smtp.*;
  2. Line 8. Create an EmailMessage instance and set its Content-Type header to multipart/mixed.
  3. Line 15. Create a MimeMessage instance that will contain both the plain text and HTML parts of the message.
  4. Line 19. Create a MimeMessage instance representing the plain text part of the message and add to the MimeMessage instance created in Line 15.
  5. Line 28. Create a MimeMessage instance representing the HTML part of the message and add to the MimeMessage instance created in Line 15.
  6. Line 35. Add the MimeMessage instance created in Line 15 to the EmailMessage instance created in Line 8.
  7. Line 37. Establish connection to SMTP server and send email. message.

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. To see what else Secure iNet Factory has to offer Download a FREE 30 day Secure iNet Factory Evaluation.