0

i am creating a wepage where i want to send a mail using gmail smtp.i tried with jsp page but i did not work so ihave craeted a code in core java which is successfully sending email.now i want to use this java code in my jsp page .i tried i but got errors

java code:

import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class SSL {
    public static void main(String [] args){
        Properties props = new Properties();
        props.put("mail.smtp.host", "smtp.gmail.com");
        props.put("mail.smtp.socketFactory.port", "465");
        props.put("mail.smtp.socketFactory.class",
                "javax.net.ssl.SSLSocketFactory");
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.port", "465");

        Session session = Session.getDefaultInstance(props,
            new javax.mail.Authenticator() {
                protected PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication("prakash.d2222","password");
                }
            });

        try {

            Message message = new MimeMessage(session);
            message.setFrom(new InternetAddress("from@no-spam.com"));
            message.setRecipients(Message.RecipientType.TO,
                    InternetAddress.parse("prakash_d22@rediffmail.com"));
            message.setSubject("hi");
            message.setText("12345" +
                    "\n\n No spam to my email, please!");

            Transport.send(message);

            System.out.println("Done");

        } catch (MessagingException e) {
            throw new RuntimeException(e);
        }
    }
}

my jsp code

<%@ page contentType="text/html; charset=iso-8859-1" language="java" import="java.sql.*" errorPage="" %>

                <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

                <html>
                <body>
                <jsp:useBean id="link" scope="application" class = "SSL.class" />
<jsp:setProperty name="link" property="*" />

                </body>
                </html>

and error shown is

type Exception report

message

description The server encountered an internal error () that prevented it from fulfilling this request.

exception

org.apache.jasper.JasperException: /pizza/page/ssl.jsp(7,4) The value for the useBean class attribute SSL.class is invalid.
    org.apache.jasper.compiler.DefaultErrorHandler.jspError(DefaultErrorHandler.java:40)
    org.apache.jasper.compiler.ErrorDispatcher.dispatch(ErrorDispatcher.java:407)
    org.apache.jasper.compiler.ErrorDispatcher.jspError(ErrorDispatcher.java:148)
    org.apache.jasper.compiler.Generator$GenerateVisitor.visit(Generator.java:1233)
    org.apache.jasper.compiler.Node$UseBean.accept(Node.java:1178)
    org.apache.jasper.compiler.Node$Nodes.visit(Node.java:2361)
    org.apache.jasper.compiler.Node$Visitor.visitBody(Node.java:2411)
    org.apache.jasper.compiler.Node$Visitor.visit(Node.java:2417)
    org.apache.jasper.compiler.Node$Root.accept(Node.java:495)
    org.apache.jasper.compiler.Node$Nodes.visit(Node.java:2361)
    org.apache.jasper.compiler.Generator.generate(Generator.java:3459)
    org.apache.jasper.compiler.Compiler.generateJava(Compiler.java:231)
    org.apache.jasper.compiler.Compiler.compile(Compiler.java:354)
    org.apache.jasper.compiler.Compiler.compile(Compiler.java:334)
    org.apache.jasper.compiler.Compiler.compile(Compiler.java:321)
    org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:592)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:328)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:717

so please help me as i am new to jsp.

prakash_d22
  • 1,153
  • 5
  • 21
  • 34

3 Answers3

3

Consider following suggestions:

  1. .class files must be placed under WEB-INF/classes
  2. Never create a class in default package.

package com.me;

public class SSL { 
    public void show(){
         ///
    }
}

And I can't see any getter/setter method in your class so no need to use <jsp:setProperty/> action in jsp page and don't include extension to class attribute.

<jsp:useBean id="link" scope="application" class="com.me.SSL" />

EDIT:

If you're NOT using IDE (netbeans/eclipse) then you must have to create folder structure under /tomcat x.x/webapps/.

/webapp   <--- This is known as `context` folder
|
|-------/WEB-INF
|       |
|       |-----------/classes
|       |                  |---/com/me/SSL.class
|       | 
|       |-----------/lib
|                    mail.jar
| sample.jsp

You have to call show() method in JSP page:

<jsp:useBean id="link" scope="application" class="com.me.SSL" />
<%
  link.show();
%> 

OR

<%
   com.me.SSL obj=new com.me.SSL();
   obj.show();
%>
KV Prajapati
  • 93,659
  • 19
  • 148
  • 186
1

Firslt modify your class SSL

public class SSL {

 public  void SendMail(){
        Properties props = new Properties();
        props.put("mail.smtp.host", "smtp.gmail.com");
        props.put("mail.smtp.socketFactory.port", "465");
        props.put("mail.smtp.socketFactory.class",
                "javax.net.ssl.SSLSocketFactory");
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.port", "465");

        Session session = Session.getDefaultInstance(props,
            new javax.mail.Authenticator() {
                protected PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication("prakash.d2222","password");
                }
            });

        try {

            Message message = new MimeMessage(session);
            message.setFrom(new InternetAddress("from@no-spam.com"));
            message.setRecipients(Message.RecipientType.TO,
                    InternetAddress.parse("prakash_d22@rediffmail.com"));
            message.setSubject("hi");
            message.setText("12345" +
                    "\n\n No spam to my email, please!");

            Transport.send(message);

            System.out.println("Done");

        } catch (MessagingException e) {
            throw new RuntimeException(e);
        }
    }
}

Just make a custom bean class that will work with you class SSL and name it like SSLImpelmenter

where your class is

public class SSLImpelmenter
{
   private SSL objSSL = new SSL();

   //getter setter methods for objSSL
}

Now in your JSP

//Add import for SSL Class
//Now Use useBean tag
<jsp:useBean id="link" scope="application" class = "SSLImpelmenter" />

SSL objSSLJSP = link.getObjSSL(); 
objSSLJSP.SendMail();
Jayant Varshney
  • 1,765
  • 1
  • 25
  • 42
0

The immediate problem is that you are using a filename instead of a class name in the "class=..." attribute. Since your class is declared in the default package (because you don't have a package declaration in your class), you should write the useBean as:

<jsp:useBean id="link" scope="application" class="SSL" />

And if you haven't done so already, you should make sure that the "SSL.class" file is on the classpath for your webapp. For instance, it could be in the "/WEB-INF/classes/" directory, or it could be in a JAR file in "/WEB-INF/lib/".

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216