Home Mail Sending an email in JSP
Questions:Ask|Latest


 
 

Share on Google+Share on Google+

Sending an email in JSP

Advertisement
Sending an email in JSP

Sending an email in JSP

     

Sending an email in JSP

In this section, you will learn how to send an email in jsp.  

Following is a simple JSP page for sending an email using javax mail APIs. The first jsp page that displays an email form containing the to, from addresses, subject and its descriptions. A "Send Mail" command button when you click that button then it will send your given information to from address. 

Note: You must be need mail.jar or activation.jar files in your applications. Whenever you could not be hold in your "WEB-INF/lib" then your application is not word perfectly and displays an error. 

Here the code of JSP:  mailAPI.jsp

<html>
<head>
<title>Mail API</title>
</head>
<body>
<table border="1" width="50%"  cellpadding="0" cellspacing="0">
  <tr>
  <td width="100%">
  <form method="POST" action="mail.jsp">
  <table border="1" width="100%" cellpadding="0" cellspacing="0">
  <h1>Mail API</h1>
  <tr>
  <td width="50%"><b>To:</b></td>
  <td width="50%"><input type="text" name="to" size="30"></td>
  </tr>
  <tr>
  <td width="50%"><b>From:</b></td>
  <td width="50%"><input type="text" name="from" size="30"></td>
  </tr>
  <tr>
  <td width="50%"><b>Subject:</b></td>
  <td width="50%"><input type="text" name="subject" size="30"></td>
  </tr>
  <tr>
  <td width="50%"><b>Description:</b></td>
  <td width="50%"><textarea name="description" type="text" 
cols="40" rows="15" size=100>
  </textarea>
  </td>
  </tr>
  <tr>
  <td><p><input type="submit" value="Send Mail" name="sendMail"></td>
  </tr>
  </table>
  </p>
  </form>
  </td>
  </tr>
</table>
</body>
</html>

 Here the code of JSP:  mail.jsp

<%page language="java" import="javax.naming.*,java.io.*,javax.mail.*,
javax.mail.internet.*,com.sun.mail.smtp.*"
%>
<html>
<head>
<title>Mail</title>
</head>
<body>
<%
try{
  Session mailSession = Session.getInstance(System.getProperties());
  Transport transport = new SMTPTransport(mailSession,new URLName("localhost"));
  transport.connect("localhost",25,null,null);
  MimeMessage m = new MimeMessage(mailSession);
  m.setFrom(new InternetAddress(%><%request.getParameter("from")%><%));
  Address[] toAddr = new InternetAddress[] {
  new InternetAddress(%><%request.getParameter("to")%><%)
  };
  m.setRecipients(javax.mail.Message.RecipientType.TO, toAddr );
  m.setSubject(%><%request.getParameter("subject")%><%);
  m.setSentDate(new java.util.Date());
  m.setContent(%><%request.getParameter("description")%><%, "text/plain");
  transport.sendMessage(m,m.getAllRecipients());
  transport.close();
  out.println("Thanks for sending mail!");
}
catch(Exception e){
  out.println(e.getMessage());
  e.printStackTrace();
}
%>
</body>
</html>

Output:

After running this application you get:

When you click "Send mail" Then you get:

Fill the following details and click the "Send Mail" command button:

Then you get:

Advertisement

Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: April 18, 2011

Ask Questions?    Discuss: Sending an email in JSP  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
bharadwaj
June 23, 2011
regarding sending mail through JSP

Thanks in advance Sir i am getting an error when i compile the above mentioned program. It is saying that it is unable to connect to SMTP host. What should I do...
Lakshmi Kanth
June 5, 2013
dispalying error message

the code is runnig but dispalying the message Could not connect to SMTP host: localhost, port: 25
Shibu
February 7, 2013
SHOWING ERROR

On executing send mail its showing "Could not connect to SMTP host: localhost, port: 25" PLS HELP
seema joshi
February 9, 2013
error in sending email

Could not connect to SMTP host: localhost, port: 25
susma
February 25, 2013
reporting error

I am getting lot of error
fdsfd
December 28, 2011
Hi this code in not working

Hi this code in not working.It is always showing Invalid Address.
inayat
January 13, 2012
error

i got this error after click on sendmail button 530 5.7.0 Must issue a STARTTLS command first. uc6sm13165942igb.4 thanks...
siddharth
January 23, 2012
getting error while trying this page.

Please note that i get this error while i am running this code. 530 5.7.0 Must issue a STARTTLS command first after that i have added the fields props.put("mail.smtp.starttls.enable","true");
Sumit vedi
February 18, 2012
please help me

it is not working. i got an error: "Could not connect to SMTP host: localhost, port: 25"
theja
March 3, 2012
code for it service help desk project

rey pls send the code for my project it service help desk
uma
March 17, 2012
cant send mail using this program

hai i tried executing this program but whenever i execute it it shows Could not connect to SMTP host: localhost, port: 25. please help me
anonymus
March 21, 2012
error

Error occurred when run these files. Could not connect to SMTP host: localhost, port: 25; nested exception is: java.net.ConnectException: Connection refused: no further information
anonymus
March 21, 2012
error

Error in this code. Could not connect to SMTP host: localhost, port: 25; nested exception is: java.net.ConnectException: Connection refused: no further information
chetan
March 31, 2012
mail sending

good email service develop
Anbuselvam.R
May 4, 2012
error

I found the folloing error "SMTPTransport cannot be resolved to a type" i do not know why?
sachin sharma
May 5, 2012
connectivity for smtp

how we connect smtp server to source code plz give answer.its very urgent
vikas
May 10, 2012
it is not working.

What alteration should I do in order to send mail from my server.
suneesh saleen
May 22, 2012
need jar file

Some one pls tel me wr i can find ta jar file ?????
Sujay Halder
May 23, 2012
Email sending

getting error---- Could not connect to SMTP host: localhost, port: 25; nested exception is: java.net.ConnectException: Connection refused: connect
Dharam
May 29, 2012
Sending Email through JSP

Hi The code provided here is not working in the specified waly as the message "Thanks for sending mail " comes out but no mail is sent .What's wrong with ?????.Plz do response Thanks and regards dharam jaswal
Ramesh Hanumanthu
June 21, 2012
java code for verification email

thanks for the given code. sir iam not getting my description in my mail. how it is posible plz help me
Nani
July 27, 2012
error

I did as per the instructions given but iam stuck with the error:"cannot connect to smtp port number:24 response:-1"
Subhash Jadhav
August 28, 2012
how to connect smtp

my code is "sending an email in jsp" gives message Could not connect to SMTP host: localhost, port: 25
siva kumar
September 6, 2012
I used the above given code: got the error

I am getting this error please help me soon. "Could not connect to SMTP host: localhost, port: 8080, response: -1"
Abhijeet Pardeshi
September 22, 2012
Error in code

code works but sends two copies of mail to each recipients
dattu
December 6, 2012
connection problum

Could not connect to SMTP host: localhost, port: 25; nested exception is: java.net.ConnectException: Connection refused: connect
Nitasha Gupta
December 9, 2012
pls solve my prob. its urgent

when i try this code then the error occur "Could not connect to SMTP host: localhost, port: 25 " How I resolve this prob pls tell me its urgent :( I am really thankfull to u
Danniel Sison
October 3, 2012
SMTP error

Hi, I'm getting an error "Could not connect to SMTP host: localhost, port: 25 " any idea what should I do with this?
pandu
December 22, 2012
java mail

Unable to compile class for JSP: i am getting the above error when trying to send a mail
ganesh
October 23, 2012
code problem..........

Could not connect to SMTP host: localhost, port: 25
Meenakshi Goyal
November 18, 2012
error in sending mail

I am getting error on session type can not be resolved,I have copied the same code and in build path-configurebuildpath-add external jar files-activationmail.jar,mail.jar are added still i am getting errors could you please help me
Jasleen Kaur
May 9, 2013
error in code mail.jsp

hii i am getting an error in mail.jsp i.e. Session cannot be resolved to a type.. what is the problem??
masoud
June 26, 2013
port

hi, its port is not working and it is not correct
masoud
June 26, 2013
port

its port is not working and it is false
sankar narayanan
August 11, 2013
could not connect

Dear i executed the above coding using NetBean 7.3.1 show this Couldn't connect to host, port: http://localhost:8084, 25; timeout -1 thanks and regards sankar
Rushikesh k
October 25, 2013
showing error

it is not working. i got an error: "Could not connect to SMTP host: localhost, port: 25"
mani
August 29, 2011
hai

hia
Rashid MV
November 13, 2011
Mail sending

i have run a program to send mail through java which i got from rose india but it is not working properly. It is showing the message as follows.. 'Could not connect to SMTP host: localhost, port: 25' Please help. Thanks in advance
DMCA.com