This tutorial covers sending email from a Java EE application using the JavaMail API.
Time to Complete
Approximately 45 minutes.
The JavaMail API defines classes which represent the components of a mail system. JavaMail does not implement an email server, instead it allows you to access an email server using a Java API. In order to test the code presented in this tutorial you must have access to an email server. While the JavaMail API specification does not mandate support for specific protocols, JavaMail typically includes support for POP3, IMAP, and SMTP. This tutorial covers sending mail messages and only requires access to an SMTP server.
Sending an email from within a Java application has many practical uses. A web application may allow visitors to create an account. Part of the account creation process could include sending an email with a unique activation code to verify that a valid email address was given at signup. Online shopping applications typically notify buyers of order or tracking status via email.
Hardware and Software Requirements
The following is a list of hardware and software requirements:
- Download and install Java JDK 7 from this link.
- Download and install an IDE and Application Server such as
NetBeans 7.1.2 with Java EE which includes GlassFish 3.1.2
(Java EE download bundle) from this link.
During installation, be sure to check the box to install
GlassFish. JUnit is an optional installation and not required
for this tutorial.
- Access to an SMTP server
- Valid credentials (username and password) for the SMTP server
- The project created in this tutorial can be downloaded from here.
Before starting this tutorial, you should:
- Have access to an SMTP server. You must know the host name, port number, and security settings for your SMTP server. Web mail providers may offer SMTP access, view your email account settings or help to find further information. Be aware that your username is often your full email address and not just the name that comes before the @ symbol.
- A Java EE IDE and Application Server such as GlassFish or Oracle WebLogic Server. JavaMail can be downloaded and used as a library in a Java SE application but this tutorial assumes the use of a Java EE application server which would already include JavaMail.
- Have basic familiarity with Servlets and Session EJBs (helpful but not required)
Create a Java EE 6 Web Application
To create a Java EE 6 Web Application (WAR) project, perform the following steps in the NetBeans IDE:
You should now have a Web Application project with an index.jsp file.
Create the required JSP, Servlet, and EJB components
You will create a JSP containing a form allowing you to specify a email recipient, subject, and message body. The form will submit to a Servlet which will read the form parameters. The Servlet will pass the form parameters to a sendEmail method in Session EJB you will create.
The Session EJB
You will complete the implementation of the sendEmail method in a later section after you have created all the components of the application.
You should now be able to deploy and run the EmailSender project. No emails will actually be sent when you submit the form. You will add the email sending capability in the next section.
Sending a Plain Text Email with the JavaMail API
In this section you will send a plain text email using the JavaMail API.
A Protocol Enum
The EmailSessionBean Class
Sending an HTML Email with the JavaMail API
This section will show you how to send an HTML email. You will modify the EmailSessionBean class to send a message consisting of both a HTML body and a plain text alternative. When the recipient of the email can not view the HTML version of the content the plain text version will be show instead.
The following relevant information may aid you when developing JavaMail applications.
To help navigate this Oracle by Example, note the following:
- Hiding Header Buttons:
- Click the Title to hide the buttons in the header. To show the buttons again, simply click the Title again.
- Topic List Button:
- A list of all the topics. Click one of the topics to navigate to that section.
- Expand/Collapse All Topics:
- To show/hide all the detail for all the sections. By default, all topics are collapsed
- Show/Hide All Images:
- To show/hide all the screenshots. By default, all images are displayed.
- To print the content. The content currently displayed or hidden will be printed.
To navigate to a particular section in this tutorial, select the topic from the list.