Sending Email using JSP

By: Vijay Emailed: 1769 times Printed: 2517 times    

Latest comments
By: rohit kumar - how this program is work
By: Kirti - Hi..thx for the hadoop in
By: Spijker - I have altered the code a
By: ali mohammed - why we use the java in ne
By: ali mohammed - why we use the java in ne
By: mizhelle - when I exported the data
By: raul - no output as well, i'm ge
By: Rajesh - thanx very much...
By: Suindu De - Suppose we are executing

This is a sample program to send email using JSP. Of course you should have a HTML page that shows a form to enter and get details of the email, just as you will see in any email client. The form should accept details such as 'to' email, 'from' email, 'subject' and 'body'. The form action should point to the JSP page shown below.

<title>JSP JavaMail Example </title>
<%@ page import="java.util.*" %>
<%@ page import="javax.mail.*" %>
<%@ page import="javax.mail.internet.*" %>
<%@ page import="javax.activation.*" %>
String host = "yourmailhost";
String to = request.getParameter("to");
String from = request.getParameter("from");
String subject = request.getParameter("subject");
String messageText = request.getParameter("body");
boolean sessionDebug = false;
// Create some properties and get the default Session.
Properties props = System.getProperties();
props.put("", host);
props.put("mail.transport.protocol", "smtp");
Session mailSession = Session.getDefaultInstance(props, null);
// Set debug on the Session
// Passing false will not echo debug info, and passing True will.
// Instantiate a new MimeMessage and fill it with the
// required information.
Message msg = new MimeMessage(mailSession);
msg.setFrom(new InternetAddress(from));
InternetAddress[] address = {new InternetAddress(to)};
msg.setRecipients(Message.RecipientType.TO, address);
msg.setSentDate(new Date());
// Hand the message to the default transport service
// for delivery.
out.println("Mail was sent to " + to);
out.println(" from " + from);
out.println(" using host " + host + ".");

JSP Home | All JSP Tutorials | Latest JSP Tutorials

Sponsored Links

If this tutorial doesn't answer your question, or you have a specific question, just ask an expert here. Post your question to get a direct answer.

Bookmark and Share


1. View Comment

Thank you for the script.
I was searching for my project.


View Tutorial          By: MUhammad Faizal at 2008-04-10 08:23:54
2. View Comment

lolz what's the code not working in JSP may be sending command is the servlet class... Not work at all

View Tutorial          By: lolz at 2008-12-10 03:31:50
3. View Comment

Hi lolz,
In order for this code to work, you should have a SMTP server and in your code you have to point to this server at the line
String host = "yourmailhost"


View Tutorial          By: Vijay at 2008-12-10 19:22:04
4. View Comment

I get an error " javax.mail" does not exists.

Please help.

View Tutorial          By: bhavin at 2009-02-03 05:50:08
5. View Comment

while run program i got this error
The server encountered an internal error () that prevented it from fulfilling this request.


org.apache.jasper.JasperException: Exception in JSP: /JSPmail.jsp:26

View Tutorial          By: jai at 2009-09-04 04:38:39
6. View Comment

Many thanks

View Tutorial          By: tushar at 2010-03-10 01:27:04
7. View Comment

i tried, the coding dont hv any errors but its not send any email not sending to inbox... i dont know what goes wrong.. i already off the firewall and antivirus too.. pls help

View Tutorial          By: anna at 2010-11-02 21:56:27
8. View Comment

dear sir ,
How can i create my smtp or host server,or can i use any other host for send mail....
Can you help me out.From this matter.
So i will thank full to you.

View Tutorial          By: jignesh at 2011-03-14 01:42:11
9. View Comment

i have an exception unknown host.
please tell me what to write in String host = "yourmailhost"; yourmailhost.

View Tutorial          By: saddam hussain at 2011-03-22 10:46:48
10. View Comment

I running this on localhost offline and getting error. and also String host="yourmailhost"
what should i write for this.

View Tutorial          By: nitisha at 2011-05-14 09:18:16
11. View Comment

mail goes as a single line statement, how to preserve the formatting of the description part in mail..??


View Tutorial          By: kishor at 2011-06-23 13:35:21
12. View Comment

thank you...

View Tutorial          By: Rojan at 2011-07-04 01:51:45
13. View Comment

hi frnds help me please .javamail program not been error its would executed but mail not b send . please explain any one .send me procedure or exact program . and i dont know about javamailhost. response here or to my mail

View Tutorial          By: sella at 2011-10-05 18:33:22
14. View Comment

In this code what should i add in place of "your mail host"?..

View Tutorial          By: manju at 2011-11-10 09:01:50
15. View Comment

simple but very usefull example, thanks a lot

View Tutorial          By: yiyo at 2011-12-01 15:18:43
16. View Comment

if i want to write my gmail address how can i provide authentication?

View Tutorial          By: asmcn at 2011-12-27 19:40:49
17. View Comment

Thanks for providing this page it works with localhost (also i used other email sending option and all working) but now problem is that after sending the email I am not getting the sent email on the destination address. where is it going ?????? that is my problem. plz do reply as i need it in my project immediately.
Thanks and regards
Dharam Jaswal

View Tutorial          By: Dharam Jaswal at 2012-02-02 13:19:24
18. View Comment

my host is not connect to SMTP host:, port: 25 error is comming.why?

View Tutorial          By: Anbuselvam at 2012-05-04 04:52:17
19. View Comment

haiiiiiiiii sir , i have error in this program, error notification are given below
Session mailSession = Session.getDefaultInstance(props, null);
session are resloved .......... pls help me

View Tutorial          By: Vinod at 2012-07-31 12:23:19
20. View Comment

The code runs successfully but, in actual the wasn't sent or received....why?

View Tutorial          By: Faisal at 2012-12-20 19:46:59
21. View Comment


View Tutorial          By: svk at 2013-01-17 19:59:05
22. View Comment

code givr an error when sending the Email:
Error:550 Invalid recipient:

please help me.

View Tutorial          By: Ravi Shanker Yadav at 2013-05-08 12:36:59
23. View Comment

i get error like this :
The requested resource (/contentselect/portal/send_mail.jsp) is not available.

i save that code with name "send_mail.jsp" next i call use button, but that not view .. please help


View Tutorial          By: Vera Yunita at 2013-05-20 03:57:18
24. View Comment

How to fix this Error.....

org.apache.jasper.JasperException: javax.servlet.ServletException: javax.mail.SendFailedException: Sending failed;
nested exception is:
class javax.mail.MessagingException: Could not connect to SMTP host: localhost, port: 25;
nested exception is:

View Tutorial          By: james prabhakaran at 2014-12-02 13:08:30
25. View Comment

org.apache.jasper.JasperException: An exception occurred processing JSP page /samplemail.jsp at line 42

39: // Hand the message to the default transport service
40: // for delivery.
42: Transport.send(msg);
43: out.println("Mail was sent to " + to);
44: out.println(" from " + from);
45: out.println(" using host " + host + ".");

View Tutorial          By: manikanta at 2015-02-02 05:39:09

Your name (required):

Your email(required, will not be shown to the public):

Your sites URL (optional):

Your comments:

More Tutorials by Vijay
What is dRuby?
Open and manipulate CSV files in Ruby
Sending Email using JSP

More Tutorials in JSP
LifecycleException: service.getName(): "Catalina"; Protocol handler start failed: ` Permission denied <null>:80
JSP Alert Example
JSP CheckBox Example
Uploading an Image to a Database using JSP
Uploading a file to a server using JSP
A JSP page that gets properties from a bean
The page Directive in JSP
The taglib, tag, include, attribute and the variable Directive in JSP
Declarations in JSP
Scriptlets and Expressions in JSP
Tag Libraries in JSP
The Request Object in JSP
The Response Object in JSP
The Out Object in JSP
The Session Object in JSP

More Latest News
Most Viewed Articles (in JSP )
What are the different scopes in JSP?
JSP Example to connect to MS SQL database and retrieve records
Sending Email using JSP
Automatically Refreshing a JSP
LifecycleException: service.getName(): "Catalina"; Protocol handler start failed: ` Permission denied <null>:80
Embedding java codes in jsp sciptlets
JSP Program for display Date
Enabling Expression Language Evaluation in JSP
The Request Object in JSP
JSP CheckBox Example
What is JSP?
Cookies using JSP or Java Bean
Click to Activate and Use this control
The JSP Program running first Time.
Most Emailed Articles (in JSP)
What is JSP?
Declaring variable in JSP
Techniques for form editing
A Simple Servlet Generating Plain Text
An Example Using Servlet Initialization and Page Modification Dates
Text Object
The Basic Syntax Expression Language in JSP
Disabling Scriptlets in JSP using web.xml
The BodyTag Interface in JSP
What are the different scopes in JSP?
Sending Email using JSP
Using a DataSource from WebLogic in a JSP
Getting HTTP Request Headers in a JSP