Sending Email using JSP
By: Vijay
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.
<html>
<head>
<title>JSP JavaMail Example </title>
</head>
<body>
<%@ 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("mail.host", 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.
mailSession.setDebug(sessionDebug);
// 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.setSubject(subject);
msg.setSentDate(new Date());
msg.setText(messageText);
// Hand the message to the default transport service
// for delivery.
Transport.send(msg);
out.println("Mail was sent to " + to);
out.println(" from " + from);
out.println(" using host " + host +
".");
%>
</table>
</body>
</html>
Comment on this tutorial
- Data Science
- Android
- AJAX
- ASP.net
- C
- C++
- C#
- Cocoa
- Cloud Computing
- HTML5
- Java
- Javascript
- JSF
- JSP
- J2ME
- Java Beans
- EJB
- JDBC
- Linux
- Mac OS X
- iPhone
- MySQL
- Office 365
- Perl
- PHP
- Python
- Ruby
- VB.net
- Hibernate
- Struts
- SAP
- Trends
- Tech Reviews
- WebServices
- XML
- Certification
- Interview
categories
Subscribe to Tutorials
Related Tutorials
Archived Comments
1. Thank you for the script.
I was searching f
View Tutorial By: MUhammad Faizal at 2008-04-10 08:23:54
2. hi
View Tutorial By: kiran at 2008-10-27 04:39:17
3. lolz what's the code not working in JSP may be sen
View Tutorial By: lolz at 2008-12-10 03:31:50
4. Hi lolz,
In order for this code to work, yo
View Tutorial By: Vijay at 2008-12-10 19:22:04
5. I get an error " javax.mail" does not ex
View Tutorial By: bhavin at 2009-02-03 05:50:08
6. while run program i got this error
The serv
View Tutorial By: jai at 2009-09-04 04:38:39
7. Many thanks
View Tutorial By: tushar at 2010-03-10 01:27:04
8. thank you lolz
View Tutorial By: felt at 2010-07-11 22:35:11
9. i tried, the coding dont hv any errors but its not
View Tutorial By: anna at 2010-11-02 21:56:27
10. dear sir ,
How can i create my smtp or host
View Tutorial By: jignesh at 2011-03-14 01:42:11
11. very good... this code is very useful for me.
View Tutorial By: shalini at 2011-03-16 06:15:58
12. very good... this code is very useful for me.
View Tutorial By: shalini at 2011-03-16 06:16:37
13. i have an exception unknown host.
please te
View Tutorial By: saddam hussain at 2011-03-22 10:46:48
14. I running this on localhost offline and getting er
View Tutorial By: nitisha at 2011-05-14 09:18:16
15. mail goes as a single line statement, how to prese
View Tutorial By: kishor at 2011-06-23 13:35:21
16. thank you...
View Tutorial By: Rojan at 2011-07-04 01:51:45
17. hi frnds help me please .javamail program not bee
View Tutorial By: sella at 2011-10-05 18:33:22
18. In this code what should i add in place of "y
View Tutorial By: manju at 2011-11-10 09:01:50
19. simple but very usefull example, thanks a lot
View Tutorial By: yiyo at 2011-12-01 15:18:43
20. if i want to write my gmail address how can i prov
View Tutorial By: asmcn at 2011-12-27 19:40:49
21. Thanks for providing this page it works with local
View Tutorial By: Dharam Jaswal at 2012-02-02 13:19:24
22. my host is 202.148.202.3.Could not connect to SMT
View Tutorial By: Anbuselvam at 2012-05-04 04:52:17
23. haiiiiiiiii sir , i have error in this program, er
View Tutorial By: Vinod at 2012-07-31 12:23:19
24. The code runs successfully but, in actual the wasn
View Tutorial By: Faisal at 2012-12-20 19:46:59
25. nice!!!!
View Tutorial By: svk at 2013-01-17 19:59:05
26. code givr an error when sending the Email:
View Tutorial By: Ravi Shanker Yadav at 2013-05-08 12:36:59
27. i get error like this :
The requested resou
View Tutorial By: Vera Yunita at 2013-05-20 03:57:18
28. How to fix this Error.....
org.apac
View Tutorial By: james prabhakaran at 2014-12-02 13:08:30
29. org.apache.jasper.JasperException: An exception oc
View Tutorial By: manikanta at 2015-02-02 05:39:09
30. Really nice tutorial ...keep posting.
View Tutorial By: Venkat at 2015-12-28 11:57:15
31. type Exception report
message An ex
View Tutorial By: Adarsh at 2016-01-04 07:53:53
32. hlo sir,my project is online complaint registratio
View Tutorial By: athira at 2016-09-20 10:02:55
33. What a crap. How will it owrk without passing your
View Tutorial By: amit at 2016-11-17 11:30:18
34. I always used to read piece of writing in news
View Tutorial By: fuck teen girl at 2017-05-26 02:08:55