Link to home
Start Free TrialLog in
Avatar of lilyyan
lilyyan

asked on

jsp email function

Hello, gurus,

I want to implement an email function by using jsp. Which are the basic steps to implement  it?  

Thanks in advance for your suggestion.
Avatar of bloodredsun
bloodredsun
Flag of Australia image

You'll need to use the Java Mail API. Here's a primer on how to implement it, http://www.javaworld.com/javaworld/jw-06-1999/jw-06-javamail.html with a sample app.
Avatar of lilyyan
lilyyan

ASKER

hi, thanks for the reply. just start to work on it. shoudl i download some java mail package? thanks
It's part of J2EE version 1.2 and above if you have the j2ee.jar but otherwise you can find it here http://java.sun.com/products/javamail/downloads/index.html.

 You can find a good example of java mail in the code examples for the Java Cookbook, see here http://javacook.darwinsys.com/download.html
Avatar of lilyyan

ASKER

i'm using jdk1.5.0.01. just installed the javamail-1.3.2 and jaf-1_0_2-upd, aslo set the class path for mail.jar and activation.jar.

i found a sample code from kheefatt

mail form

<html>
<head>
</head>
<%@ page language="java" import="java.sql.*, java.util.ArrayList" %>
<body>
<form name="SubForm" method="post" action="send_mail.jsp">
     <table width="50%" border="1" >
     <tr>
          <td width="108" height="34">To : </td>
          <td width="363">
          <input type="text" name="receiver">
          </td>
     </tr>
     <tr>
          <td height="80" >Message : </td>
          <td><textarea name="mailtext"></textarea>
          </td>
     </tr>
     <tr>
          <td height="34" valign="top">&nbsp;</td>
          <td valign="top"><input type="submit" name="submit" value="Submit Information">
          </td>
     </tr>
     </table>
</form>
</body>
</html>

------------------
<%@ page language="java" import="java.sql.*, java.util.ArrayList" %>
<%@ page import="java.util.*, javax.mail.*, javax.mail.internet.*" %>
<html>
<head>
</head>
<body>
<table width="50%" border="0" >
<tr>
<td>
<%
     String emailAdd = request.getParameter("receiver");
     String mailBody = request.getParameter("mailtext");
     boolean mail_error = false;
     String errMsg = "";

     try
     {
          Properties props = new Properties();
          props.put("mail.smtp.host", "127.0.0.1");  //replace 127.0.0.1 with the name u specify in your mail server
          Session s = Session.getInstance(props,null);
          MimeMessage message = new MimeMessage(s);
          InternetAddress from = new InternetAddress(emailAdd);
          message.setFrom(from);
          InternetAddress to = new InternetAddress("kheefatt@yahoo.com"); //your email address
          message.addRecipient(Message.RecipientType.TO, to);
          message.setSubject("Mail Test!");
          message.setText(mailBody);
          Transport.send(message);
     }
     catch (Exception ex)
     {
          String titleMsg = "Error Exception";
          errMsg = ex.getMessage();
          mail_error = true;
     }    
     if (mail_error==false)
          out.write("Thank you for subscribing to our newsletter.<BR>");
     else if (mail_error==true)
     {
          out.write("There's an error in the mail server. Please try again later!<BR>\n");
     }
%>
</td>
</tr>
</table>
</body>
</html>


-------------------------------------------------------------
after i tested the above code, got the following error

org.apache.jasper.JasperException: Unable to compile class for JSP

An error occurred at line: 10 in the jsp file: /fileSub/send_mail.jsp
Generated servlet error:
Session cannot be resolved or is not a type

An error occurred at line: 10 in the jsp file: /fileSub/send_mail.jsp
Generated servlet error:
Session cannot be resolved

An error occurred at line: 10 in the jsp file: /fileSub/send_mail.jsp
Generated servlet error:
MimeMessage cannot be resolved or is not a type

.............

i already set the classpath and restarted the tomcat server.

thanks for the reply.
ASKER CERTIFIED SOLUTION
Avatar of bloodredsun
bloodredsun
Flag of Australia image

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
Hi,

Please forgive the intrusion, bloodredsun. I just wanted to add that there is jakarta taglib for email that can be used instead of splashing the java code in the jsp like this. I haven't personally used it but I assume it is of decent quality coming from jakarta.
http://jakarta.apache.org/taglibs/doc/mailer-doc/intro.html

Regards,
Jim
Avatar of lilyyan

ASKER

hello, gurus,

the problem is solved. there is some error in the classpath setting. now it's working.

jim_cakalic , thanks for your reply. i will have a reference for the link you mentioned.

now i want to implement some function like sending mail to multiple users and add attachment. if there is some puzzle, i will post another question.
Jim, nothing to forgive mate, we're all here to help and I'm always happy to hear whatever you have to say :-)

and thanks lilyyan
Just trying to keep it polite :-)