Sending E-Mails with JSP Pagesby Faisal Khan.
In this article we will learn how to send simple text emails using JSP pages.
Since it is not a good habit to put Java code in JSP pages, we will create a
JavaBean to do that.
Why to send emails?
Following are just a few reason why you will need to send emails from your
web site, enterprise application etc :
- To put feedback forms on your site, allowing users to easily send comments
and suggestions to you regarding the content on your web site.
- Sending confirmation emails to buyers that the product they bought will
be sent to them in this much time.
- To program your application in such a way to send you ( the webmaster ) email
whenever some thing important goes down on your web site e.g. database server
crashes.
- To send emails to mailing list. This is a rather important use and I will
probably describe it in a separate article.
Well, above were only few points which came to my mind, but I know that you understand
that you need to have this capability on your web site.
Requirements
You need to have at least a JSP 1.1 capable application server or Servlet container.
Almost all of the application servers, servlet containers I know are capable of JSP 1.1
so it shouldn't be any problem. The least you can do is to download and install
Tomcat server. I have explained every step from downloading to installing and running Tomcat server in a separate article. You
can read it from there.
Next you need to have JavaMail ( mail.jar ) jar file
in your application server's classpath. Download the latest JavaMail 1.2 jar file
from Sun's JavaMail
web site and put it in your server /lib folder. Most application servers have a "lib"
( stands for library ) folder where you can put the jar files you need your server to
know about. If your application server doesn't have a lib folder, you should consult
it's documentation as to where additional jar files should be put.
If nothing works you can at least put the jar file in your web application's
/WEB-INF/lib folder. This should work. Still if nothing works, you can post your
question here at the bottom of this article.
One last thing you need to have in your application server's classpath is
Java Activation Framework jar file. You can download it from
here. It is needed by the JavaMail class files. Once both activation.jar and
mail.jar files are in your server's classpath or in your web application's WEB-INF/lib
folder, you are ready to move forward.
Building the application
To demonstrate how to send emails, we'll create just 4 files :
- index.html
A simple HTML page which displays a form to you containing few input fields.
Here you will enter the sender's and receiver's email addresses, subject and
message of the email.
- mailer.jsp
The JSP page which calls the MailerBean to send email. Since all the work
is done by the MailerBean, this JSP page is simpler than you can imagine.
- errorPage.jsp
An error page which will catch any exceptions thrown by mailer.jsp ( from the
MailerBean ) JSP page and show it to the user. Exceptions can be thrown when
for instance you enter an illegal receiver's email address, or simply forget to
enter anything in the receiver's input field.
- MailerBean.class
The actual bean which does all the hard work of sending emails for us.
Advice
Just in case if you are wondering that it is taking more effort than you thought
as you have to create two extra files; errorPage.jsp and MailerBean, you are right.
I could have easily cluttered the mailer.jsp page will all the Java code required
to send emails but then it'll be wrong. What if you happen to require sending emails
in another JSP page for instance? will you copy all the code from mailer.jsp to
that page? you shouldn't do that. Encapsulating all the code in MailerBean allows
us to use this bean from anywhere we want. You can use it from within JSP pages,
Servlets, other beans or even EJBs and standalone applications. Using JavaBeans
allows a great deal of scalability.
The second extra page, errorPage.jsp is an error page which will catch the ugly
exceptions from within mailer.jsp page show it in a rather friendly environment to
the user. I have added this error page here so that you also learn good practices while
learing J2EE.
To sum up, scalability is the key. If you ever happen to come across a solution
which in not scalable, doesn't matter how good it is, my advice is to stay away from it.
i. index.html
Create a new folder in your web application folder and give it any name. For
example's sake, I will call this folder "mail". Now create a new index.html HTML
page in it and copy the following text in it :
<html>
<head>
<style>
div,input,textarea { font-family:Tahoma; font-size:8pt; }
input.std { width:200; }
div.frame { padding-left:70; }
</style>
</head>
<body>
<div class="frame">
<form action="mailer.jsp" method="post">
To :<br>
<input type="text" name="to" class="std"></input><br>
From :<br>
<input type="text" name="from" class="std"></input><br>
Subject :<br>
<input type="text" name="subject" class="std"></input><br>
Message :<br>
<textarea rows="10" cols="80" name="message"></textarea>
<br>
<input type="submit" value="Send"></input>
</form>
</div>
</body>
</html>
As you can see it is a simple HTML page which displays a form to the user
containing 3 input and 1 big textarea field. The 2 input fields are for receiver's
and sender's email address, while the 3rd field is for the subject of the email.
ii. mailer.jsp
Create a new JSP page and save it as "mailer.jsp" in the same folder as the one
in which you placed index.html. Copy the following code and paste it in mailer.jsp
page :
<%@ page errorPage="errorPage.jsp" %>
<html>
<head>
<style>
div,input,textarea { font-family:Tahoma; font-size:8pt; }
input.std { width:200; }
div.frame { padding-left:70; color:green; }
</style>
</head>
<body>
<div class="frame">
<jsp:useBean id="mailer" class="com.stardeveloper.bean.test.MailerBean">
<jsp:setProperty name="mailer" property="*"/>
<% mailer.sendMail(); %>
</jsp:useBean>
Email has been sent successfully.
</div>
</body>
</html>
Explanation
First important thing to notice is the "errorPage" attribute in the "page"
directive at the top. It tells the servlet container that this page makes use
of an error page so any exception which is thrown in this page should be made
available to the error page, where we show it to the user. In this article we will learn how to send simple text emails using JSP pages.
Since it is not a good habit to put Java code in JSP pages, we will create a
JavaBean to do that.
Why to send emails?
Following are just a few reason why you will need to send emails from your
web site, enterprise application etc :
- To put feedback forms on your site, allowing users to easily send comments
and suggestions to you regarding the content on your web site.
- Sending confirmation emails to buyers that the product they bought will
be sent to them in this much time.
- To program your application in such a way to send you ( the webmaster ) email
whenever some thing important goes down on your web site e.g. database server
crashes.
- To send emails to mailing list. This is a rather important use and I will
probably describe it in a separate article.
Well, above were only few points which came to my mind, but I know that you understand
that you need to have this capability on your web site.
Requirements
You need to have at least a JSP 1.1 capable application server or Servlet container.
Almost all of the application servers, servlet containers I know are capable of JSP 1.1
so it shouldn't be any problem. The least you can do is to download and install
Tomcat server. I have explained every step from downloading to installing and running Tomcat server in a separate article. You
can read it from there.
Next you need to have JavaMail ( mail.jar ) jar file
in your application server's classpath. Download the latest JavaMail 1.2 jar file
from Sun's JavaMail
web site and put it in your server /lib folder. Most application servers have a "lib"
( stands for library ) folder where you can put the jar files you need your server to
know about. If your application server doesn't have a lib folder, you should consult
it's documentation as to where additional jar files should be put.
If nothing works you can at least put the jar file in your web application's
/WEB-INF/lib folder. This should work. Still if nothing works, you can post your
question here at the bottom of this article.
One last thing you need to have in your application server's classpath is
Java Activation Framework jar file. You can download it from
here. It is needed by the JavaMail class files. Once both activation.jar and
mail.jar files are in your server's classpath or in your web application's WEB-INF/lib
folder, you are ready to move forward.
Building the application
To demonstrate how to send emails, we'll create just 4 files :
- index.html
A simple HTML page which displays a form to you containing few input fields.
Here you will enter the sender's and receiver's email addresses, subject and
message of the email.
- mailer.jsp
The JSP page which calls the MailerBean to send email. Since all the work
is done by the MailerBean, this JSP page is simpler than you can imagine.
- errorPage.jsp
An error page which will catch any exceptions thrown by mailer.jsp ( from the
MailerBean ) JSP page and show it to the user. Exceptions can be thrown when
for instance you enter an illegal receiver's email address, or simply forget to
enter anything in the receiver's input field.
- MailerBean.class
The actual bean which does all the hard work of sending emails for us.
Advice
Just in case if you are wondering that it is taking more effort than you thought
as you have to create two extra files; errorPage.jsp and MailerBean, you are right.
I could have easily cluttered the mailer.jsp page will all the Java code required
to send emails but then it'll be wrong. What if you happen to require sending emails
in another JSP page for instance? will you copy all the code from mailer.jsp to
that page? you shouldn't do that. Encapsulating all the code in MailerBean allows
us to use this bean from anywhere we want. You can use it from within JSP pages,
Servlets, other beans or even EJBs and standalone applications. Using JavaBeans
allows a great deal of scalability.
The second extra page, errorPage.jsp is an error page which will catch the ugly
exceptions from within mailer.jsp page show it in a rather friendly environment to
the user. I have added this error page here so that you also learn good practices while
learing J2EE.
To sum up, scalability is the key. If you ever happen to come across a solution
which in not scalable, doesn't matter how good it is, my advice is to stay away from it.
i. index.html
Create a new folder in your web application folder and give it any name. For
example's sake, I will call this folder "mail". Now create a new index.html HTML
page in it and copy the following text in it :
<html>
<head>
<style>
div,input,textarea { font-family:Tahoma; font-size:8pt; }
input.std { width:200; }
div.frame { padding-left:70; }
</style>
</head>
<body>
<div class="frame">
<form action="mailer.jsp" method="post">
To :<br>
<input type="text" name="to" class="std"></input><br>
From :<br>
<input type="text" name="from" class="std"></input><br>
Subject :<br>
<input type="text" name="subject" class="std"></input><br>
Message :<br>
<textarea rows="10" cols="80" name="message"></textarea>
<br>
<input type="submit" value="Send"></input>
</form>
</div>
</body>
</html>
As you can see it is a simple HTML page which displays a form to the user
containing 3 input and 1 big textarea field. The 2 input fields are for receiver's
and sender's email address, while the 3rd field is for the subject of the email.
ii. mailer.jsp
Create a new JSP page and save it as "mailer.jsp" in the same folder as the one
in which you placed index.html. Copy the following code and paste it in mailer.jsp
page :
<%@ page errorPage="errorPage.jsp" %>
<html>
<head>
<style>
div,input,textarea { font-family:Tahoma; font-size:8pt; }
input.std { width:200; }
div.frame { padding-left:70; color:green; }
</style>
</head>
<body>
<div class="frame">
<jsp:useBean id="mailer" class="com.stardeveloper.bean.test.MailerBean">
<jsp:setProperty name="mailer" property="*"/>
<% mailer.sendMail(); %>
</jsp:useBean>
Email has been sent successfully.
</div>
</body>
</html>
Explanation
First important thing to notice is the "errorPage" attribute in the "page"
directive at the top. It tells the servlet container that this page makes use
of an error page so any exception which is thrown in this page should be made
available to the error page, where we show it to the user.
|