Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Mail Sending an email in JSP

Related Tutorials


 
 

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:

Advertisements

If you enjoyed this post then why not add us on Google+? Add us to your Circles



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

Related Tutorials

Discuss: Sending an email in JSP  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:41
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"
saurabh kumar
July 16, 2014
remarks

this code is not working error -must use starttls command 503.
kiran gv
August 14, 2014
How to attacth file

Can You please help me on how to attach files to this.
Swapnil
August 14, 2014
unable to send mail from a contact us page in html

i have developed a html contact form on through i want to send message to a single email id from different site visitor's, but i am unable to send mail from contact.html page, please help
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