Share on Google+Share on Google+

Sending an email in JSP

Sending an email in JSP

Sending an email in JSP


Sending an email in JSP

In this section, you will learn how to send an email in jsp.  

Following is a simple JSP page for sending an email using javax mail APIs. The first jsp page that displays an email form containing the to, from addresses, subject and its descriptions. A "Send Mail" command button when you click that button then it will send your given information to from address. 

Note: You must be need mail.jar or activation.jar files in your applications. Whenever you could not be hold in your "WEB-INF/lib" then your application is not word perfectly and displays an error. 

Here the code of JSP:  mailAPI.jsp

<title>Mail API</title>
<table border="1" width="50%"  cellpadding="0" cellspacing="0">
  <td width="100%">
  <form method="POST" action="mail.jsp">
  <table border="1" width="100%" cellpadding="0" cellspacing="0">
  <h1>Mail API</h1>
  <td width="50%"><b>To:</b></td>
  <td width="50%"><input type="text" name="to" size="30"></td>
  <td width="50%"><b>From:</b></td>
  <td width="50%"><input type="text" name="from" size="30"></td>
  <td width="50%"><b>Subject:</b></td>
  <td width="50%"><input type="text" name="subject" size="30"></td>
  <td width="50%"><b>Description:</b></td>
  <td width="50%"><textarea name="description" type="text" 
cols="40" rows="15" size=100>
  <td><p><input type="submit" value="Send Mail" name="sendMail"></td>

 Here the code of JSP:  mail.jsp

<%page language="java" import="javax.naming.*,*,javax.mail.*,
  Session mailSession = Session.getInstance(System.getProperties());
  Transport transport = new SMTPTransport(mailSession,new URLName("localhost"));
  MimeMessage m = new MimeMessage(mailSession);
  m.setFrom(new InternetAddress(%><%request.getParameter("from")%><%));
  Address[] toAddr = new InternetAddress[] {
  new InternetAddress(%><%request.getParameter("to")%><%)
  m.setRecipients(javax.mail.Message.RecipientType.TO, toAddr );
  m.setSentDate(new java.util.Date());
  m.setContent(%><%request.getParameter("description")%><%, "text/plain");
  out.println("Thanks for sending mail!");
catch(Exception e){


After running this application you get:

When you click "Send mail" Then you get:

Fill the following details and click the "Send Mail" command button:

Then you get:



Posted on: April 18, 2011 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Discuss: Sending an email in JSP  

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
sankar narayanan
August 11, 2013
could not connect

Dear i executed the above coding using NetBean 7.3.1 show this Couldn't connect to host, port: http://localhost:8084, 25; timeout -1 thanks and regards sankar
Rushikesh k
October 25, 2013
showing error

it is not working. i got an error: "Could not connect to SMTP host: localhost, port: 25"
saurabh kumar
July 16, 2014

this code is not working error -must use starttls command 503.
kiran gv
August 14, 2014
How to attacth file

Can You please help me on how to attach files to this.
August 14, 2014
unable to send mail from a contact us page in html

i have developed a html contact form on through i want to send message to a single email id from different site visitor's, but i am unable to send mail from contact.html page, please help
April 2, 2015
not working this code

Could not connect to SMTP host: localhost, port: 25" error display
April 17, 2015
for mail

i got null in output. so please tell me what i do.
August 29, 2011

Rashid MV
November 13, 2011
Mail sending

i have run a program to send mail through java which i got from rose india but it is not working properly. It is showing the message as follows.. 'Could not connect to SMTP host: localhost, port: 25' Please help. Thanks in advance