All Packages  Class Hierarchy  This Package  Previous  Next  Index

Class com.oreilly.servlet.MailMessage

java.lang.Object
   |
   +----com.oreilly.servlet.MailMessage

public class MailMessage
extends Object
A class to help send SMTP email. It can be used by any Java program, not just servlets. Servlets are likely to use this class to:

This class is an improvement on the sun.net.smtp.SmtpClient class found in the JDK. This version has extra functionality, and can be used with JVMs that did not extend from the JDK. It's not as robust as the JavaMail Standard Extension classes, but it's easier to use and easier to install.

It can be used like this:

 String mailhost = "localhost";  // or another mail host
 String from = "Mail Message Servlet ";
 String to = "to@somedomain.com";
 String cc1 = "cc1@somedomain.com";
 String cc2 = "cc2@somedomain.com";
 String bcc = "bcc@somedomain.com";
  
 MailMessage msg = new MailMessage(mailhost);
 msg.from(from);
 msg.to(to);
 msg.cc(cc1);
 msg.cc(cc2);
 msg.bcc(bcc);
 msg.setSubject("Test subject");
 PrintStream out = msg.getPrintStream();
  
 Enumeration enum = req.getParameterNames();
 while (enum.hasMoreElements()) {
   String name = (String)enum.nextElement();
   String value = req.getParameter(name);
   out.println(name + " = " + value);
 }
  
 msg.sendAndClose();
 

Be sure to set the from address, then set the recepient addresses, then set the subject and other headers, then get the PrintStream, then write the message, and finally send and close. The class does minimal error checking internally; it counts on the mail host to complain if there's any malformatted input or out of order execution.

An attachment mechanism based on RFC 1521 could be implemented on top of this class. In the meanwhile, JavaMail is the best solution for sending email with attachments.

Still to do:

Version:
1.1, 00/03/19, added angle brackets to address, helps some servers
Author:
Jason Hunter, Copyright © 1999

Constructor Index

 o MailMessage()
Constructs a new MailMessage to send an email.
 o MailMessage(String)
Constructs a new MailMessage to send an email.

Method Index

 o bcc(String)
Sets the bcc address.
 o cc(String)
Sets the cc address.
 o from(String)
Sets the from address.
 o getPrintStream()
Returns a PrintStream that can be used to write the body of the message.
 o sendAndClose()
Sends the message and closes the connection to the server.
 o setHeader(String, String)
Sets the named header to the given value.
 o setSubject(String)
Sets the subject of the mail message.
 o to(String)
Sets the to address.

Constructors

 o MailMessage
 public MailMessage() throws IOException
Constructs a new MailMessage to send an email. Use localhost as the mail server.

Throws: IOException
if there's any problem contacting the mail server
 o MailMessage
 public MailMessage(String host) throws IOException
Constructs a new MailMessage to send an email. Use the given host as the mail server.

Parameters:
host - the mail server to use
Throws: IOException
if there's any problem contacting the mail server

Methods

 o from
 public void from(String from) throws IOException
Sets the from address. Also sets the "From" header. This method should be called only once.

Throws: IOException
if there's any problem reported by the mail server
 o to
 public void to(String to) throws IOException
Sets the to address. Also sets the "To" header. This method may be called multiple times.

Throws: IOException
if there's any problem reported by the mail server
 o cc
 public void cc(String cc) throws IOException
Sets the cc address. Also sets the "Cc" header. This method may be called multiple times.

Throws: IOException
if there's any problem reported by the mail server
 o bcc
 public void bcc(String bcc) throws IOException
Sets the bcc address. Does NOT set any header since it's a *blind* copy. This method may be called multiple times.

Throws: IOException
if there's any problem reported by the mail server
 o setSubject
 public void setSubject(String subj)
Sets the subject of the mail message. Actually sets the "Subject" header.

 o setHeader
 public void setHeader(String name,
                       String value)
Sets the named header to the given value. RFC 822 provides the rules for what text may constitute a header name and value.

 o getPrintStream
 public PrintStream getPrintStream() throws IOException
Returns a PrintStream that can be used to write the body of the message. A stream is used since email bodies are byte-oriented. A writer could be wrapped on top if necessary for internationalization.

Throws: IOException
if there's any problem reported by the mail server
 o sendAndClose
 public void sendAndClose() throws IOException
Sends the message and closes the connection to the server. The MailMessage object cannot be reused.

Throws: IOException
if there's any problem reported by the mail server

All Packages  Class Hierarchy  This Package  Previous  Next  Index