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.

Results 1 to 12 of 12

Thread: SEND MAIL IN JSP

  1. #1
    Junior Member
    Join Date
    Jan 2014
    Posts
    23
    My Mood
    Mellow
    Thanks
    0
    Thanked 1 Time in 1 Post

    Question SEND MAIL IN JSP

    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//


  2. #2
    Member
    Join Date
    Oct 2013
    Location
    United Kingdom
    Posts
    62
    Thanks
    1
    Thanked 4 Times in 4 Posts

    Default Re: SEND MAIL IN JSP

    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

  3. #3
    Junior Member
    Join Date
    Jan 2014
    Posts
    23
    My Mood
    Mellow
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: SEND MAIL IN JSP

    ya i knw about java mail api
    and i also added .jar files to my lib..
    but still i m facing problem ..

  4. #4
    Member andbin's Avatar
    Join Date
    Dec 2013
    Location
    Italy
    Posts
    443
    Thanks
    4
    Thanked 122 Times in 114 Posts

    Default Re: SEND MAIL IN JSP

    Quote Originally Posted by Praful Chougale View Post
    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//
    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.netSCJP 5 (91%) – SCWCD 5 (94%)

    Useful links for Java beginnersMy new project Java Examples on Google Code

  5. #5
    Junior Member
    Join Date
    Jan 2014
    Posts
    23
    My Mood
    Mellow
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: SEND MAIL IN JSP

    the same link i had referred but its not working.

  6. #6
    Member
    Join Date
    Oct 2013
    Location
    United Kingdom
    Posts
    62
    Thanks
    1
    Thanked 4 Times in 4 Posts

    Default Re: SEND MAIL IN JSP

    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

  7. #7
    Junior Member
    Join Date
    Jan 2014
    Posts
    23
    My Mood
    Mellow
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: SEND MAIL IN JSP

    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.

  8. #8
    Member andbin's Avatar
    Join Date
    Dec 2013
    Location
    Italy
    Posts
    443
    Thanks
    4
    Thanked 122 Times in 114 Posts

    Default Re: SEND MAIL IN JSP

    Quote Originally Posted by Praful Chougale View Post
    This is my JSP
    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.netSCJP 5 (91%) – SCWCD 5 (94%)

    Useful links for Java beginnersMy new project Java Examples on Google Code

  9. #9
    Junior Member
    Join Date
    Jan 2014
    Posts
    23
    My Mood
    Mellow
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: SEND MAIL IN JSP

    I m Using JAVA SE 7 update 51

    and netbeans 7.4

    does it require the jar files to add to lib???

  10. #10
    Member andbin's Avatar
    Join Date
    Dec 2013
    Location
    Italy
    Posts
    443
    Thanks
    4
    Thanked 122 Times in 114 Posts

    Default Re: SEND MAIL IN JSP

    Quote Originally Posted by Praful Chougale View Post
    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.netSCJP 5 (91%) – SCWCD 5 (94%)

    Useful links for Java beginnersMy new project Java Examples on Google Code

  11. #11
    Junior Member
    Join Date
    Jan 2014
    Posts
    23
    My Mood
    Mellow
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: SEND MAIL IN JSP

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

  12. #12
    Junior Member
    Join Date
    Jan 2014
    Posts
    23
    My Mood
    Mellow
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: SEND MAIL IN JSP

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

    Quote Originally Posted by Praful Chougale View Post
    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 rel="nofollow" href="http://192.168.123.101:8080/WebApplication2/index.html/contact.html"><input type="submit" value="BACK" /></a></center>
    </h1>
    <br /><div style="z-index:3" class="shade" align="center"></div>
    </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>
    this is working code

Similar Threads

  1. [SOLVED] Unable to send a mail with attachment
    By Ragavarajan in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 25th, 2012, 09:05 AM
  2. Mail Send Module Not Working
    By hashitagarwal in forum Java SE APIs
    Replies: 1
    Last Post: September 14th, 2012, 10:25 AM
  3. How to Send emails from Google Mail using JavaMail API
    By JavaPF in forum Java SE API Tutorials
    Replies: 3
    Last Post: February 26th, 2012, 04:21 PM
  4. Send Scanned File to E-mail
    By babywiz21 in forum Java Theory & Questions
    Replies: 1
    Last Post: April 2nd, 2011, 08:40 PM
  5. How to Send emails from Google Mail using JavaMail API
    By JavaPF in forum Java Code Snippets and Tutorials
    Replies: 1
    Last Post: November 14th, 2010, 11:00 AM

Tags for this Thread