> You don't need anything as heavy as JavaMail to do something as simple as
this -
> just open a connection to port 25 of your mail server and talk SMTP
protocol to it.
> About 50 lines of code would do it, and there will be plenty of examples
around to
> copy from.
Don't do it. JavaMail is very easy to use and why it would be considered
heavy is beyond my understanding. This is a rather simplified, not entirely
elegant version we use for our low-tech testing, but it has the main parts
to make it more parameterized, etc.
// Copyright (c) 1999 ExperTrade Corporation. All rights reserved.
// This file is proprietary and confidential.
package com.expertrade.servlet;
import java.util.Properties;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.servlet.*;
import javax.servlet.http.*;
/**
* This accepts a page and sends an email with the relevant info via email.
*
* @author David Wall
*/
public class FormEmailer
extends HttpServlet
{
public void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, java.io.IOException
{
doPost( req, res );
}
public void doPost(HttpServletRequest req, HttpServletResponse res)
throws ServletException, java.io.IOException
{
res.setStatus(HttpServletResponse.SC_NO_CONTENT);
// Get the two required parameters
String [] requestedDataArray = null;
requestedDataArray = req.getParameterValues(PARAM_EMAIL_TO);
if ( requestedDataArray == null )
throw new ServletException("Missing parameter: " +
PARAM_EMAIL_TO);
String emailTo = requestedDataArray[0];
requestedDataArray = req.getParameterValues(PARAM_EMAIL_SUBJECT);
if ( requestedDataArray == null )
throw new ServletException("Missing parameter: " +
PARAM_EMAIL_SUBJECT);
String emailSubject = requestedDataArray[0];
InternetAddress[] toAddresses = new InternetAddress[1];
try
{
toAddresses[0] = new InternetAddress(emailTo);
}
catch( javax.mail.internet.AddressException e )
{
throw new ServletException("Invalid email TO address: " +
emailTo + " - " + e.getMessage() );
}
// Get all of the form fields...
java.util.Enumeration params = req.getParameterNames();
StringBuffer message = new StringBuffer(1000);
message.append("TO: ").append(emailTo).append("\n");
message.append("SUBJECT: ").append(emailSubject).append("\n\n");
while( params.hasMoreElements() )
{
String name = (String)params.nextElement();
if ( name.equals(PARAM_EMAIL_TO) ||
name.equals(PARAM_EMAIL_SUBJECT) )
continue;
requestedDataArray = req.getParameterValues(name);
if ( requestedDataArray != null )
{
message.append(name).append(": ");
if ( requestedDataArray.length > 0 )
{
for( int i=0; i < requestedDataArray.length; ++i )
{
if ( i != 0 )
message.append(" ");
message.append(requestedDataArray[i]).append("\n");
}
}
else
message.append("\n");
}
}
message.append("\n");
message.append("\nRemote user:
").append(req.getRemoteUser()).append("\n");
message.append("Remote address:
").append(req.getHeader("REMOTE_ADDR")).append("\n");
message.append("Remote host:
").append(req.getHeader("REMOTE_HOST")).append("\n");
message.append("Browser:
").append(req.getHeader("User-Agent")).append("\n").append("<EOM>\n");
// Register them
prepareEmail(req,res,toAddresses,emailSubject,message.toString());
}
public String getServletInfo()
{
return "FormEmailer, Version 1.0 (14FEB99), ExperTrade Corporation";
}
/**
* Called whenever the servlet is installed into the web server.
*/
public void init( ServletConfig config )
throws ServletException
{
try
{
fromAddress = new InternetAddress("[EMAIL PROTECTED]");
// use a resource parameter instead!
}
catch( javax.mail.internet.AddressException e )
{
throw new ServletException("Invalid from email address: " +
e.getMessage() );
}
String smtpServer = config.getInitParameter(PARAM_SMTP_SERVER);
if ( smtpServer == null )
throw new ServletException("Missing servlet parameter: " +
PARAM_SMTP_SERVER);
Properties prop = new Properties();
prop.put("mail.smtp.host",smtpServer);
session = Session.getDefaultInstance(prop,null);
super.init(config);
}
/**
* Called whenever the servlet is removed from the web server.
*/
public void destroy()
{
session = null;
super.destroy();
}
void prepareEmail( HttpServletRequest req,
HttpServletResponse res,
InternetAddress[] toAddresses,
String subject,
String message
)
throws ServletException, java.io.IOException
{
boolean isOkay = true;
res.setContentType("text/html");
res.setHeader("pragma", "no-cache");
ServletOutputStream out = res.getOutputStream();
writeHeader(out);
try
{
if ( ! sendEmail( toAddresses, subject, message ) )
{
throw new java.io.IOException("Failed to send email");
}
}
catch(java.io.IOException e)
{
if ( isOkay )
out.println("<H1>Sorry!</H1>");
out.println("<P>We were unable to record your information.
Please try again later.");
isOkay = false;
}
if ( isOkay )
{
out.println("<H1>Thank you!</H1>");
out.println("<P>This form has been submitted successfully.");
}
writeFooter(out);
}
boolean sendEmail( InternetAddress[] toAddresses,
String subject,
String message
)
throws ServletException, java.io.IOException
{
try
{
Message msg = new MimeMessage(session);
msg.setFrom(fromAddress);
msg.setRecipients(Message.RecipientType.TO, toAddresses);
msg.setSubject(subject);
msg.setContent(message, "text/plain" );
Transport.send( msg );
return true;
}
catch( javax.mail.MessagingException e )
{
throw new java.io.IOException("Failed to send Email: " +
e.getMessage());
}
}
// ****************** Instance variables *******************
private Session session = null;
private InternetAddress fromAddress = null;
private static final String NBSP = " ";
private static final String GREEN = "\"#009900\"";
private static final String RED = "\"#FF0000\"";
private static final String BLACK = "\"#000000\"";
// HTML PARAM required fields
private static final String PARAM_EMAIL_TO = "EmailTo";
private static final String PARAM_EMAIL_SUBJECT = "EmailSubject";
// Servlet INIT param
private static final String PARAM_SMTP_SERVER = "smtpServer";
} // FormEmailer
// eof: FormEmailer.java
===========================================================================
To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff JSP-INTEREST".
FAQs on JSP can be found at:
http://java.sun.com/products/jsp/faq.html
http://www.esperanto.org.nz/jsp/jspfaq.html