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>



Archived Comments

1. I always used to read piece of writing in news
papers but now as I am a user of web so from

View Tutorial          By: fuck teen girl at 2017-05-26 02:08:55

2. What a crap. How will it owrk without passing your email password.
View Tutorial          By: amit at 2016-11-17 11:30:18

3. hlo sir,my project is online complaint registration. for this project i want to sent complaints to t
View Tutorial          By: athira at 2016-09-20 10:02:55

4. type Exception report

message An exception occurred processing JSP page /email.jsp at

View Tutorial          By: Adarsh at 2016-01-04 07:53:53

5. Really nice tutorial ...keep posting.
View Tutorial          By: Venkat at 2015-12-28 11:57:15

6. org.apache.jasper.JasperException: An exception occurred processing JSP page /samplemail.jsp at line
View Tutorial          By: manikanta at 2015-02-02 05:39:09

7. How to fix this Error.....

org.apache.jasper.JasperException: javax.servlet.ServletEx

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

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

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

9. code givr an error when sending the Email:
Error:550 Invalid recipient: [email protected]

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

10. nice!!!!
View Tutorial          By: svk at 2013-01-17 19:59:05

11. 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

12. haiiiiiiiii sir , i have error in this program, error notification are given below
Session ma

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

13. my host is 202.148.202.3.Could not connect to SMTP host: 202.148.202.3, port: 25 error is comming.w
View Tutorial          By: Anbuselvam at 2012-05-04 04:52:17

14. Thanks for providing this page it works with localhost (also i used other email sending option and a
View Tutorial          By: Dharam Jaswal at 2012-02-02 13:19:24

15. if i want to write my gmail address how can i provide authentication?
View Tutorial          By: asmcn at 2011-12-27 19:40:49

16. simple but very usefull example, thanks a lot
View Tutorial          By: yiyo at 2011-12-01 15:18:43

17. In this code what should i add in place of "your mail host"?..
View Tutorial          By: manju at 2011-11-10 09:01:50

18. hi frnds help me please .javamail program not been error its would executed but mail not b send . p
View Tutorial          By: sella at 2011-10-05 18:33:22

19. thank you...
View Tutorial          By: Rojan at 2011-07-04 01:51:45

20. 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

21. I running this on localhost offline and getting error. and also String host="yourmailhost"
View Tutorial          By: nitisha at 2011-05-14 09:18:16

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

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

23. very good... this code is very useful for me.
View Tutorial          By: shalini at 2011-03-16 06:16:37

24. very good... this code is very useful for me.
View Tutorial          By: shalini at 2011-03-16 06:15:58

25. dear sir ,
How can i create my smtp or host server,or can i use any other host for send mail.

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

26. i tried, the coding dont hv any errors but its not send any email not sending to inbox... i dont kno
View Tutorial          By: anna at 2010-11-02 21:56:27

27. thank you lolz
View Tutorial          By: felt at 2010-07-11 22:35:11

28. Many thanks
View Tutorial          By: tushar at 2010-03-10 01:27:04

29. while run program i got this error
The server encountered an internal error () that prevented

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

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

Please help.

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

31. Hi lolz,
In order for this code to work, you should have a SMTP server and in your code you h

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

32. lolz what's the code not working in JSP may be sending command is the servlet class... Not work at a
View Tutorial          By: lolz at 2008-12-10 03:31:50

33. hi
View Tutorial          By: kiran at 2008-10-27 04:39:17

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

I need INTRANET CHAT

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


Most Viewed Articles (in JSP )

Latest Articles (in JSP)

Comment on this tutorial