public class MailMessage extends Object
It can be used like this:
String mailhost = "localhost"; // or another mail host String from = "Mail Message Servlet <MailMessage@server.com>"; String to = "to@you.com"; String cc1 = "cc1@you.com"; String cc2 = "cc2@you.com"; String bcc = "bcc@you.com"; MailMessage msg = new MailMessage(mailhost); msg.setPort(25); 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 recipient 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:
Modifier and Type | Field and Description |
---|---|
static String |
DEFAULT_HOST
default mailhost
|
static int |
DEFAULT_PORT
default port for SMTP: 25
|
Constructor and Description |
---|
MailMessage()
Constructs a new MailMessage to send an email.
|
MailMessage(String host)
Constructs a new MailMessage to send an email.
|
MailMessage(String host,
int port)
Constructs a new MailMessage to send an email.
|
Modifier and Type | Method and Description |
---|---|
void |
bcc(String bcc)
Sets the bcc address.
|
void |
cc(String cc)
Sets the cc address.
|
void |
from(String from)
Sets the from address.
|
PrintStream |
getPrintStream()
Returns a PrintStream that can be used to write the body of the message.
|
void |
replyto(String rto)
Sets the replyto address
This method may be
called multiple times.
|
void |
sendAndClose()
Sends the message and closes the connection to the server.
|
void |
setHeader(String name,
String value)
Sets the named header to the given value.
|
void |
setPort(int port)
Set the port to connect to the SMTP host.
|
void |
setSubject(String subj)
Sets the subject of the mail message.
|
void |
to(String to)
Sets the to address.
|
public static final String DEFAULT_HOST
public static final int DEFAULT_PORT
public MailMessage() throws IOException
IOException
- if there's any problem contacting the mail serverpublic MailMessage(String host) throws IOException
host
- the mail server to useIOException
- if there's any problem contacting the mail serverpublic MailMessage(String host, int port) throws IOException
host
- the mail server to useport
- the port to connect toIOException
- if there's any problem contacting the mail serverpublic void bcc(String bcc) throws IOException
bcc
- the bcc addressIOException
- if there's any problem reported by the mail serverpublic void cc(String cc) throws IOException
cc
- the cc addressIOException
- if there's any problem reported by the mail serverpublic void from(String from) throws IOException
from
- the from addressIOException
- if there's any problem reported by the mail serverpublic PrintStream getPrintStream() throws IOException
IOException
- if there's any problem reported by the mail serverMessage
public void replyto(String rto)
rto
- the replyto addresspublic void sendAndClose() throws IOException
IOException
- if there's any problem reported by the mail serverpublic void setHeader(String name, String value)
name
- name of the headervalue
- contents of the headerpublic void setPort(int port)
port
- the port to use for connection.DEFAULT_PORT
public void setSubject(String subj)
subj
- the subject of the mail messagepublic void to(String to) throws IOException
to
- the to addressIOException
- if there's any problem reported by the mail server