How can I send email from a JSP page?

Govind Seshadri

The JavaMAIL API is the standard mechanism for sending email. See the JavaMail FAQ for how to use it. You can place j2ee.jar (or mail.jar and activation.jar) under web-inf/lib folder in tomcat3.2.4.

You can send email from any JSP engine (like the JSWDK reference implementation) that supports the Sun specific sun.net.smtp package.

(Statutory warning: Using internal Sun-specific packages is not an approach jGuru recommends, as it will prevent your JSP pages from being truly portable.)

The following scriptlet makes use of the SmtpClient class to send an email from within a JSP page.

<%@ page import="sun.net.smtp.SmtpClient, java.io.*" %>
<%
 String from="gseshad@hotmail.com";
 String to="gov@jguru.com, govi@bigfoot.com";
 try{
     SmtpClient client = new SmtpClient("mail.xxxxx.xxx");
     client.from(from);
     client.to(to);
     PrintStream message = client.startMessage();
     message.println("To: " + to);
     message.println("Subject:  Sending email from JSP!");
     message.println("This was sent from a JSP page!");
     message.println();
     message.println("Cool beans! :-)");
     message.println();     
     message.println();
     client.closeServer();
  }
  catch (IOException e){	
     System.out.println("ERROR SENDING EMAIL:"+e);
  }
%>
0 Comments  (click to add your comment)
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

About | Sitemap | Contact