'JSP Contact Form - Sending it to a Gmail

I am new with JSP and I am working in a project to create a contact form page. I am using Tomcat as my local host. I have created the form, and saved it in JSP. It is a very simple form. With name, email, subject, message.

Now, I need to give action to it using JSP as well, and send it to a Gmail. So when someone uses the contact form it will be send to a gmail adress. I created a new JSP file and named it mail.jsp.

But now I am totally lost. Can someone help me, please?

Here is the first page, the contact form:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>Contact Form</title>
    </head>
    <body>

    <form method="post" action="mail.jsp">
    <center><h4>Contact us:</h4></center>
    <br />
    <center>Name: <input type="text" name="name"><br /></center> 
    <br> 
    <center>Email: <input type="text" name="email"><br /></center>
    <br>  
    <center>Subject: <input type="text" name="subject"><br /></center>
    <br>  
    <center>Message: <br/><textarea name="message"> </textarea><br /></center>
    <center>
    <input type="submit" value="Submit">
    <input type="reset" value="Reset">
    </center>   
  </form>
  </body>
  </html>

What should I do now?



Solution 1:[1]

You need to use an SMTP client (like the one in the Javamail library) to send from your domain [email protected] to [email protected]. You need to put the Javamail JAR on your CLASSPATH, and code like in this question.

Since Java programming in a JSP is not recommended, you'd better using a Servlet for this.

Solution 2:[2]

As your action is set to mail.jsp so create that JSP and inside JSP you can use scriptlets like

<%
// Use Java Mail API to send email here
%>

Some points
1. Get the information from request e.grequest.getParameter("subject")
2. Explore Java Mail API and find out about SMTP server available for you. If not available then you can also Google SMTP Server (Do bit google on it)
3. I think your problem is how to put Java Code in JSP, so as mentioned above use scriptlets.

The best way of doing this create a servlet and in your form action attribute give URL of that servlet. And after sending email from that servlet you can forward to any other JSP or same one again with a message (Email Sent).

If you are new to JSP then this should help, but if you are new to Java do tell me I can send source code that can send email using your form parameters.

Solution 3:[3]

Look at JavaBrains video tutorial, it's great for me.
There will be information how You can send data from form in JSP to servlet and how You can do something with that data.
http://javabrains.koushik.org/p/jsps-and-servlets.html

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Community
Solution 2 Babar Ali
Solution 3 Dob