gnu.inet.smtp

Class SMTPConnection


public class SMTPConnection
extends Object

An SMTP client. This implements RFC 2821.

Field Summary

protected static int
AMBIGUOUS
protected static String
AUTH
protected static String
DATA
static int
DEFAULT_PORT
The default SMTP port.
protected static String
EHLO
protected static String
EXPN
protected static String
FINISH_DATA
protected static String
HELO
protected static String
HELP
protected static int
INFO
protected static String
MAIL_FROM
protected static String
NOOP
protected static int
OK
protected static int
OK_NOT_LOCAL
protected static int
OK_UNVERIFIED
protected static String
QUIT
protected static String
RCPT_TO
protected static int
READY
protected static String
RSET
protected static int
SEND_DATA
protected static String
SP
protected static String
STARTTLS
protected static String
VRFY
protected boolean
continuation
If true, there are more responses to read.
protected boolean
debug
If true, log events.
protected String
greeting
The greeting message given by the server.
protected LineInputStream
in
The input stream used to read responses from the server.
protected CRLFOutputStream
out
The output stream used to send commands to the server.
protected String
response
The last response message received from the server.
protected Socket
socket
The underlying socket used for communicating with the server.

Constructor Summary

SMTPConnection(String host)
Creates a new connection to the specified host, using the default SMTP port.
SMTPConnection(String host, int port)
Creates a new connection to the specified host, using the specified port.
SMTPConnection(String host, int port, int connectionTimeout, int timeout, boolean debug)
Creates a new connection to the specified host, using the specified port.

Method Summary

boolean
authenticate(String mechanism, String username, String password)
Authenticates the connection using the specified SASL mechanism, username, and password.
OutputStream
data()
Requests an output stream to write message data to.
List
ehlo(String hostname)
Issues an EHLO command.
List
expn(String address)
Returns a list of valid possibilities for the specified mailing list, or null on failure.
boolean
finishData()
Completes the DATA procedure.
protected int
getAllResponses()
Returns the next response from the server.
String
getGreeting()
Returns the server greeting message.
String
getLastResponse()
Returns the text of the last response received from the server.
protected int
getResponse()
Returns the next response from the server.
boolean
helo(String hostname)
Issues a HELO command.
List
help(String arg)
Returns some useful information about the specified parameter.
boolean
mailFrom(String reversePath, ParameterList parameters)
Execute a MAIL command.
void
noop()
Issues a NOOP command.
void
quit()
Close the connection to the server.
boolean
rcptTo(String forwardPath, ParameterList parameters)
Execute a RCPT command.
void
rset()
Aborts the current mail transaction.
protected void
send(String command)
Send the specified command string to the server.
boolean
starttls()
Negotiate TLS over the current connection.
boolean
starttls(TrustManager tm)
Negotiate TLS over the current connection.
List
vrfy(String address)
Returns a list of valid possibilities for the specified address, or null on failure.

Field Details

AMBIGUOUS

protected static final int AMBIGUOUS

Field Value:
553


AUTH

protected static final String AUTH

Field Value:
""


DATA

protected static final String DATA

Field Value:
""


DEFAULT_PORT

public static final int DEFAULT_PORT
The default SMTP port.

Field Value:
25


EHLO

protected static final String EHLO

Field Value:
""


EXPN

protected static final String EXPN

Field Value:
""


FINISH_DATA

protected static final String FINISH_DATA

Field Value:
"\n"


HELO

protected static final String HELO

Field Value:
""


HELP

protected static final String HELP

Field Value:
""


INFO

protected static final int INFO

Field Value:
214


MAIL_FROM

protected static final String MAIL_FROM

Field Value:
""


NOOP

protected static final String NOOP

Field Value:
""


OK

protected static final int OK

Field Value:
250


OK_NOT_LOCAL

protected static final int OK_NOT_LOCAL

Field Value:
251


OK_UNVERIFIED

protected static final int OK_UNVERIFIED

Field Value:
252


QUIT

protected static final String QUIT

Field Value:
""


RCPT_TO

protected static final String RCPT_TO

Field Value:
""


READY

protected static final int READY

Field Value:
220


RSET

protected static final String RSET

Field Value:
""


SEND_DATA

protected static final int SEND_DATA

Field Value:
354


SP

protected static final String SP

Field Value:
""


STARTTLS

protected static final String STARTTLS

Field Value:
""


VRFY

protected static final String VRFY

Field Value:
""


continuation

protected boolean continuation
If true, there are more responses to read.


debug

protected boolean debug
If true, log events.


greeting

protected final 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 String response
The last response message received from the server.


socket

protected Socket socket
The underlying socket used for communicating with the server.

Constructor Details

SMTPConnection

public SMTPConnection(String host)
            throws IOException
Creates a new connection to the specified host, using the default SMTP port.

Parameters:
host - the server hostname


SMTPConnection

public SMTPConnection(String host,
                      int port)
            throws IOException
Creates a new connection to the specified host, using the specified port.

Parameters:
host - the server hostname
port - the port to connect to


SMTPConnection

public SMTPConnection(String host,
                      int port,
                      int connectionTimeout,
                      int timeout,
                      boolean debug)
            throws IOException
Creates a new connection to the specified host, using the specified port.

Parameters:
host - the server hostname
port - the port to connect to
connectionTimeout - the connection timeout in milliseconds
timeout - the I/O timeout in milliseconds
debug - whether to log progress

Method Details

authenticate

public boolean authenticate(String mechanism,
                            String username,
                            String password)
            throws IOException
Authenticates the connection using the specified SASL mechanism, username, and password.

Parameters:
mechanism - a SASL authentication mechanism, e.g. LOGIN, PLAIN, CRAM-MD5, GSSAPI
username - the authentication principal
password - the authentication credentials

Returns:
true if authentication was successful, false otherwise


data

public OutputStream data()
            throws 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.

Returns:
a stream for writing messages to


ehlo

public List ehlo(String hostname)
            throws 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.

Parameters:
hostname - the local host name


expn

public List expn(String address)
            throws IOException
Returns a list of valid possibilities for the specified mailing list, or null on failure.

Parameters:
address - a mailing list name


finishData

public boolean finishData()
            throws IOException
Completes the DATA procedure.

Returns:
true id transfer was successful, false otherwise

See Also:
data()


getAllResponses

protected int getAllResponses()
            throws 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 String getGreeting()
Returns the server greeting message.


getLastResponse

public String getLastResponse()
Returns the text of the last response received from the server.


getResponse

protected int getResponse()
            throws IOException
Returns the next response from the server.


helo

public boolean helo(String hostname)
            throws IOException
Issues a HELO command.

Parameters:
hostname - the local host name


help

public List help(String arg)
            throws IOException
Returns some useful information about the specified parameter. Typically this is a command.

Parameters:
arg - the context of the query, or null for general information

Returns:
a list of possibly useful information, or null if the command failed.


mailFrom

public boolean mailFrom(String reversePath,
                        ParameterList parameters)
            throws IOException
Execute a MAIL command.

Parameters:
reversePath - the source mailbox(from address)
parameters - optional ESMTP parameters

Returns:
true if accepted, false otherwise


noop

public void noop()
            throws IOException
Issues a NOOP command. This does nothing, but can be used to keep the connection alive.


quit

public void quit()
            throws IOException
Close the connection to the server.


rcptTo

public boolean rcptTo(String forwardPath,
                      ParameterList parameters)
            throws IOException
Execute a RCPT command.

Parameters:
forwardPath - the forward-path(recipient address)
parameters - optional ESMTP parameters

Returns:
true if successful, false otherwise


rset

public void rset()
            throws IOException
Aborts the current mail transaction.


send

protected void send(String command)
            throws IOException
Send the specified command string to the server.

Parameters:
command - the command to send


starttls

public boolean starttls()
            throws 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(TrustManager tm)
            throws 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.

Parameters:
tm - the custom trust manager to use


vrfy

public List vrfy(String address)
            throws IOException
Returns a list of valid possibilities for the specified address, or null on failure.

Parameters:
address - a mailbox, or real name and mailbox


* SMTPConnection.java * Copyright (C) 2003 Chris Burdess * * This file is part of GNU inetlib, a library. * * GNU inetlib is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * GNU inetlib is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this library; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * * Linking this library statically or dynamically with other modules is * making a combined work based on this library. Thus, the terms and * conditions of the GNU General Public License cover the whole * combination. * * As a special exception, the copyright holders of this library give you * permission to link this library with independent modules to produce an * executable, regardless of the license terms of these independent * modules, and to copy and distribute the resulting executable under * terms of your choice, provided that you also meet, for each linked * independent module, the terms and conditions of the license of that * module. An independent module is a module which is not derived from * or based on this library. If you modify this library, you may extend * this exception to your version of the library, but you are not * obliged to do so. If you do not wish to do so, delete this * exception statement from your version.