Java Mail API

1. E-MAIL

An electronic mail (or simply email or e-mail), as its name suggests, is a digital version of a message (or letter). This concept was introduced much earlier than evolution of the Internet. Needless to say that the email system was very simple at that time. It was as simple as leaving a message on someone’s desk. It allowed a user only to place a message in another user’s directory where later one could see the message after logging in.

The current email system is much more powerful. It allows two parties to exchange emails across networks using addresses called email addresses. It does not even require the sender and recipient to both be online at the same time. It has, no doubt, become one of the most popular applications of the Internet.

A set of protocols is used for delivery and accessing of such emails. Simple Mail Transfer Protocol (SMTP) is used to deliver emails, whereas Post Office Protocol (POP) and Internet Message Access Protocol (IMAP) are used to access emails.

2. JAVAMAIL API

JavaMail is a powerful API that provides a platform-independent framework to send and retrieve electronic-mail (e-mail). Originally, the specification does not mandate support for a specific protocol. However, JavaMail includes support for some well-known protocols such as SMTP, POP and IMAP.

The package javax.maii contains classes that encapsulate the different components of the email system. Initially, the detailed layout of these classes and interfaces may wrongly cause you to believe that you are in big trouble. However, once you start working, you will certainly discover that it is a very simple and handy API for incorporating robust mail/messaging functionality in your applications.

The package com.sun.maii contains implementation classes for mail protocols such as SMTP, POP and IMAP. Note that JavaMail itself does not implement an email server. It helps us in accessing an existing email server. Since, almost all users have an account in the Gmail server, we shall use it to test the code present in this chapter.

Note that sending email using the Java application has various practical use as follows:

  • When a user creates an account, an email, containing critical information to be used by the user later, may be sent automatically at the end of account creation.
  • In the middle of an account creation, some web application may send an activation code to the specified email address to verify if the user really owns that email address.
  • In case a user forgets the password, an email containing the password may be sent to the user’s email address automatically.
  • For important updates, emails may be sent automatically to the relevant users.

3. INSTALLING JAVAMAIL API

Java 2 Platform, Enterprise Edition (J2EE) 1.3 includes JavaMail API. So, if you have this version installed in your computer, you need not download JavaMail API. However, for other versions of Java, you have to download a jar (java archive) file that contains the necessary classes and interfaces. During the development of this manuscript, the version of JavaMail API is 1.5.0. We downloaded the jar file javax.mail.jar from https://java.net/projects/javamail/pages/Home.

To use this API, include this jar file in you classpath environment variable. If you do not have the right to change this environment variable, specify this jar file (using -cp option) during compilation and execution. If you still can’t understand, then follow the instruction given in the ‘Running the example’ sections.

If you are not using Java SE 6 or newer, you will also need an extension called Java Activation Framework (JAF). Being a kind of plug-in to Java, it basically helps us to handle multitude data formats such as simple text or extremely complex documents composed of images, audio, video, and even live objects. The JAF is provided as a separate package javax.activation. The JavaMail API Design Specification (Version 1.5) can be found in the following URL:

http://javamail.java.net/nonav/docs/JavaMail-1.5.pdf

Source: Uttam Kumar Roy (2015), Advanced Java programming, Oxford University Press.

Leave a Reply

Your email address will not be published. Required fields are marked *