Advanced JSP Sample : Sending Email

Previous Contents Next

OTN provides three versions of this sample: one using the Java Mail API directly in a JavaBean, one using the sendMail utility tag and one using the utility javabean oracle.jsp.webutil.email.SendMailBean.

Using JSPs and Java Mail API

Contents

Purpose

Send email using JSPs and Java Mail API.

Remarks

An HTML form accepts input such as email ID, subject, and message body. When the user clicks the Send button, the JSP invokes SendMailBean to send the email using the JavaMail API. The JSP gets parameter values from the HTTP request object and passes them to the send method in the SendMailBean. This 'send' method uses the JavaMail API to send the mail.

Code from SendMail.jsp

  <jsp:useBean id="sendMail" class="SendMailBean" scope="page" />
<%
String l_from = request.getParameter("p_from");
String l_to = request.getParameter("p_to");
String l_cc = request.getParameter("p_cc");
String l_subject = request.getParameter("p_subject");
String l_message = request.getParameter("p_message");
%>
<%= sendMail.send(l_from, l_to, l_cc, l_subject, l_message) %>

 

Source Code Listings

Using the sendMail utility tag

Contents

Purpose

Send email using OC4J JSP sendMail tag in the JSPs.

Remarks

An HTML form accepts input such as email ID, subject, and message body. When the user clicks the Send button, the SendMail.jsp using the OC4J JSP sendMail tag sends an email. The SendMail.jsp gets parameter values from the HTTP request object and passes them through the sendMail tag attributes.

Code from SendMail.jsp

...

  <%
//Read all inputs into local variables
String l_from = request.getParameter("p_from");
String l_to = request.getParameter("p_to");
String l_cc = request.getParameter("p_cc");
String l_bcc = request.getParameter("p_bcc");
String l_subject = request.getParameter("p_subject");
String l_message = request.getParameter("p_message");
String l_smtpSvr = request.getParameter("p_smtpServer");
session.setAttribute("smtpServer",l_smtpSvr);
%>
<%-- Use OC4J Jsp mail tag to send the mail --%>
<mail:sendMail host='<%=(String)session.getValue("smtpServer")%>'
sender='<%=l_from%>'
recipient='<%=l_to%>'
cc='<%=l_cc%>'
bcc='<%=l_bcc%>'
subject='<%=l_subject%>'>
<%=l_message%>
</mail:sendMail>
...

 

Source Code Listings

Using the utility javabean

Contents

Purpose

Send email using JSPs using OC4J JSP utility JavaBean.

Remarks

An HTML form accepts input such as email ID, subject, and message body. When the user clicks the Send button, the JSP invokes SendMailBean to send the email using the JavaMail API. The JSP gets parameter values from the HTTP request object and sets them in the OC4J JSP Utility JavaBean, oracle.jsp.webutil.email.SendMailBean. Then, the JSP calls the 'sendMessage' method in the Bean which sends the mail using the values set earlier.

Code from SendMail.jsp

...
<jsp:useBean id="sendMail" class="oracle.jsp.webutil.email.SendMailBean" scope="page" />
<%
String l_from = request.getParameter("p_from");
String l_to = request.getParameter("p_to");
String l_cc = request.getParameter("p_cc");
String l_bcc = request.getParameter("p_bcc");
String l_subject = request.getParameter("p_subject");
String l_message = request.getParameter("p_message");
String l_smtpSvr = request.getParameter("p_smtpServer");
session.setAttribute("smtpServer",l_smtpSvr); //set the mail server host
sendMail.setHost(l_smtpSvr);
//set sender address
sendMail.setSender(l_from);
//set the recipient(s)
sendMail.setRecipient(l_to);
//set Cc address
sendMail.setCc(l_cc);
//set Bcc address
sendMail.setBcc(l_bcc);
//set Subject of the email
sendMail.setSubject(l_subject); //set body of the mail
sendMail.setContent(l_message); //send the email
sendMail.sendMessage(pageContext);

%>
...

 

Source Code Listings

 


Previous Contents Next
E-mail this page
Printer View Printer View
Oracle Is The Information Company About Oracle | Oracle RSS Feeds | Careers | Contact Us | Site Maps | Legal Notices | Terms of Use | Privacy