I have a project set up in Netbeans IDE with JDK 1.8 and Java EE8 which includes GlassFish Server 5.1. I want to add email capability to this simple app, what are my options? The JavaMail API, a free and open-source package, makes it easy to add mail-handling capability to any application – in my case, a JavaServer Faces web app. The API includes convenience classes which encapsulate common mail functions and protocols. Let’s look at the most used classes/packages of the JavaMail API:
The Session Class
The Session class, a final concrete class, represents a mail session. It sets and gets global and per-user mail-related properties that define the interface between a mail-enabled client and the network. The Session also authenticates the user and initiates the message transport and store services. Transport services are responsible for accepting a message and sending it to specified recipient while store services are used to manipulate storage of emails. The Internet mail service mostly uses SMTP to implement transport services and IMAP to implement store services. As we’re learning how to send emails from an application here, we’ll focus on the transport services.
You can read more about the Session class here.
The Message Class
The Message class is an abstract class that defines a set of attributes and content for a mail message. Attributes of the Message class specify addressing information (From, To, Subject, Reply-To) and define the structure of the content, including the content type. The Message class implements the Part interface. The Part interface defines attributes that are required to define and format data content carried by a Message object, and to interface successfully to a mail system.
Message Composition and Transport
A client creates a new message by instantiating an appropriate Message subclass such as MimeMessage or MimeBodyPart. It sets attributes like the recipient addresses and the subject and inserts the content into the Message object. Finally, it sends the Message by invoking the Transport.send method.
We compose our message using MimeMessage class which represents a MIME style email message. MIME (Multipurpose Internet Mail Extensions) refers to a standard for describing email messages that are sent across the Internet. You can read more about Class MimeMessage.
The Transport class models the transport agent that routes a message to its destination addresses. This class provides methods that send a message to a list of recipients.
Configure the JavaMail API in a JSF Application
In this case, we'll configure sending emails using SMTP protocol. You must have SMTP server that is responsible to send mails. You can install one or use that’s provided by the host provider or by services like Gmail.
The javax.mail.jar file contains the core classes of JavaMail API used to implement mailing services. You will also need an implementation of the JavaBeans Activation Framework (JAF) unless you're using JDK 1.6 or newer (which includes JAF). activation.jar file contains classes that make up the JAF standard extension. JavaMail uses JAF to encapsulate message data, and to handle commands intended to interact with that data. Interaction with message data should take place via JAF-aware JavaBeans.
The javax.mail.jar file includes the SMTP, IMAP, and POP3 protocol providers. The SMTP service provider provides access to an SMTP server to deliver email notifications over the Internet. JavaMail also provides access from an application to IMAP and POP capable mail servers on your network or the Internet in order to read emails from these servers. Note that the API itself does not provide mail server functionality; you must have access to a mail server to use JavaMail.
Visit the JavaMail API website and download the "javax.mail.jar" and "activation.jar" files. Copy and paste them into the web/WEB-INF/lib folder. Create the lib folder if it does not exist. In NetBeans, right click on the Project and go to Properties. Select Libraries, Add JARs... Select the .jar files from web/WEB-INF/lib and click OK.
Steps to send the email
- Create a Session instance using the Properties object and the Authenticator object (if SMTP authentication is needed). Get the Session object using Session.getInstance method with Authenticator. JavaMail uses standard properties, which may be set in the Session object, or in the Properties object used to create the Session object. There is more information on this in the JavaMail API documentation
- Construct a MimeMessage instance, populate the message headers and content. JavaMail uses the JAF defined DataHandler object to contain data placed in the message.
- Send the message - The javax.mail.Transport class provides method send to send the message to all recipient addresses specified in the message.