Signup · Login
Stardeveloper.com  
Home · Tutorials · Forums · ASP.NET Newsletter Application · Web Hosting Plans · Faisal Khan's Blog · Contact
Search Stardeveloper.com
Newsletter
Enter your email address to receive full length articles at Stardeveloper:


Article Categories
.NET  .NET
  ASP (16)
  ASP.NET (41)
  ADO (16)
  ADO.NET (11)
  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)
Log In
UserName Or Email:

Password:

Auto-Login:

Miscellaneous Links
  Submit Article
Stardeveloper RSS Feed
Hosted by Securewebs.com
 
Home : J2EE : JSP : Sending Emails using JavaServer Pages
 
RSS - Read full length articles at Stardeveloper using Stardeveloper RSS Feed RSS

Sending Emails using JavaServer Pages

by Faisal Khan. Follow Faisal Khan on Twitter Follow Faisal Khan on Facebook

In this article we will learn how to send plain 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 send the emails for us.

Why will you need to send emails from your website?
Following are just a few reason why you will need to send emails from your website, 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.

If you are looking for a production ready, stable, easy to use ASP.NET Newsletter application, then look no further: Faisal Khan (the author of this tutorial) has created a great Newsletter Application for you to download and start using today. It is easy to install, lets your users subscribe using a subscription form, provides a simple administration interface to create and send newsletters, handles the task of sending newsletters in the background using thread queues on the server, and lets users unsubscribe if they wish so.

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: 76, Comments: 112 )
    Contains 1 or more replies by the Author of this Article.
    Contains 1 or more replies by Faisal Khan.

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