Sponsered Links
Categories
Sponsered Links

Send Mail by using JSP and Servlet

 

This is a simple send mail example in J2EE. In this example we are using JSP and Servlet technology fro send the mail to a specific user. To run this program on your machine, simply create a directory and copy and paste the code in a file as name mentioned below.

demo.html

<html>

<body>

<form method=post action=demo.jsp>

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

          <input type=submit>

</form>

</body>

</html>


demo.jsp

<html>

    <body>

    <%@page import="java.sql.*"  %>

           <%

            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

            String url  = "jdbc:odbc:telephone"; 

            Connection con = DriverManager.getConnection(url);

            Statement stm = con.createStatement();

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

            ResultSet rs   = stm.executeQuery ( sql);

            while(rs.next())  {

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

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

                   out.println("========");

               }

          %>

</body>

</html>


mailservlet.html

<html>

<body>

<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><br>

                                             <input     type=submit>

</form>

</body>

</html>


mailServlet.java

import javax.servlet.*;

import javax.servlet.http.*;

import java.io.*;

import javax.mail.*;

import javax.mail.internet.*;

import javax.mail.event.*; 

import java.net.*;

import java.util.*;

public class servletmail extends HttpServlet{

    public  void doPost (HttpServletRequest request, HttpServletResponse response)   throws ServletException, IOException {

        PrintWriter out = response.getWriter();

        response.setContentType("text/html");

        try  {

           Properties props=new Properties();

           props.put("mail.smtp.host","localhost"); 

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

           String s1 = request.getParameter("text1");

           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));

          message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(s2, false));

           message.setSubject(s3);

           message.setText(s4);        

           Transport.send(message);

           out.println("Mail has been sent successfully");

        }

        catch(Exception e)  {

           System.out.println("Exception: "+e);

        }

    }

}

 
 
Sponsered Links
Latest Updates
 
All Content of this site is for learning only. We do not warrant the correctness of its content. The risk from using it lies entirely with the user. While using this site, you agree to have read and accepted our terms of use and privacy policy.
Copyright © 2009 JSPSERVLETTUTORIAL.INFO All Right Reserved