Online Hibernate Training just in Rs. 1000

One week online training in Hibernate ORM framework. Learn Hibernate Framework and enhance your skills. Best Online Hibernate training at 90% discount. Join Now!! Offer is valid Only for limited Period!!! Online Training

Send Email From JSP & Servlet

In this page you will learn how to Send Email From JSP & Servlet. The code example of this is also available for download for free.


J2EE Tutorial - Send Email From JSP & Servlet


A simple html form('dbdemo.htm') which invokes dbdemo.jsp  follows:




<form   method=post  action=dbdemo.jsp>

type the selectquery  here

  <input   type=text   size=60  name='text1'> <br>

  <input   type=submit>







    <[email protected] import="java.sql.*"  %>



  String  url  = "jdbc:odbc:telephone";   // an access database registered in odbc  

  Connection   con = DriverManager.getConnection(url);

  Statement   stm = con.createStatement();   //  container for sql  

  String  sql = request.getParameter('text1');   // sql

  ResultSet  rs   = stm.executeQuery ( sql);



   out.println(rs.getString(1)+"<br>");  // name

   out.println(rs.getString(2)+"<br>");   // telephone number







As for sending mail from webserver, using JavaMail API, the following code shows how the required data

such as 'from', 'to', 'subject' and 'message' are collected  by the servlet and then processed for sending the mail.





<form  method=post  action="http://localhost:8080/servlet/mailservlet">

  sender  <input type=text name=text1><br>

   Reciever  <input type=text name=text2><br>

   Subject  <input type=text name=text3><br>

  Message  <textarea name='area1' rows=5 cols=30>  </textarea>

   <input   type=submit>




import javax.servlet.*;

import javax.servlet.http.*;


import javax.mail.*;

import javax.mail.internet.*;   // important

import javax.mail.event.*;  // important


import java.util.*;

public class servletmail extends HttpServlet


  public  void doPost(HttpServletRequest request,HttpServletResponse response)

    throws ServletException, IOException


  PrintWriter out=response.getWriter();




   Properties props=new Properties();

   props.put("","localhost");   //  'localhost' for testing

   Session   session1  =  Session.getDefaultInstance(props,null);

   String s1 = request.getParameter("text1"); //sender (from)

   String s2 = request.getParameter("text2");

   String s3 = request.getParameter("text3");

   String s4 = request.getParameter("area1");

   Message message =new MimeMessage(session1);

  message.setFrom(new InternetAddress(s1));






   out.println("mail has been sent");


  catch(Exception ex)







 Using javamail requires that we provide classpath to mail.jar & activation.jar. These should have been already installed in our machine. Otherwise, we will not be able to compile the servlet.  For testing the servlet, we should have installed some mail server in our machine.  For compiling the servlet, we have to set classpath to c:\jsdk2.0\src (java servlet development kit).

 (We are using Tomcat server.  The TOMCAT server is a webserver especially created for executing servlets and JSP . It is a joint effort by SunMicroSystems  & Apache Foundation. Tomcat can be run on Apache server or independently. Tomcat will work in Windows platform also. The current version is Tomcat 4. Tomcat 5 is expected shortly).

 We can  move dbdemo.htm and dbdemo.jsp to :

  c:\tomcat\webapps\root   directory.

 In another dos window,

   c:\tomcat\bin>SET   TOMCAT_HOME=C:\TOMCAT


  >startup ( this will start the tomcat server in port 8080)

Start InternetExplorer and type the url as 'http://localhost:8080/dbdemo.htm. We will get the form. Type the sql and submit the form. We will get the resultset sent by the server. We need not compile the jsp file. It is automatically compiled at the server.

For running a servlet, copy the servlet's class file to: c:\tomcat\webapps\root\web-inf\classes  directory.

For javamail , we  have to copy mail.jar & activation.jar to classes folder.

copy the invoking html file to: c:\tomcat\webapps\root   directory.

In the browser type the URL as 'http://localhost:8080/mailservlet.htm'. Enter the required data and submit the form to the server. The mail will be sent as specified by us. That is as brief a tour as possible  of the first group of technologies in the J2EE basket. This is an essential foundation because , even when we use RMI/EJB , the correct practice is to invoke the RMI through a servlet. In the JSP example seen above, the code was exposed to the web-administarator. The better method is to encapsulate the code as a bean and invoke it. This way, only the class file will be seen by the server-administaror. The JSP page also will be easier to read and maintain.In  the following  example , we will adopt that method.


Share on Google+Share on Google+

Send Email From JSP & Servlet

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



Discuss: Send Email From JSP & Servlet   View All Comments

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
Ankur Srivastava
July 28, 2011
I have the following queries regarding this post

->Where and what to change if i want to implement it on a web server like this line props.put("","localhost"); // 'localhost' for testing ->what to place instead of this localhost? ->Also tell me that, is it necessary to have a host with SMTP account for implementing it? ->Since our PC can also act as a webserver when connected to internet and the tomcat server is turned on, can we send an email to someone from our own PC without using any email accounts like that of gmail etc? plz reply thanks Ankur Srivastava [email protected]
deep jansari
November 3, 2011
how to find ISP's smtp address

sir, i am using tatadocomo's network for internet. i am unable to find the smpt address for this. please help me to find this. thanx
December 21, 2011
regarding a feature

The post your comment feature I want to use for a portal. Can you please tell me what all I need and how to code this.
January 25, 2012

Nice but i want to send a mial with an pdf attachment can u have any examples
August 1, 2012
message services

what is the program for file sending from one mail to another mail through tomcat server
August 14, 2012
unable to navigate

hi i am looking for j2ee tutorial but i am unable to find wer to start... the links are not working i am unable to get started with the tutorial because i am unable to navigate.. please help
September 7, 2012
Java servlet

how to send a security key through email_id send me the code.
Narayan Bhendarkar
October 6, 2012
code for message portal in servlet

I want code to redirect to the message portal site. I want code for that Plz,sent me as soon as possible.. thank you