I want to send email from my jsp file ..and i have a html code which contains form and inputs.
how can i send the email ??????
and i m using netbeans//
Welcome to the Java Programming Forums
The professional, friendly Java community. 21,500 members and growing!
The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.
>> REGISTER NOW TO START POSTING
Members have full access to the forums. Advertisements are removed for registered users.
I want to send email from my jsp file ..and i have a html code which contains form and inputs.
how can i send the email ??????
and i m using netbeans//
Do you have any idea about Java Mail Service. Please go through it (you can search in website or can get nice e-books). Kindly let me know if you face any issues.
Thanks and regards,
Sambit Swain
ya i knw about java mail api
and i also added .jar files to my lib..
but still i m facing problem ..
Sending an email has, by itself, nothing to do with JSPs. In Java there is the well known JavaMail API.
See here for example: Java - Sending Email using JavaMail API
Andrea, www.andbin.net — SCJP 5 (91%) – SCWCD 5 (94%)
Useful links for Java beginners – My new project Java Examples on Google Code
the same link i had referred but its not working.
Can we have the code please? Are you getting any error message? Please post the information you have so that we can help more regarding this
Thanks and regards,
Sambit Swain
This is my JSP
<%--
Document : mail
Created on : Jan 22, 2014, 3:52:38 PM
Author : xyz
--%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ page import="java.io.*,java.util.*,javax.mail.*"%>
<%@ page import="javax.mail.internet.*,javax.activation.*"% >
<%@ page import="javax.servlet.http.*,javax.servlet.*" %>
<%
String result;
String to = "vjkanade@yahoo.com";
String from = "prafulchougale@hotmail.com";
String host = "localhost";
Properties properties = System.getProperties();
properties.setProperty("mail.smtp.host", host);
Session mailSession = Session.getDefaultInstance(properties);
try
{
MimeMessage message = new MimeMessage(mailSession);
message.setFrom(new InternetAddress(from));
message.addRecipient(Message.RecipientType.TO,new InternetAddress(to));
message.setSubject("This is the Subject Line!");
message.setContent("<h1>This messageis actual </h1>","text/html" );
Transport.send(message);
result = "Succesfully send the message....";
}
catch (MessagingException mex)
{
mex.printStackTrace();
out.print("<h1>"+mex.getMessage()+"</h1>");
result = "Sending failed....";
}
%>
<html>
<head>
<title>Send HTML Email using JSP</title>
</head>
<body>
<center>
<h1>Send Email using JSP</h1>
</center>
<p align="center">
<%
out.println("Result: " + result + "\n");
%>
</p>
</body>
</html>
--- Update ---
This is my html file
<form id="contact-form" method="post" action="mail.jsp">
<input type="text" name="name" placeholder="Name
<input type="text" name="email" placeholder="Email
<input type="text" name="subject" placeholder="Subject
<textarea name="message"></textarea>
<input type="submit" class="button" name="submit" value="send">
</form>
--- Update ---
mail is not been sent that is the problem.
First, in general it's a very bad thing to have all that code into the JSP. At least create a Java class with a method (even static method) and call it from the JSP.
Second, have you downloaded the required jar(s) and put them under WEB-INF/lib ?
(note that if you are using at least Java 6, the JAF is already included and you don't need an external jar for JAF)
Third, does your SMTP require authentication?
Fouth, if you have any compilation error/runtime exception, tell us.
Andrea, www.andbin.net — SCJP 5 (91%) – SCWCD 5 (94%)
Useful links for Java beginners – My new project Java Examples on Google Code
I m Using JAVA SE 7 update 51
and netbeans 7.4
does it require the jar files to add to lib???
Yes, unless they are provided by the servlet container/application server.
Since you are using Java 7, you need only the JavaMail API jar. Please, see here: https://java.net/projects/javamail/pages/Home
Andrea, www.andbin.net — SCJP 5 (91%) – SCWCD 5 (94%)
Useful links for Java beginners – My new project Java Examples on Google Code
yes i have added the required jar files to lib. and the above code showing the exception as..
Couldn't connect to host, port: localhost, 25; timeout -1
Send Email using JSP
Result: Sending failed....
yes, got it.. the mail has been sent...
i changed my whole code..
please check whether the code is of world class.
Thank you.. all
Suggest me if there are any changes.? but code is working..
the jsp file
<%--
Document : mailJSP
Created on : Jan 24, 2014, 11:58:16 AM
Author : xyz
--%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ page import="java.util.*,javax.mail.*"%>
<%@ page import="javax.mail.internet.*" %>
<%
//Creating a result for getting status that messsage is delivered or not!
String result;
// Get recipient's email-ID, message & subject-line from index.html page
final String to = "vijay@brainchamber.net";
final String subject = request.getParameter("subject");
String messg="\n\n\nName: "+request.getParameter("name");
messg=messg+"\n\n\nMessage: "+ request.getParameter("message");
messg=messg+"\n\n\nEmail Id: "+request.getParameter("email");
// Sender's email ID and password needs to be mentioned
final String from = "xyz@gmail.com";
//added my email id and password..
final String pass = "abc";
// Defining the gmail host
String host = "smtp.gmail.com";
// Creating Properties object
Properties props = new Properties();
// Defining properties
props.put("mail.smtp.host", host);
props.put("mail.transport.protocol", "smtp");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.user", from);
props.put("mail.password", pass);
props.put("mail.port", "465");
// Authorized the Session object.
Session mailSession = Session.getInstance(props, new javax.mail.Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(from, pass);
}
});
try {
// Create a default MimeMessage object.
MimeMessage message = new MimeMessage(mailSession);
// Set From: header field of the header.
message.setFrom(new InternetAddress(from));
// Set To: header field of the header.
message.addRecipient(Message.RecipientType.TO,new InternetAddress(to));
// Set Subject: header field
message.setSubject(subject);
// Now set the actual message
message.setText(messg);
// Send message
Transport.send(message);
result = "Your mail sent successfully....";
} catch (MessagingException mex) {
mex.printStackTrace();
result = "Error: unable to send mail....";
}
%>
<title>School Chartered || Contact</title>
</head>
<body>
<h1>
<b><center><font color="red"><% out.println(result);%></b><br>
<a href="http://192.168.123.101:8080/WebApplication2/index.html/contact.html"><input type="submit" value="BACK" /></a></center>
</h1>
</body>
</html>
--- Update ---
the html file contains.
<form id="contact-form" method="post" action="SendMail.jsp">
<input type="text" name="name" placeholder="Name
<input type="text" name="email" placeholder="Email
<input type="text" name="subject" placeholder="Subject
<textarea name="message"></textarea>
<input type="submit" class="button" name="submit" value="send">
</form>
--- Update ---
this is working code