Top Description Fields Constructors Methods
org.apache.tools.mail

public Class MailMessage

Additional top-level class in compilation unit: MailPrintStream.

extends Object
Class Inheritance
Imports
java.io.BufferedOutputStream, .IOException, .OutputStream, .PrintStream, java.net.InetAddress, .Socket, java.util.LinkedHashMap, .Map, .Vector

A class to help send SMTP email. 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, and has an Open Source license.

It can be used like this:

String mailhost = "localhost";  // or another mail host
String from = "Mail Message Servlet <[email protected]>";
String to = "[email protected]";
String cc1 = "[email protected]";
String cc2 = "[email protected]";
String bcc = "[email protected]";

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:

Version
1.1, 2000/03/19, added angle brackets to address, helps some servers version 1.0, 1999/12/29

Field Summary

Modifier and TypeField and Description
private final Vector<String>
cc

list of email addresses to cc to

public static final String
DEFAULT_HOST

default mailhost

public static final int
DEFAULT_PORT

default port for SMTP: 25

private String
from

sender email address

private final Map<String, String>
headers

headers to send in the mail

private String
host

host name for the mail server

private SmtpResponseReader
private static final int
private static final int
private static final int
private static final int
private static final int
private static final int
private static final int
private static final int
private MailPrintStream
private int
port

host port for the mail server

private final Vector<String>
replyto

list of email addresses to reply to

private Socket
private final Vector<String>
to

list of email addresses to send to

Constructor Summary

AccessConstructor and Description
public
MailMessage()

Constructs a new MailMessage to send an email.

public
MailMessage(String
the mail server to use
host
)

Constructs a new MailMessage to send an email.

public
MailMessage(String
the mail server to use
host
,
int
the port to connect to
port
)

Constructs a new MailMessage to send an email.

Method Summary

Modifier and TypeMethod and Description
public void
bcc(String
the bcc address
bcc
)

Sets the bcc address.

public void
cc(String
the cc address
cc
)

Sets the cc address.

pack-priv void
pack-priv void
pack-priv void
public void
from(String
the from address
from
)

Sets the from address.

public PrintStream

Returns:

a printstream containing the data and the headers of the email
getPrintStream
()

Returns a PrintStream that can be used to write the body of the message.

pack-priv void
pack-priv boolean
isResponseOK(String response, int[] ok)

public void
replyto(String
the replyto address
rto
)

Sets the replyto address This method may be called multiple times.

pack-priv static String
pack-priv void
send(String msg, int[] ok)

public void
sendAndClose()

Sends the message and closes the connection to the server.

pack-priv void
pack-priv void
pack-priv void
pack-priv void
pack-priv void
pack-priv void
pack-priv void
pack-priv void
public void
setHeader(String
name of the header
name
,
String
contents of the header
value
)

Sets the named header to the given value.

public void
setPort(int
the port to use for connection.
port
)

Set the port to connect to the SMTP host.

pack-priv void
public void
setSubject(String
the subject of the mail message
subj
)

Sets the subject of the mail message.

pack-priv void
public void
to(String
the to address
to
)

Sets the to address.

pack-priv String
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

ccback to summary
private final Vector<String> cc

list of email addresses to cc to

DEFAULT_HOSTback to summary
public static final String DEFAULT_HOST

default mailhost

DEFAULT_PORTback to summary
public static final int DEFAULT_PORT

default port for SMTP: 25

fromback to summary
private String from

sender email address

headersback to summary
private final Map<String, String> headers

headers to send in the mail

hostback to summary
private String host

host name for the mail server

inback to summary
private SmtpResponseReader in
OK_DATAback to summary
private static final int OK_DATA
OK_DOTback to summary
private static final int OK_DOT
OK_FROMback to summary
private static final int OK_FROM
OK_HELOback to summary
private static final int OK_HELO
OK_QUITback to summary
private static final int OK_QUIT
OK_RCPT_1back to summary
private static final int OK_RCPT_1
OK_RCPT_2back to summary
private static final int OK_RCPT_2
OK_READYback to summary
private static final int OK_READY
outback to summary
private MailPrintStream out
portback to summary
private int port

host port for the mail server

replytoback to summary
private final Vector<String> replyto

list of email addresses to reply to

socketback to summary
private Socket socket
toback to summary
private final Vector<String> to

list of email addresses to send to

Constructor Detail

MailMessageback to summary
public MailMessage() throws IOException

Constructs a new MailMessage to send an email. Use localhost as the mail server with port 25.

Exceptions
IOException:
if there's any problem contacting the mail server
MailMessageback to summary
public MailMessage(String host) throws IOException

Constructs a new MailMessage to send an email. Use the given host as the mail server with port 25.

Parameters
host:String

the mail server to use

Exceptions
IOException:
if there's any problem contacting the mail server
MailMessageback to summary
public MailMessage(String host, int port) throws IOException

Constructs a new MailMessage to send an email. Use the given host and port as the mail server.

Parameters
host:String

the mail server to use

port:int

the port to connect to

Exceptions
IOException:
if there's any problem contacting the mail server

Method Detail

bccback to summary
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.

Parameters
bcc:String

the bcc address

Exceptions
IOException:
if there's any problem reported by the mail server
ccback to summary
public void cc(String cc) throws IOException

Sets the cc address. Also sets the "Cc" header. This method may be called multiple times.

Parameters
cc:String

the cc address

Exceptions
IOException:
if there's any problem reported by the mail server
connectback to summary
pack-priv void connect() throws IOException
disconnectback to summary
pack-priv void disconnect() throws IOException
flushHeadersback to summary
pack-priv void flushHeaders() throws IOException
fromback to summary
public void from(String from) throws IOException

Sets the from address. Also sets the "From" header. This method should be called only once.

Parameters
from:String

the from address

Exceptions
IOException:
if there's any problem reported by the mail server
getPrintStreamback to summary
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 can be wrapped on top if necessary for internationalization. This is actually done in Message.java

Returns:PrintStream

a printstream containing the data and the headers of the email

Exceptions
IOException:
if there's any problem reported by the mail server
See Also
org.apache.tools.ant.taskdefs.email.Message
getReadyback to summary
pack-priv void getReady() throws IOException
isResponseOKback to summary
pack-priv boolean isResponseOK(String response, int[] ok)
replytoback to summary
public void replyto(String rto)

Sets the replyto address This method may be called multiple times.

Parameters
rto:String

the replyto address

sanitizeAddressback to summary
pack-priv static String sanitizeAddress(String s)
sendback to summary
pack-priv void send(String msg, int[] ok) throws IOException
sendAndCloseback to summary
public void sendAndClose() throws IOException

Sends the message and closes the connection to the server. The MailMessage object cannot be reused.

Exceptions
IOException:
if there's any problem reported by the mail server
sendDataback to summary
pack-priv void sendData() throws IOException
sendDotback to summary
pack-priv void sendDot() throws IOException
sendFromback to summary
pack-priv void sendFrom(String from) throws IOException
sendHeloback to summary
pack-priv void sendHelo() throws IOException
sendQuitback to summary
pack-priv void sendQuit() throws IOException
sendRcptback to summary
pack-priv void sendRcpt(String rcpt) throws IOException
setCcHeaderback to summary
pack-priv void setCcHeader()
setFromHeaderback to summary
pack-priv void setFromHeader()
setHeaderback to summary
public void setHeader(String name, String value)

Sets the named header to the given value. RFC 822 provides the rules for what text may constitue a header name and value.

Parameters
name:String

name of the header

value:String

contents of the header

setPortback to summary
public void setPort(int port)

Set the port to connect to the SMTP host.

Parameters
port:int

the port to use for connection.

See Also
DEFAULT_PORT
setReplyToHeaderback to summary
pack-priv void setReplyToHeader()
setSubjectback to summary
public void setSubject(String subj)

Sets the subject of the mail message. Actually sets the "Subject" header.

Parameters
subj:String

the subject of the mail message

setToHeaderback to summary
pack-priv void setToHeader()
toback to summary
public void to(String to) throws IOException

Sets the to address. Also sets the "To" header. This method may be called multiple times.

Parameters
to:String

the to address

Exceptions
IOException:
if there's any problem reported by the mail server
vectorToListback to summary
pack-priv String vectorToList(Vector<String> v)