0

i'm trying to invoke a java function on a jsp page. the function is supposed to send a mail by demand.

this is the java code:

package s;

import java.io.UnsupportedEncodingException;
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.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

        public class SendMail {

            public static void send() {

                final String username = "";
                final String password = "";

                Properties props = new Properties();
                props.put("mail.smtp.starttls.enable", "true");
                props.put("mail.smtp.auth", "true");
                props.put("mail.smtp.host", "smtp.gmail.com");
                props.put("mail.smtp.port", "587");

                Session session = Session.getInstance(props,
                  new javax.mail.Authenticator() {
                    protected PasswordAuthentication getPasswordAuthentication() {
                        return new PasswordAuthentication(username, password);
                    }
                  });

                try {

                    Message message = new MimeMessage(session);
                    message.setFrom(new InternetAddress("shov.rz@gmail.com"));
                    message.setRecipients(Message.RecipientType.TO,
                        InternetAddress.parse("shov.rz@gmail.com"));
                    message.setSubject("Testing Subject");
                    message.setText("Dear Mail Crawler,"
                        + "\n\n No spam to my email, please!");

                    Transport.send(message);

                    System.out.println("Done");

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

    }

and i'm invoking it on a jsp page:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <%@import page="s.SendMail"%>
    <%
    s.SendMail.send();
    %>
    <html>
<head>
</head>
</html>

it doesn't compile and the eclipse throws an exception: org.apache.jasper.JasperException what am i doing wrong? thank's for the help!

  • Since you import `s.SendMail`, you do not need to prefix with `s` anymore: `SendMail.send()` is enough. If this doesn't work, then no idea. – fge Jun 05 '13 at 19:36
  • it didn't work... i have the same problem – user2445729 Jun 05 '13 at 19:44
  • take a look on [this question](http://stackoverflow.com/questions/239147/how-do-you-import-classes-in-jsp) – Quirin Jun 05 '13 at 19:46
  • the import is done currectly. if i replace the send function with another function - it works. i have trouble in invoking this specific method. – user2445729 Jun 05 '13 at 19:52
  • Could you post output, a stack trace or something? – Quirin Jun 05 '13 at 19:53
  • Regarding to [this post](http://community.jaspersoft.com/questions/523793/how-solve-orgapachejasperjasperexception-javalangnullpointerexception) I suspect a NullPointerException in your send() implementation, so check all your references (variables) which one is null. – Quirin Jun 05 '13 at 19:59
  • org.apache.jasper.JasperException: An exception occurred processing JSP page /reww.jsp at line 4 1: <%@ page language="java" contentType="text/html; 2: pageEncoding=UTF-8"%> 3: <%@page import="s.SendMail" %> 4: <%SendMail.send();%> 5: – user2445729 Jun 06 '13 at 19:50
  • Stacktrace: org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:505) org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:398) org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:342) org.apache.jasper.servlet.JspServlet.service(JspServlet.java:267) javax.servlet.http.HttpServlet.service(HttpServlet.java:717) – user2445729 Jun 06 '13 at 19:53

1 Answers1

1

The 'page' and 'import' terms are the wrong way round. Should be:

    <%@ page import="s.SendMail" %>
Simon Curd
  • 790
  • 5
  • 12