ARTICLE

How to send an E-Mail In JSP

Posted by Vikas Mishra Articles | JSP December 24, 2011
In this article I am going to describe how to send an e-mail in JSP using the NetBeans IDE(7.0).
Reader Level:
Download Files:
 

Sending An Email Using JSP Code With Netbeans IDE

In this article I am going to describe how to send an e-mail in JSP using the NetBeans IDE(7.0). This article is divided into several Steps. 

Step 1 : Downloading and Registering Libraries in NetBeans IDE 7.0.

First of all, you need to download the latest version of Java Mail , as well as DJ Native Swing. After you download the libraries, you need to put them into the NetBeans Library Manager. I recommend you store all libraries in, for example, C:\Program Files\Java\libraries.

custom1.jpg

Step 2 : Creating a new JSP web project

In this step we select a new web project and click on next.

selet new web page.jpg

Step 3 : Name and Location

Giving a specific name and set a location for this web application and click on next button.

set name and location.jpg

Step 4 : Select server and setting

Select server GlassFish 3.1 and java EE 6 web application and also set the context path.

select serrver and setting it.jpg

Step 5 : Select Framework

There is no need to select any frame for this application and click on finish button.

selectFramework.jpg

Step 6 : Select New JSP File

Select a new JSP File in This Way. 

create new jsp file.jpg

Index.jsp

Click on the finish button and code it with email receiving code in the following way.

index.jsp.co.jpg


<html>
<head>
<title>java mail </title>
</head>
<body bgcolor="green">
<%@ page import="java.util.*" %>
<%@ page import="javax.mail.*" %>
<%@ page import="javax.mail.internet.*" %>
<%@ page import="javax.activation.*" %>
<%
String host = "localhost, 192.168.1.1";//your local host
String to = request.getParameter("to");
String from = request.getParameter("from");
String subject = request.getParameter("subject");
String messageText = request.getParameter("body");
boolean sessionDebug = false;
Properties props = System.getProperties();
props.put("mail.host", host);
props.put("mail.transport.protocol", "smtp");
Session mailSession = Session.getDefaultInstance(props, null);
mailSession.setDebug(sessionDebug);
Message msg = new MimeMessage(mailSession);
msg.setFrom(new InternetAddress(from));
InternetAddress[] address = {new InternetAddress(to)};
msg.setRecipients(Message.RecipientType.TO, address);
msg.setSubject(subject);
msg.setSentDate(new Date());
msg.setText(messageText);
Transport.send(msg);
out.println("Mail was sent to " + to);
out.println(" from " + from);
out.println(" using host " + host + ".");
%>
</table>
</body>
</html>


MailForm.jsp

mailformco.jpg

<html>
<body bgcolor="cyan">
<form action="index.jsp" method="post">
<table cellspacing="2" cellpadding="2" border="1">
<tr>
<td>To:</td>
<td>
<input type="text" name="to" size="30" maxlength="30">
</td>
</tr>
<tr>
<td>From:</td>
<td>
<input type="text" name="from" size="30" maxlength="30">
</td>
</tr>
<tr>
<td>Subject</td>
<td>
<input type="text" name="subject" size="30" maxlength="30">
</td>
</tr>
<tr>
<td colspan="2">
<textarea cols="40" rows="10" name="body"></textarea>
</td>
</tr>
<tr>
<td>
<input type="submit" name="submit" value="Submit">
<input type="Reset">
</td>
</tr>
</table>
</form>
</body>
</html>


Compile and Running The Application

After these step compile and run the application(shift+f6) and find the following output.

Mailform.jsp

home.jsp.jpg

Index.jsp

index.jsp.jpg

I hope that this application help you to understand in how to send an E-mail in JSP via NetBeans.   

Login to add your contents and source code to this article
post comment
     

Can't working in eclipse? it's not working in eclipse. My error is it. org.apache.jasper.JasperException: An exception occurred processing JSP page /mail_send.jsp at line 3330: msg.setSubject(subject); 31: msg.setSentDate(new Date()); 32: msg.setText(messageText); 33: Transport.send(msg); 34: out.println("Mail was sent to " + to); 35: out.println(" from " + from); 36: out.println(" using host " + host + ".");Stacktrace:org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:521)org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:412)org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)javax.servlet.http.HttpServlet.service(HttpServlet.java:717)

Posted by Kwun Jaehyun Feb 06, 2013

Thanks..........

Posted by Vikas Mishra Jan 02, 2012

I really like this website,and hope you will write more ,thanks a lot for your information. <a href=" http://www.koreanjapanclothing.com/Evening_Dresses_62.htm " target="_new">wholesale evening dresses</a> <a href=" http://www.koreanjapanclothing.com/Fashion_Shoes_29.htm" target="_new">cheap fashion boots</a>

Posted by jay chou Dec 30, 2011
COMMENT USING
PREMIUM SPONSORS
Over-C is a holistic consortium of communications and technology specialists. We build, deploy and market both business as well as consumer products and solutions.
Join a Chapter
SPONSORED BY
  • PDF reports have never been easier to create. With our included WYSIWYG Designer, you can layout your reports, set up your data source and let DynamicPDF ReportWriter do the rest.
Join a Chapter