Link to home
Start Free TrialLog in
Avatar of raghuudupa
raghuudupa

asked on

Sending mail from a JSP using Javamail........Very urgent!!!!!!! (250 points)

Hi Experts,

I have a jsp which gets loaded with data from the backend.Now this jsp has a table made of columns(time in hrs 00:00 to 23:00) which gets populated each time it is refreshed for every hr.If the data gets loaded correctly then the colour of the particular cell ie  column(time) and row(represented by some name) becomes green and when it fails it remains red.Now i need to send an email when ever the loading fails.How do i do that??
Do i have to write the javamail code within the jsp that iam having right now??If somebody can help me with the code it will be great!

The code for the jsp goes like this:

<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<%@ page session="false" %>
<%@ page import="java.sql.*"%>
<%@ page import="com.inat.db.*"%>
<%@ page import="java.util.*"%>
<%@ page import="java.text.*"%>
<%@ page import="com.inat.bo.Status"%>
<%@ page import="com.inat.bo.StatusEntry"%>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<link type="text/css" rel="StyleSheet" href="../css/statustable.css" />
<title>INat::LoadStatus</title>
</head>
<body>
<h2 align=center> INAT :: Data Load Status </h2>
<form>
<%@ include file="../calendar.jsp" %>
<%
    Enumeration entries = Status.getStatus(selDate);
%>
</form>
<p><b>Status for : <%= displayDate %> <b></p>
<br>
<table class="status-table" border=0 cellspacing=1 cellpadding=2>
<thead>
<tr>
<td> BSC </td>
<%
for (int i = 0; i < 24; i++) {
    String ts = i + ":00";
%>
<td> <%= ts %> </td>
<%
}
%>
</tr>
</thead>
<%
while(entries.hasMoreElements()) {
    StatusEntry entry = (StatusEntry)entries.nextElement();
    int numDataArrays = entry.getNumDataArrays();    
%>
    <tr>
        <td class="label"> <%= entry.getName() %> </td>
        <% for (int hour = 0; hour < 24; hour++) {
               String code = "success";
               String msg = "";
               for (int i = 0; i < numDataArrays; i++) {
                   int[] data = entry.getDataArray(i);
                   if (data[hour] <= 0) {
                       code = "error";
                   }
                   msg += entry.getDataArrayName(i) + "=" + data[hour] + "<br>";
               }
        %>
         <td class=<%=code %>>
         <%= msg %>
         <a href="ShowLogs.jsp?date=<%= selDate %>&bsc=<%= entry.getName() %>&hour=<%= hour %>&type=log">Info </a>,
         <a href="ShowLogs.jsp?date=<%= selDate %>&bsc=<%= entry.getName() %>&hour=<%= hour %>&type=err">Errors</a><br>        
         </td>
        <%
         }
        %>
    </tr>
<%
}
%>    
</table>
</body>
</html>

------------------------------------------------------->---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------->------------------------------------------------------------------------------------
I know i have to use the java mail api and write code something similar to this...


 Properties props = new Properties();
            props.put("mail.smtp.host", smtp);

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

            Message msg = new MimeMessage(session);
            msg.setFrom (new InternetAddress ("Accounts","accounts@widgetsandgadgets"));
            msg.addRecipient (Message.RecipientType.TO, new InternetAddress (toName,toAddr));
            msg.setSubject (subject);
            msg.setContent (content, "text/plain");
           
            Transport.send (msg);
        }

_------------------------------------------------------------>--------------------------------------------------------------------------------------------------------------------------------------
But i do not know the process how i should be able to do it.If someone can give me the code i would appreciate it..I am new to this area....
Thanks once again..
Avatar of Siva Prasanna Kumar
Siva Prasanna Kumar
Flag of India image

http://www.java2s.com/Code/Java/Servlets/Servletandemail.htm

there are lots of way one thing is you can have a srvlet like the above one and when the user says send you just nedd to forward the form action to this servlet and then send the main to the email id specified by the user and then use a request dispathcer to go to a new JSP.

here is another example http://www.java2s.com/Code/Java/JSP/EMail.htm

but i didn't had time to go through it.

or  else check out some shopping cart  applcation in http://www.java2s.com/Code/Java/JSP/Shopping-Cart.htm

atleast one of them will have a mailing option.

Thank You.
ASKER CERTIFIED SOLUTION
Avatar of Isisagate
Isisagate

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of raghuudupa
raghuudupa

ASKER

Thanks for the reply..
But what exactly should i do..??
Do i include the code that you sent inside my jsp ??Where do i instantiate? Please go through my requirement that i have written in my question and give me a good possible solution...
I appreciate your help....

Thanks
What server are you running?
The webserver is Apache Tomcat/5.5.12............
copy the code and save the file as SendMail.java(capitolization needed).  compile the .java file with javac (syntax - javac SendMail.java)  That will create a SendMail.class file.  copy that file into webapps/<app name>/WEB-INF/classes/ folder

download javaMail and copy the .jar file to webapps/<app name>/WEB-INF/lib folder

Insert the following code into your jsp page error block.

SendMail mail = new  SendMail(<to>,<from>,<bcc>,<host>,<message>,<subject>,<username>,<password>);
mail.send();

In my jsp page i need to include this right??

if (data[hour] <= 0) {
                       code = "error";
SendMail mail = new  SendMail(<to>,<from>,<bcc>,<host>,<message>,<subject>,<username>,<password>);
mail.send();
}

I hope this is correct??
Hi Isisagate,
What are the parameters that i require for this application...??
What do i need to find out like for example smtp hostname etc etc. for this application??
How do i get the from,uname,host ,pwd and to parameters??

Please explain the flow..I would really appreciate it..
Yes,

replace <to> with recient e-mail address.
replace <from> with the responder e-mail address
replace <bcc> with a blind carbon copy(leave blank if not using)
replace <host> with smtp server address
replace <message> with the message body.
replace <subject> with the message subject
replace <username> with smtp servers authenication user name
replace <password> with smtp servers authentication password.

a sample line would be.

if (data[hour] <= 0) {
                       code = "error";
                       SendMail mail = new SendMail("raghuudupa@someDomain.com","raghuudupa@someDomain.com","","mail.someDomain.com","data[hour] <= 0","program error","raghuudupa","password");
                       mail.send();
}

Cool ....Thanks a lot ...
Will get back to you and award you points once this works...
Thanks and i appreciate your help..