AurosBlog

Technical Blog

JavaMail puts messaging power at ease

The JavaMail API is a messaging framework intended to build platform-independent applications that use e-mail messaging. It is included in the J2EE platform and is available as an optional package in J2SE. The API’s main purpose is not for transporting, delivering, and forwarding messages, but rather providing protocol-independent access to mail infrastructure for sending and receiving messages.
Overview

The JavaMail API is divided into two parts:

* The main part of the API is focused on sending and receiving messages (independent of the protocol).
* The second part implements protocol-specific languages, such as SMTP, POP, IMAP, and NNTP. With the JavaMail API, in order to communicate with a server, you need a provider for a protocol. (The creation of protocol-specific providers is not covered in this article, but Sun provides a sufficient set of providers for free—at least SMTP, POP, and IMAP, which is more than enough for sending and receiving standard e-mail messages over standard Internet infrastructure.)

All versions of the JavaMail API require the JavaBeans Activation Framework, which adds support for typing arbitrary blocks of data and handling it accordingly. JavaMail uses Multipurpose Internet Mail Extensions (MIME) implementation from that framework. The MIME implementation defines the content of what is transferred in an e-mail: the format of messages, attachments, and so on. In order to use JavaMail, an application must include classpathmail.jar and activation.jar to operate properly. If you use J2EE, there is nothing special you have to do to use the basic JavaMail API—it comes with J2EE classes.

In 2006, JavaMail became open source, and the source code for the JavaMail API Reference Implementation is now available under the open source license as part of the project called GlassFish.

Classes

Here is an overview of some of the most important classes used in JavaMail applications:

  • javax.mail.Session: This class is used to control access to the implementations of the other mail classes that represent the services offered by the mail system, for instance javax.mail.Store. (Be careful not to confuse this class with javax.servlet.http.HttpSession.)
  • javax.mail.Transport: This class is used for sending mail messages via a specific protocol such as SMTP, as implemented by the service provider.
  • javax.mail.Store: This class is implemented by the service provider. It aims to allow access to read, write, monitor, and search activities for a particular mail protocol such as POP3 or IMAP4. A reference to the javax.mail.Folder class is obtained via this class.
  • javax.mail.Folder: This class gives a hierarchical view of javax.mail.Message objects and provides access to specific messages for read, delete, and reply actions.
  • javax.mail.internet.MimeMessage: This class models the actual mail message. It holds very little information and data about the message when it is first instantiated; as successive methods retrieve more data about the message, this class is used to store that data.
  • javax.mail.internet.InternetAddress: This class models a RFC822 (Internet standard) e-mail address, i.e., an address of the form peter@mikhalenko.ru. If an incorrect address format is encountered, an error occurs and an AddressException is thrown within the Java method processing the e-mail address.

Usage example

Listing A contains sample code that sends a test message. In this example, you should replace the placeholder e-mail addresses with real addresses and replace smtp.address.com with the address of a real SMTP server that is used by your Internet provider or company.

A Session object is created representing our e-mail session. The Session object’s constructor requires a java.util.Properties object representing the session’s key information. For this example, you only need to set the “mail.smtp.host” property to an appropriate SMTP host. After that, you create the actual MimeMessage object and customize it for the e-mail. Two InternetAddress objects are needed representing the “from” and “to” addresses respectively. In your code, these should be valid e-mail addresses that you can access. Once the addresses are set for the e-mail, a subject and a simple message is set.

Finally, the message is ready to be sent. In JavaMail, the Transport object handles sending messages. Since we only have one, the static send method is used. This method uses the SMTP server specified as the “mail.smtp.host” address.

Going beyond the basics

The JavaMail API has many advanced capabilities. As an example, you can add any mail headers to the message, set up multiple recipients, create blind copies of a message (what BCC is intended for), recode the message text into different charset encodings, and so on. For example, Listing B shows how to add multiple recipients.

If you send a message with a non-ASCII character (an accentuated character), the recipient may have problems reading it. To make the message easier to read, try adding the following lines to give a hint about the encoding:

MimeMessagemimeMsg = new MimeMessage(session);mimeMsg.setHeader("Content-Transfert-Encoding", "8Bit");mimeMsg.setText(frenchMsg, "iso-8859-1");Transport.send((Message)frenchMsg);

This is fine for the body, but in order to correctly display accentuated characters in a title, you need to encode it using the ‘quoted-printable’ or ‘base64′ encodings. Hopefully, you can also use the javax.mail.internet.MimeUtility class. The encodeText() method will make sure that a passed string is “mail safe.” Listing C shows how you will send an e-mail with an attachment.

cheers Aurobindo :)


AddThis Feed Button

HTML 5 aims to formalize Semantic HTML

It’s kind of hard to believe that it’s almost been a decade since HTML 4.01 was made official. However, it has taken browsers a while to recognize the standard, and this process continues today. I was pleased to see that the W3C has released the first draft of HTML 5 with its goal of creating Semantic HTML.

Semantic HTML

The impetus behind Semantic HTML is the creation of HTML documents that are void of any reference to how the HTML is presented. This means that Semantic HTML contains only the implied meaning of data via the use of the appropriate tags. This has been the goal of the separation of content and presentation, and HTML 5 is pushing it closer to reality.

A couple examples of Semantic HTML include only using the table element for tabular data, and actual header tags (,,) are used to mark important data on a page. The creation of Semantic HTML introduces a number of new features and banishes some older elements.

New additions in HTML 5
HTML 5’s new additions focus on Semantic HTML by allowing Web developers to easily assign meaning to content via HTML tags. The following list provides a sampling of these elements:

  • article: Represents an independent piece of content within a page.
  • audio: Provides multimedia support.
  • canvas: Renders dynamic bitmap graphics on the fly, such as graphs, games, and so forth.
  • dialog: Marks up a conversation.
  • embed: Provides support for plug-in content.
  • footer: Represents the footer of a section.
  • header: Represents the header of a section.
  • nav: Represents a section of a document whose purpose is navigation.
  • section: Represents a generic section of a document.
  • video: Provides multimedia support.

This subset of new elements demonstrates that presentational HTML is gone. Turn your attention to CSS when approaching the task of properly delivering content to the user community. One worry with working with a new technology is backward compatibility.

The HTML 5 standard includes two conformance requirements for Web developers and the software used to view the content (the user agent). The user agent section includes guidelines to ensure older HTML is correctly rendered, so there should be no worries about breaking existing Web content.

Out with the old
A quick perusal of the first draft of HTML 5 shows that the push towards CSS for presentation continues, as relevant elements have been removed from HTML 5. These elements include font manipulation with the standard font tag, along with basefont and big.

You will no longer be able to use the underline element for underlining text, and strike is gone as well. It is interesting to see the bold and italics elements are still in place. One element that is often overused is; beginning with HTML 5, you should use CSS to horizontally center elements.

Numerous attributes have also been removed; for instance, the align, background, and bgcolor attributes have been removed from the body element.

I know the removal of frame-related elements (frame, frameset, and noframes) will receive a resounding cheer from Web developers. The negative impact on accessibility and usability were the final straw for frames. Also, the applet tag has been removed, and the object tag is now the standard.

This is just a quick list of changes; the W3C site offers a detailed list of what has changed from HTML 4 to HTML 5.

cheers Aurobindo!!!


AddThis Feed Button

|