Search code examples
jspjakarta-mail

Error in a psp program


I am working on a project in jsp and I am getting error in the following line, here is the complete program. The error is in line

Session session = Session.getInstance(props, auth);

The error is: Duplicate local variable "session". Please help me fix this error.

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ page import = "com.mail.Restaurant.EmailUtil" %>
<%@ page import=java.util.Properties,javax.*,javax.mail.Authenticator,
javax.mail.PasswordAuthentication,javax.mail.Session %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
"http://www.w3.org  /TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<%
   final String Message = request.getParameter("Message");
   final String Name = request.getParameter("fname");
   final String Messageowner= ("Hi"+Name+","+"\n\n"+
   "You will be contactedshortly\n    \n\n\n Regards,\n xyz.com");
   final String fromEmail = "[email protected]"; //requires valid gmail id
   final String TID = request.getParameter("email");
   final String password = "password"; // correct password for gmail id
   final String OwnerEmail = "[email protected]"; // can be any email id 
   final String Ownersubject=("Request from"+Name) ;
   System.out.println("TLSEmail Start");
   Properties props = new Properties();
   props.put("mail.smtp.host", "smtp.gmail.com"); //SMTP Host
   props.put("mail.smtp.port", "587"); //TLS Port
   props.put("mail.smtp.auth", "true"); //enable authentication
   props.put("mail.smtp.starttls.enable", "true"); //enable STARTTLS

           //create Authenticator object to pass in Session.getInstance argument
   Authenticator auth = new Authenticator() {
       //override the getPasswordAuthentication method
       protected PasswordAuthentication getPasswordAuthentication() {
           return new PasswordAuthentication(fromEmail, password);
       }
   };
   Session session = Session.getInstance(props, auth);

   EmailUtil.sendEmail(session, OwnerEmail,"Request from a Person", Message);
   EmailUtil.sendEmail(session, TID,"Restaurant.com", Messageowner);
   %>
   </body>
   </html>

Solution

  • Jsp has a set of predefined values and "session" is one of them. If you set session="false" in the header tag, the jsp-compiler should not include own session variable declaration.