• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 498
  • Last Modified:

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.
0
lilyyan
Asked:
lilyyan
  • 4
  • 3
  • 2
1 Solution
 
bloodredsunCommented:
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.
0
 
lilyyanAuthor Commented:
hi, thanks for the reply. just start to work on it. shoudl i download some java mail package? thanks
0
 
bloodredsunCommented:
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
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
lilyyanAuthor Commented:
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.
0
 
bloodredsunCommented:
Firstly, check the correct jars are available to tomcat, i.e. is the j2ee.jar or the mail jars on the classpath as that is apparently what's wrong. You might need to restart tomcat for these changes to update the tomcat classpath. Filing that, put them in the web-inf/lib of your app.

Secondly, Session is very close to session so you may need to qualify it as in javax.mail.Session s= .........
0
 
Jim CakalicSenior Developer/ArchitectCommented:
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
0
 
lilyyanAuthor Commented:
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.
0
 
bloodredsunCommented:
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
0
 
Jim CakalicSenior Developer/ArchitectCommented:
Just trying to keep it polite :-)
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 4
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now