JavaMail bean...
----------------------------------------------------------------------------
----
package mail;

import java.io.*;
import java.util.*;
import java.net.*;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.mail.*;
import javax.mail.internet.*;

public class JavaMail{

 private String SMTPHost = "SMTP.ug.bcc.bilkent.edu.tr";
 String from, to, subject, text;

 public void setFrom(String name){
  from = name;
 }
 public void setTo(String name){
  to = name;
 }
 public void setSubject(String name){
  subject = name;
 }

 public void setText(String name){
  text = name;
 }

 public String send() {

  String error = "";

  try{
   Properties sysProps = System.getProperties();
   sysProps.put("mail.smtp.host",SMTPHost);
   Session session = Session.getInstance(sysProps,null);

   MimeMessage message = new MimeMessage(session);

   Address fromAddress = new InternetAddress(from);
   message.setFrom(fromAddress);

   Address[] toAddress = InternetAddress.parse(to);
   message.setRecipients(Message.RecipientType.TO,toAddress);

   message.setSubject(subject);

   message.setText(text.toString());
   Transport.send(message);
  }
  catch (AddressException e){
    error = error + e.getClass() + e.getMessage() + "\n";
  }
  catch (SendFailedException e){
    error = error + e.getClass() + e.getMessage() + "\n";
  }
  catch (MessagingException e){
    error = error + e.getClass() + e.getMessage() + "\n";
  }

  return error;
 }
}
----------------------------------------------------------------------------
-----
mail.html
----------------------------------------------------------------------------
------
<html>

<head>
    <title>carts</title>
</head>

<body bgcolor="white">

<form type=POST action=mail.jsp>
<TABLE>
<TR><TD>From:</TD><TD><input type=text name=from></TD></TR>
<TR><TD>To:</TD><TD><input type=text name=to></TD></TR>
<TR><TD>Subject:</TD><TD><input type=text name=subject></TD></TR>
<TR><TD>Text:</TD><TD><TEXTAREA cols=52 name = text rows=5
wrap=virtual></TEXTAREA></TD></TR>
</TABLE>
<input type=submit value = "Submit">
</FORM>
</body>
</html>

----------------------------------------------------------------------------
------
mail.jsp
----------------------------------------------------------------------------
------
<html>

<jsp:useBean id="mail" scope="session" class="mail.JavaMail" />

<jsp:setProperty name="mail" property="*" />

<%@ include file ="/db/mail.html" %>

<%
 String error = mail.send();
 if(error.equals(""))
%>
Successfull
<%
    else
%>

<%=error%>

</TABLE>

</html>
----------------------------------------------------------------------------
------

===========================================================================
To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff JSP-INTEREST".
Some relevant FAQs on JSP/Servlets can be found at:

 http://java.sun.com/products/jsp/faq.html
 http://www.esperanto.org.nz/jsp/jspfaq.html
 http://www.jguru.com/jguru/faq/faqpage.jsp?name=JSP
 http://www.jguru.com/jguru/faq/faqpage.jsp?name=Servlets

Reply via email to