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.
Step 2 : Creating a new JSP web project
In this step we select a new web project and click on next.
Step 3 : Name and Location
Giving a specific name and set a location for this web application
and click on next button.
Step 4 : Select server and setting
Select server GlassFish 3.1 and java EE 6 web application and also set the
context path.
Step 5 : Select Framework
There is no need to select any frame for this application and click
on finish button.
Step 6 : Select New JSP File
Select a new JSP File in This Way.
Index.jsp
Click on the finish button and code it with email receiving code in the
following way.
<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
<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
Index.jsp
I hope that this application help you to understand in how to send an E-mail in JSP via NetBeans.