CLOSE ✕
Get in Touch
Thank you for your interest! Please fill out the form below if you would like to work together.

Thank you! Your submission has been received!

Oops! Something went wrong while submitting the form

Send password recovery email using Java Server Pages

Neelanshi Sharma
|
JSP
|
Jan 19, 2021

To send an email using a JSP, you should have the JavaMail API and the Java Activation Framework (JAF) installed on your machine. Download and unzip these files, in the newly-created top-level directories. You will find a number of jar files for both the applications.

Create a form for receiver's email

First thing that needs to be done is to create a simple form to intake receiver's email or to fetch id we wish to send email to.

This could be easily done within our .jsp file as JSP allows embedding Dynamic Elements in HTML Pages itself instead of having separate CGI files. So our first step would be to create an input field for it and wrap it inside the <form> tag with method="Post".

<html>
<head>
<title>Send Email using JSP</title>
</head>

<body>
<form name="emailform" action="Recovery.jsp" method="post">
<h1>Recover Your Password</h1>
Email ID: <input type="text" name="email" size=50px placeholder="email" required="required">

<input type="submit" name="Email" value="Send Link" >
</form></body>
</html>

Also, to enable mailing functionality we need to import the following:

<%@ page import = "java.io.*,java.util.*,javax.mail.*"%>
<%@ page import = "javax.mail.internet.*,javax.activation.*"%>
<%@ page import = "javax.servlet.http.*,javax.servlet.*" %>

Now we an continue with the background functionality as to what must happens once Send is clicked.

1. Save all your variables to be passed as parameters to an email i.e. From, Subject, Message, Password (of the sender) and the Host ("smtp.gmail.com" incase of Gmail).

// Recipient's email ID needs to be mentioned.
String to = "xyz@gmail.com";

// Sender's email ID needs to be mentioned
String from = "abcd@gmail.com";

// Assuming you are sending email from localhost
String host = "localhost";

2. Map the above data using Properties class available in Java. Enable SMTP by setting it's value to true. You need to use port 587 for SMTP (other ports are 25, 465 and 2525).

// Get system properties object
Properties properties = System.getProperties();

// Setup mail server
properties.setProperty("mail.smtp.host", host);

3. Initialize an instance of Session to continue and override getPasswordAuthentication() method which returns an object of type PasswordAuthentication.

// Get the default Session object.
Session mailSession = Session.getDefaultInstance(properties);

4. Create an instance of class MimeMessage. This class represents a MIME style email message. It implements the Message abstract class and the MimePart interface. (MimeMessage uses the InternetHeaders class to parse and store the top level RFC 822 headers of a message.)

// Create a default MimeMessage object.
MimeMessage message = new MimeMessage(mailSession);

// Set From: header field of the header.
message.setFrom(new InternetAddress(from));

// Set To: header field of the header.
message.addRecipient(Message.RecipientType.TO,
                              new InternetAddress(to));
// Set Subject: header field
message.setSubject("This is the Subject Line!");

// Now set the actual message
message.setText("This is actual message");

5. Send the message using Transport class. Transport class is used as a message transport mechanism. This class normally uses the SMTP protocol to send a message.

// Send message
Transport.send(message);

And there you go. Your mail has been sent. You can call this JSP using the URL http://localhost:8080/Recovery.jsp. This will help send an email to the given email ID abcd@gmail.com.

Result: Your mail sent successfully.....

You can find the complete code for this procedure here.

Neelanshi Sharma
Neelanshi is a mobile and web developer. She is a computer science major from Banasthali Vidyapith, India.

Recent Blog Posts

Lets Work Together
Contact Me