Introduction
This article explains how to create a Java Mail API. The NetBeans IDE is used for creating this application.
What is Java Mail API
This API is used to write, read and compose mails. This API provides a platform-independent and protocol-independent framework for sending and receiving mails. The javax.mail and javax.mail.activation packages contain the core classes of the Java Mail API.
Some uses
The following are some uses:
- Used at the time of registering the user (sending notification such as thanks message)
- Used when the user forgot their password; the password is then sent to the user's email-id
- Used to send some important notification, update, etcetera.
Some Protocols mainly used are
- POP (Post Office Protocol).
- NNTP (Network News Transfer Protocol).
- SMTP (Simple Mail Transfer Protocol).
- MIME (Multipurpose Internet Mail Extension Protocol).
- etcetera
Note: In our application; we use the MIME and SMTP protocols.
Some core classes
The following are some core classes:
- javax.mail.Folder
- javax.mail.Message
- javax.mail.Address
- javax.mail.Store
- javax.mail.Session
- javax.mail.PasswordAuthentication
- javax.mail.MimeMessages etcetera.
How to send email
This API provides many ways to send email, but you must have a SMTP server to send emails to. Use the simple GMail server or download any SMTP server manually.
How to send email
Use the following procedure to to send email:
- First get a session object
- Then compose message
- Finally, send the message
1. Get the Session Object
The javax.mail.Session class provides the following two method to get the object of a session.
- Session.getInstance()
- Session.getDefaultInstance()
2. Compose message
The javax.mail.Message class provides a method to compose the message. For this we need to pass the session object in the Mime interface.
Example
MimeMessage mssg=new MimeMessage(session);
3. Send the message
The javax.mail.Transport class provide methods to send the message.
- static void send(Message mssg)
- static void send(Message mssg, Address[] add)
Let's start creating our application using a servlet
For creating this application you first need to download the following JAR files and add to your project library.
- activation.jar
- mail.jar
Note: You can download the latest version of this JAR file directly from the Oracle Website.
Now you need to create following files:
- index.html file
- MailServlet.java file
- Mail.java file
1. index.html file
This file is necessary to provide an interface in which the user can provide their email-id, subject and message contents.
2. MailServlet.java file
This servlet file handles the request and provides the response to the user.
3. Mail.java file
This Java file contains the send method to send the email to the specifeid recipients.
Now to create this application we need to use the following procedure.
Step 1
Open the NetBeans IDE.

Step 2
Choose "Java web" -> "Web application" as in the following.

Step 3
Provide your project name as "JavaMailAPP" as in the following.
Step 4
Choose the Server and Java version wizard as in the following.

Step 5
Now delete the default "index.jsp" file and create a new "index.html" file and write the following code there.
index.html
<!DOCTYPE html>
<html>
<head>
<title>TODO supply a title</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta name="viewport" content="width=device-width">
</head>
<body bgcolor="pink">
<form action="MailServlet">
<table>
<tr>
<td>To:</td>
<td><input type="text" name="mail"/><br/></td>
</tr>
<tr>
<td>Subject:</td>
<td><input type="text" name="subject"><br/></td>
</tr>
<tr>
<td>Message Text:</td>
<td><textarea rows="12" cols="80" name="message"></textarea><br/></td>
</tr>
<tr>
<td><input type="submit" value="Send"></td>
<td><input type="reset" value="Reset"></td>
</tr>
</table>
</form>
</body>
</html>Step 6
Now create a servlet named "MailServlet" and write the following code there.
MailServlet.java





Join the conversation! Your thoughts help the community grow.