An SMTP client.
This implements RFC 2821.
AMBIGUOUS
protected static final int AMBIGUOUS
- 553
AUTH
protected static final java.lang.String AUTH
- "AUTH"
DATA
protected static final java.lang.String DATA
- "DATA"
DEFAULT_PORT
public static final int DEFAULT_PORT
The default SMTP port.
- 25
EHLO
protected static final java.lang.String EHLO
- "EHLO"
EXPN
protected static final java.lang.String EXPN
- "EXPN"
FINISH_DATA
protected static final java.lang.String FINISH_DATA
- "\n."
HELO
protected static final java.lang.String HELO
- "HELO"
HELP
protected static final java.lang.String HELP
- "HELP"
INFO
protected static final int INFO
- 214
MAIL_FROM
protected static final java.lang.String MAIL_FROM
- "MAIL FROM:"
NOOP
protected static final java.lang.String NOOP
- "NOOP"
OK
protected static final int OK
- 250
OK_NOT_LOCAL
protected static final int OK_NOT_LOCAL
- 251
OK_UNVERIFIED
protected static final int OK_UNVERIFIED
- 252
QUIT
protected static final java.lang.String QUIT
- "QUIT"
RCPT_TO
protected static final java.lang.String RCPT_TO
- "RCPT TO:"
READY
protected static final int READY
- 220
RSET
protected static final java.lang.String RSET
- "RSET"
SEND_DATA
protected static final int SEND_DATA
- 354
SP
protected static final java.lang.String SP
- " SP "
STARTTLS
protected static final java.lang.String STARTTLS
- "STARTTLS"
VRFY
protected static final java.lang.String VRFY
- "VRFY"
continuation
protected boolean continuation
If true, there are more responses to read.
debug
protected boolean debug
If true, log events.
greeting
protected final java.lang.String greeting
The greeting message given by the server.
in
protected LineInputStream in
The input stream used to read responses from the server.
out
protected CRLFOutputStream out
The output stream used to send commands to the server.
response
protected java.lang.String response
The last response message received from the server.
socket
protected java.net.Socket socket
The underlying socket used for communicating with the server.
SMTPConnection
public SMTPConnection(java.lang.String host)
throws java.io.IOException
Creates a new connection to the specified host, using the default SMTP
port.
host
- the server hostname
SMTPConnection
public SMTPConnection(java.lang.String host,
int port)
throws java.io.IOException
Creates a new connection to the specified host, using the specified
port.
host
- the server hostnameport
- the port to connect to
SMTPConnection
public SMTPConnection(java.lang.String host,
int port,
int connectionTimeout,
int timeout,
boolean debug)
throws java.io.IOException
Creates a new connection to the specified host, using the specified
port.
host
- the server hostnameport
- the port to connect toconnectionTimeout
- the connection timeout in millisecondstimeout
- the I/O timeout in millisecondsdebug
- whether to log progress
authenticate
public boolean authenticate(java.lang.String mechanism,
java.lang.String username,
java.lang.String password)
throws java.io.IOException
Authenticates the connection using the specified SASL mechanism,
username, and password.
mechanism
- a SASL authentication mechanism, e.g. LOGIN, PLAIN,
CRAM-MD5, GSSAPIusername
- the authentication principalpassword
- the authentication credentials
- true if authentication was successful, false otherwise
data
public java.io.OutputStream data()
throws java.io.IOException
Requests an output stream to write message data to.
When the entire message has been written to the stream, the
flush
method must be called on the stream. Until then no
further methods should be called on the connection.
Immediately after this procedure is complete, finishData
must be called to complete the transfer and determine its success.
- a stream for writing messages to
ehlo
public java.util.List ehlo(java.lang.String hostname)
throws java.io.IOException
Issues an EHLO command.
If successful, returns a list of the SMTP extensions supported by the
server.
Otherwise returns null, and HELO should be called.
hostname
- the local host name
expn
public java.util.List expn(java.lang.String address)
throws java.io.IOException
Returns a list of valid possibilities for the specified mailing list,
or null on failure.
address
- a mailing list name
finishData
public boolean finishData()
throws java.io.IOException
Completes the DATA procedure.
- true id transfer was successful, false otherwise
data()
getAllResponses
protected int getAllResponses()
throws java.io.IOException
Returns the next response from the server.
If the response is a continuation, continues to read responses until
continuation ceases. If a different response code from the first is
encountered, this causes a protocol exception.
getGreeting
public java.lang.String getGreeting()
Returns the server greeting message.
getLastResponse
public java.lang.String getLastResponse()
Returns the text of the last response received from the server.
getResponse
protected int getResponse()
throws java.io.IOException
Returns the next response from the server.
helo
public boolean helo(java.lang.String hostname)
throws java.io.IOException
Issues a HELO command.
hostname
- the local host name
help
public java.util.List help(java.lang.String arg)
throws java.io.IOException
Returns some useful information about the specified parameter.
Typically this is a command.
arg
- the context of the query, or null for general information
- a list of possibly useful information, or null if the command
failed.
mailFrom
public boolean mailFrom(java.lang.String reversePath,
ParameterList parameters)
throws java.io.IOException
Execute a MAIL command.
reversePath
- the source mailbox (from address)parameters
- optional ESMTP parameters
- true if accepted, false otherwise
noop
public void noop()
throws java.io.IOException
Issues a NOOP command.
This does nothing, but can be used to keep the connection alive.
quit
public void quit()
throws java.io.IOException
Close the connection to the server.
rcptTo
public boolean rcptTo(java.lang.String forwardPath,
ParameterList parameters)
throws java.io.IOException
Execute a RCPT command.
forwardPath
- the forward-path (recipient address)parameters
- optional ESMTP parameters
- true if successful, false otherwise
rset
public void rset()
throws java.io.IOException
Aborts the current mail transaction.
send
protected void send(java.lang.String command)
throws java.io.IOException
Send the specified command string to the server.
command
- the command to send
starttls
public boolean starttls()
throws java.io.IOException
Negotiate TLS over the current connection.
This depends on many features, such as the JSSE classes being in the
classpath. Returns true if successful, false otherwise.
starttls
public boolean starttls(javax.net.ssl.TrustManager tm)
throws java.io.IOException
Negotiate TLS over the current connection.
This depends on many features, such as the JSSE classes being in the
classpath. Returns true if successful, false otherwise.
tm
- the custom trust manager to use
vrfy
public java.util.List vrfy(java.lang.String address)
throws java.io.IOException
Returns a list of valid possibilities for the specified address, or
null on failure.
address
- a mailbox, or real name and mailbox