Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Sending email with message body contain data in table displayed in one

802198Aug 14 2008 — edited Aug 16 2008
Hi,
I have one jsp page where I am dispalying the table data retrieved fron database.
Now i am trying to send this table as message body of the mail to the user.I am doing same stuff last from six days but unable to send such data.
Please help me Sir,as i am newbie here.
Here is an jsp page.
    Document   : evaluationeventtable
    Created on : Jul 24, 2008, 6:52:37 PM
    Author     : user1
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ page language ="java" %>
<%@ page import="java.sql.*, javax.sql.*, javax.naming.*,java.io.*,java.util.*" %>
<!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=UTF-8">
        <title>IGIDR</title>
        <link rel="stylesheet" href="../styles/styles.css" type="text/css"> 
    </head>
    <body>
         <table width="100%" cellspacing="0" cellpadding="0" border="0">
<tr>
<td><%@ include file="/includes/logohead.jsp" %></td>
</tr> 
<tr><td><%@ include file="/toplinks.jsp"%></td></tr>
<tr>
<td width="100%" valign="top">
<table width="100%" cellspacing="0" cellpadding="0" border="1" borderColor=#000066>
    <tr>
<td width="80%">&nbsp;
    <%  int QNO;
         String message=null;
         //String message1=null;
         //String message2=null;
         String noA,noB,noC,noD;                 
        String ID=request.getParameter("id");        
        String EVENTID=request.getParameter("event");        
        Connection connection = null;
        Statement st = null;
        Statement st1 = null;
        Statement st2 = null;
        Statement st3 = null;
        Statement st4 = null;
        Statement st5 = null;
        ResultSet rs= null;
        ResultSet rs1= null;
        ResultSet rs2= null;
        ResultSet rs3= null;
        ResultSet rs4= null;
        ResultSet rs5= null;
        
        Class.forName("com.mysql.jdbc.Driver");
        Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/mynewdatabase","root", "root123");
          
                                      
                       st=con.createStatement();
                       
                try  {               
                  
                  rs = st.executeQuery("SELECT * FROM Questionbank where Questionid='"+ID+"'");
               %>
            <table border="1" cellpadding="2" cellspacing="3" bgcolor="#E6E6FA" align="center"><tr><th>Questions</th><th>A</th><th>B</th><th>C</th><th>D</th></tr>
             <%  message="<table border=1 cellpadding=2 cellspacing=3 bgcolor=#E6E6FA align=center><tr><th>Questions</th><th>A</th><th>B</th><th>C</th><th>D</th></tr>";%>
               <%
              while ( rs.next() )
                     {  
                  
                        //String NO=rs.getString("Qserialno");
                        //session.setAttribute("no",NO); 
                        //String NAME=rs.getString("questionname");
                        //session.setAttribute("name",NAME);                                     
                   %>
                   <tr><td><%=rs.getString("Qserialno") + "." + rs.getString("questionname")%></td>
                   <%
                    message=message+"<tr><td>"+rs.getString("Qserialno") + "." + rs.getString("questionname")+"</td>";
                                                  
                   st1=con.createStatement();  
                   try
                    {         
                      rs1=st1.executeQuery("select count(*) as total  from final where questionid='"+ID+"'and Eventid='"+EVENTID+"'and Qserialno='"+rs.getString("Qserialno")+"'and Answer='A'");
                     rs1.next();
                     noA=rs1.getString("total");
                     session.setAttribute("NOA",noA);
                      rs1=st1.executeQuery("select count(*)  as total from final where questionid='"+ID+"'and Eventid='"+EVENTID+"'and Qserialno='"+rs.getString("Qserialno")+"'and Answer='B'");
                   rs1.next();
                     noB=rs1.getString("total");
                     session.setAttribute("NOB",noB);
                      rs1=st1.executeQuery("select count(*) as total  from final where questionid='"+ID+"'and Eventid='"+EVENTID+"'and Qserialno='"+rs.getString("Qserialno")+"'and Answer='C'");
                    rs1.next();
                     noC=rs1.getString("total");
                     session.setAttribute("NOC",noC);
                      rs1=st1.executeQuery("select count(*) as total  from final where questionid='"+ID+"'and Eventid='"+EVENTID+"'and Qserialno='"+rs.getString("Qserialno")+"'and Answer='D'");
                      rs1.next();
 
                     noD=rs1.getString("total");                    
                     session.setAttribute("NOD",noD); 
                     message=message+"<td>"+noA+"</td><td>"+noB+"</td><td>"+noC+"</td><td>"+noD+"</td></tr></table>";
                     %>                      
                 <td><%=noA%></td><td><%=noB%></td><td><%=noC%></td><td><%=noD%></td></tr>
                   
               <%         
                   
                  
                     } finally
                      {
                           if (rs1 != null)
                           {
                               rs1.close();
                               rs1 = null;
                           } if (st1 != null)
                           {
                               st1.close();
                               st1 = null;
                               
                           }
                    
                           }
                          }
                       } 

                                   finally
                       {
                           if (rs != null)
                           {
                               rs.close();
                               rs = null;
                           }
                           if (st != null)
                           {
                               st.close();
                               st = null;
                               con.close();
                               
                           }

                       }                
                       
                       
                      
                  %></table> &nbsp;&nbsp;
                  <table align="center" width="50%" cellspacing="0" cellpadding="0" border="1" borderColor=#D2691E>
                  <form name="sendmail" action="/student/servletmail" method="POST">
                   <tr class="CellColor">
		<td>To</td>
		<td class="CellColor" width="1%">
			:
		</td>
		<td class="CellColor">
			<input type="text" name="to" size="25" value="">
		</td>
                <td>From</td>
		<td class="CellColor" width="1%">
			:
		</td>
		<td class="CellColor">
			<input type="text" name="from" size="25" value="">
		</td>
                
	</tr>
        <input type="hidden" name="message" value="<%=message%>">
        <tr class="CellColor">
		<td class="CellColor" colspan="9" align="center">
			<input type="submit" name="tn1" value="Send" >
			&nbsp;&nbsp;&nbsp;
			
		</td>
	</tr>    
                      
  </form></table></td></tr></table>
                   
              </table>
                     </body></html>    
And here is an servlet where i am trying to send the mail
package com.student.igidr.test;


import java.io.*;
import java.net.*;
import java.util.*;
import javax.mail.*;
import javax.mail.event.*;
import javax.mail.internet.*;

import javax.servlet.*;
import javax.servlet.http.*;

/**
 *
 * @author user1
 */
public class servletmail extends HttpServlet


    {

        public  void doPost(HttpServletRequest request,HttpServletResponse response)

                                  throws ServletException, IOException

    {

        PrintWriter out=response.getWriter();

        response.setContentType("text/html");

        try

        {

           Properties props=new Properties();

           props.put("mail.smtp.host","webmail.igidr.ac.in");   //  'localhost' for testing

   Session   session1  =  Session.getDefaultInstance(props,null);

           String s1 = request.getParameter("to"); 

           String s2 = request.getParameter("from");

           //String s3 = request.getParameter("sub");

           String s4 = request.getParameter("message");
          // out.println(s4);

     Message message =new MimeMessage(session1);

     message.setFrom(new InternetAddress(s2));

      message.setRecipients

             (Message.RecipientType.TO,InternetAddress.parse(s1,false));

          // message.setSubject(s3);

           message.setText(s4);        
          message.setContent(s4,"text/html");

           Transport.send(message);

           out.println("mail has been sent");

        }        

        catch(Exception ex)

        {

           System.out.println("ERROR....."+ex);

        }

    }

}
I am using the message variable to send the message also as input variable in servlet.
Whether it is write Way to represent hole table body into message variable in jsp page.
Please help me.

Thanks and Regards
haresh

Edited by: HARSHAL_GURAV on Aug 13, 2008 11:15 PM

Edited by: HARSHAL_GURAV on Aug 13, 2008 11:56 PM
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Sep 13 2008
Added on Aug 14 2008
4 comments
873 views