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 (43)
  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)

Main Category  Other
  Website Maintenance (3)
Log In
UserName Or Email:

Password:

Auto-Login:

Hosted by Securewebs.com
 
Home : J2EE : JSP : Sending Emails using JavaServer Pages
 
Read full length articles at Stardeveloper using Twitter Follow on Twitter Facebook Facebook fan page Email Get Articles via Email RSS Get Articles via RSS Feed

Sending Emails using JavaServer Pages

by Faisal Khan.

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

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