Signup · Login
Stardeveloper.com  
Home · Articles · Forums · Advertise · Contact
Search this Website
Stay Informed
Enter your email address below to be informed every time a new article is posted at Stardeveloper.com:
Article Categories
.NET  .NET
  ASP (15)
  ASP.NET (29)
  ADO (16)
  ADO.NET (10)
  COM (6)
  Web Services (4)
  C# (1)
  VB.NET (3)
  IIS (2)

J2EE  J2EE
  JSP (15)
  Servlets (9)
  Web Services (1)
  EJB (4)
  JDBC (4)
  E-Commerce (1)
  J2ME (1)
  Products (1)
  Applets (1)
  Patterns (1)
Latest Forum Activity
Re: Unable to insert data in an Access databa..
by Faisal Khan on 12 Nov 2008 Go To Post

Re: HOW TO MOVE FROM APACHE TO MS IIS
by Faisal Khan on 12 Nov 2008 Go To Post

Re: Preventing/Investigating the Source of SQ..
by smithsan on 19 Oct 2008 Go To Post

Re: Preventing/Investigating the Source of SQ..
by peeru999 on 8 Oct 2008 Go To Post

Re: Preventing/Investigating the Source of SQ..
by Faisal Khan on 8 Oct 2008 Go To Post

Log In
UserName Or Email:

Password:

Auto-Login:

Miscellaneous Links
  Submit Article

Hosted by Securewebs.com
 
Home : J2EE : JSP : Sending E-Mails with JSP Pages
 

Sending E-Mails with JSP Pages
by 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.


 ( 2 Remaining ) Next

Comments/Questions ( Threads: 69, Comments: 104 )
    Contains 1 or more replies by the Author of this Article.
    Contains 1 or more replies by Faisal Khan.

  1. pls help me to find the soln to send coments(feedback) to my mail id(gmail id)from my JSP web page
  2. problem on submitting index.html
  3. Error in mailer.jsp.URGENT!
  4. Error in mailer.jsp
  5. About message variable
  6. i am Getting error after sending index.html page.
  7. getting error when runing the sending mail application
  8. I am not using outlook express, is there any other way to find SMTP address ( 1 Reply ) This thread contains 1 reply by the Author of this Article. This thread contains 1 reply by Faisal Khan.
  9. Wats d pbm in my code....After sending index page...Im getting this error
  10. Urgent -- HTTP 500 - Internal server error
  11. Carbon Copies
  12. HTTP 500 Error ( 1 Reply )
  13. cannot send email
  14. how to handle the exception ( 2 Replies ) This thread contains 1 reply by the Author of this Article. This thread contains 1 reply by Faisal Khan.
  15. error in mailer.jsp
  16. Excellent Little Course
  17. Error : Page cannot be displayed ( 1 Reply )
  18. Jsp Mailer
  19. How to configure the script so it creates MESSAGE in the bean?
  20. errorPage.jsp isn't working
  21. SMTP Host
  22. problem in Sending email in struts
  23. Http 500 Error
  24. URGENT!! Please Help! ( 1 Reply )
  25. Email Address to be pre-defined
  26. Help
  27. Unable to compile class for JSP-package cmail does not exist
  28. question for sending mail using jsp
  29. question for sending mail using jsp
  30. Failed to load Main-Class attribute
  31. Problem in executing
  32. E-mail witch attachments ( 3 Replies )
  33. Email was sent successfully but was not received
  34. Include variables in body ( 1 Reply )
  35. Lotus Notes Email
  36. sending a html page as email in jsp
  37. mail.jar not clear!!!!
  38. MimeMessage Problem
  39. sending failed
  40. Problem with connecting to SMTP host
  41. some informations
  42. my jsp not run
  43. HTTP 500 - Internal server error ( 1 Reply )
  44. error while running MailerBean.Java ( 1 Reply )
  45. error while compiling
  46. Problem with HTTP 500 Internal server error in mailer.jsp ( 3 Replies )
  47. Problem in the MailerBean while sending (Invalid Addresses) ( 3 Replies )
  48. jps email list
  49. send email with jsp if user is idle for more than N days (without setting up SMTP)
  50. thsi code was very usefull
  51. org.apache.jasper.JasperException: Unable to compile class for JSP
  52. My SMTP need login and password ( 2 Replies )
  53. javax/activation/DataSource ( 1 Reply )
  54. help,help,I couldn't get it compiled!!! ( 1 Reply )
  55. i am finding it difficult while compiling MailerBean.java
  56. help jsp email
  57. Sending E-Mails with JSP Pages ( 1 Reply )
  58. Java mail ( 1 Reply ) This thread contains 1 reply by the Author of this Article. This thread contains 1 reply by Faisal Khan.
  59. Facing FileNotFoundException in following code of sending attachment from jsp
  60. Problem in accessing MailerBean
  61. On Sending emails ( 1 Reply )
  62. Are you planning to write something about receiving an Email ( 1 Reply )
  63. Problems with the MailerBean ( 1 Reply ) This thread contains 1 reply by the Author of this Article. This thread contains 1 reply by Faisal Khan.
  64. about setting SMTP ( 1 Reply ) This thread contains 1 reply by the Author of this Article. This thread contains 1 reply by Faisal Khan.
  65. About compile MailerBean ( 3 Replies ) This thread contains 1 reply by the Author of this Article. This thread contains 1 reply by Faisal Khan.
  66. confused about class path ( 1 Reply ) This thread contains 1 reply by the Author of this Article. This thread contains 1 reply by Faisal Khan.
  67. about importing javamail
  68. about mailer.jsp ( 1 Reply )
  69. This site simply rocks ( 2 Replies ) This thread contains 1 reply by the Author of this Article. This thread contains 1 reply by Faisal Khan.

Post Comments/Questions

In order to post questions/comments, you must be logged-in. If you are not a member yet, then signup, otherwise login. Once you login then come back to this page and you'll see a form right here which will allow you to post comments/questions.

Please note, one of the benefits of signing up is to be notified immediately by email everytime you receive a reply to the thread you have subscribed.

 
© 1999 - 2008 Stardeveloper.com, All Rights Reserved.