| |||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
public class SMTPTransport extends Transport
This class implements the Transport abstract class using SMTP for message submission and transport.
See the com.sun.mail.smtp package documentation for further information on the SMTP protocol provider.
This class includes many protected methods that allow a subclass to extend this class and add support for non-standard SMTP commands. The {@link #issueCommand} and {@link #sendCommand} methods can be used to send simple SMTP commands. Other methods such as the {@link #mailFrom} and {@link #data} methods can be overridden to insert new commands before or after the corresponding SMTP commands. For example, a subclass could do this to send the XACT command before sending the DATA command:
protected OutputStream data() throws MessagingException { if (supportsExtension("XACCOUNTING")) issueCommand("XACT", 25); return super.data(); }
Field Summary |
---|
Fields inherited from class javax.mail.Service |
---|
debug, session, url |
Constructor Summary | |
---|---|
SMTPTransport(Session session, URLName urlname) Constructor that takes a Session object and a URLName that represents a specific SMTP server. |
|
protected |
SMTPTransport(Session session, URLName urlname, String name, int defaultPort, boolean isSSL) Constructor used by this class and by SMTPSSLTransport subclass. |
Method Summary | |
---|---|
protected void |
Check if we're in the connected state. |
void |
close() Close the Transport and terminate the connection to the server. |
void |
Start the SMTP protocol on the given socket, which was already connected by the caller. |
protected OutputStream |
data() Send the DATA command to the SMTP host and return
an OutputStream to which the data is to be written.
|
protected boolean |
Issue the EHLO command.
|
protected void |
finalize() Stop the event dispatcher thread so the queue can be garbage collected. |
protected void |
Terminate the sent data. |
String |
Return the parameter the server provided for the specified service extension, or null if the extension isn't supported. |
int |
Return the return code from the last response we got from the server. |
String |
Return the last response we got from the server. |
String |
Get the name of the local host, for use in the EHLO and HELO commands. |
boolean |
Should we report even successful sends by throwing an exception? If so, a SendFailedException will always be thrown and
an com.sun.mail.smtp.SMTPAddressSucceededException will be included in the exception
chain for each successful address, along with the usual
com.sun.mail.smtp.SMTPAddressFailedException for each unsuccessful address.
|
String |
Gets the SASL realm to be used for DIGEST-MD5 authentication. |
boolean |
Should we use the STARTTLS command to secure the connection if the server supports it? |
boolean |
Should we use the RSET command instead of the NOOP command in the @{link #isConnected isConnected} method? |
protected void |
Issue the HELO command.
|
boolean |
Check whether the transport is connected. |
void |
issueCommand(String cmd, int expect) Send the command to the server. |
protected void |
mailFrom() Issue the MAIL FROM: command to start sending a message.
|
protected boolean |
protocolConnect(String host, int port, String user, String passwd) Performs the actual protocol-specific connection attempt. |
protected void |
rcptTo() Sends each address to the SMTP host using the RCPT TO:
command and copies the address either into
the validSentAddr or invalidAddr arrays.
|
protected int |
Reads server reponse returning the returnCode
as the number.
|
protected void |
sendCommand(String cmd) Sends command cmd to the server terminating
it with CRLF .
|
void |
sendMessage(Message message, Address[] addresses) Send the Message to the specified list of addresses. |
void |
setLocalHost(String localhost) Set the name of the local host, for use in the EHLO and HELO commands. |
void |
setReportSuccess(boolean reportSuccess) Set whether successful sends should be reported by throwing an exception. |
void |
setSASLRealm(String saslRealm) Sets the SASL realm to be used for DIGEST-MD5 authentication. |
void |
setStartTLS(boolean useStartTLS) Set whether the STARTTLS command should be used. |
void |
setUseRset(boolean useRset) Set whether the RSET command should be used instead of the NOOP command in the @{link #isConnected isConnected} method. |
protected int |
simpleCommand(byte[] cmd) Send the command to the server and return the response code from the server. |
int |
simpleCommand(String cmd) Send the command to the server and return the response code from the server. |
protected void |
startTLS() Issue the STARTTLS command and switch the socket to
TLS mode if it succeeds.
|
protected boolean |
supportsAuthentication(String auth) Does the server we're connected to support the specified authentication mechanism? Uses the extension information returned by the server from the EHLO command. |
boolean |
supportsExtension(String ext) Return true if the SMTP server supports the specified service extension. |
protected static String |
Convert a string to RFC 1891 xtext format. |
Methods inherited from class javax.mail.Transport |
---|
addTransportListener, notifyTransportListeners, removeTransportListener, send, send, sendMessage |
Methods inherited from class javax.mail.Service |
---|
addConnectionListener, close, connect, connect, connect, connect, finalize, getURLName, isConnected, notifyConnectionListeners, protocolConnect, queueEvent, removeConnectionListener, setConnected, setURLName, toString |
Constructor Detail |
---|
public SMTPTransport(Session session, URLName urlname)
session
urlname
protected SMTPTransport(Session session, URLName urlname, String name, int defaultPort, boolean isSSL)
session
urlname
name
defaultPort
isSSL
Method Detail |
---|
protected void checkConnected()
public synchronized void close() throws MessagingException
close
in class Service
MessagingException
public synchronized void connect(Socket socket) throws MessagingException
socket
MessagingException
protected OutputStream data() throws MessagingException
DATA
command to the SMTP host and return
an OutputStream to which the data is to be written.
MessagingException
protected boolean ehlo(String domain) throws MessagingException
EHLO
command.
Collect the returned list of service extensions.
domain
- our domainMessagingException
protected void finalize() throws Throwable
finalize
in class Service
Throwable
protected void finishData() throws IOException, MessagingException
IOException
MessagingException
public String getExtensionParameter(String ext)
ext
- the service extension namepublic synchronized int getLastReturnCode()
public synchronized String getLastServerResponse()
public synchronized String getLocalHost()
public synchronized boolean getReportSuccess()
SendFailedException
will always be thrown and
an {@link com.sun.mail.smtp.SMTPAddressSucceededException
SMTPAddressSucceededException} will be included in the exception
chain for each successful address, along with the usual
{@link com.sun.mail.smtp.SMTPAddressFailedException
SMTPAddressFailedException} for each unsuccessful address.
public synchronized String getSASLRealm()
public synchronized boolean getStartTLS()
public synchronized boolean getUseRset()
protected void helo(String domain) throws MessagingException
HELO
command.
domain
- our domainMessagingException
public synchronized boolean isConnected()
isConnected
in class Service
public synchronized void issueCommand(String cmd, int expect) throws MessagingException
cmd
- the command to sendexpect
- the expected response codeMessagingException
protected void mailFrom() throws MessagingException
MAIL FROM:
command to start sending a message. Gets the sender's address in the following order:
MessagingException
protected boolean protocolConnect(String host, int port, String user, String passwd) throws MessagingException
Unless mail.smtp.ehlo is set to false, we'll try to identify ourselves using the ESMTP command EHLO. If mail.smtp.auth is set to true, we insist on having a username and password, and will try to authenticate ourselves if the server supports the AUTH extension (RFC 2554).
protocolConnect
in class Service
host
- the name of the host to connect toport
- the port to use (-1 means use default port)user
- the name of the user to login aspasswd
- the user's passwordMessagingException
- for non-authentication failuresprotected void rcptTo() throws MessagingException
RCPT TO:
command and copies the address either into
the validSentAddr or invalidAddr arrays.
Sets the sendFailed
flag to true if any addresses failed.
MessagingException
protected int readServerResponse() throws MessagingException
returnCode
as the number. Returns -1 on failure. Sets
lastServerResponse
and lastReturnCode
.
MessagingException
protected void sendCommand(String cmd) throws MessagingException
cmd
to the server terminating
it with CRLF
.
cmd
MessagingException
public synchronized void sendMessage(Message message, Address[] addresses) throws MessagingException, SendFailedException
If all the addresses
succeed the SMTP check
using the RCPT TO:
command, we attempt to send the message.
A TransportEvent of type MESSAGE_DELIVERED is fired indicating the
successful submission of a message to the SMTP host.
If some of the addresses
fail the SMTP check,
and the mail.stmp.sendpartial
property is not set,
sending is aborted. The TransportEvent of type MESSAGE_NOT_DELIVERED
is fired containing the valid and invalid addresses. The
SendFailedException is also thrown.
If some of the addresses
fail the SMTP check,
and the mail.stmp.sendpartial
property is set to true,
the message is sent. The TransportEvent of type
MESSAGE_PARTIALLY_DELIVERED
is fired containing the valid and invalid addresses. The
SMTPSendFailedException is also thrown.
MessagingException is thrown if the message can't write out
an RFC822-compliant stream using its writeTo
method.
sendMessage
in class Transport
message
- The MimeMessage to be sentaddresses
- List of addresses to send this message toMessagingException
- if the connection is dead
or not in the connected state or if the message is
not a MimeMessage.SendFailedException
- if the send failed because of
invalid addresses.public synchronized void setLocalHost(String localhost)
localhost
public synchronized void setReportSuccess(boolean reportSuccess)
reportSuccess
- should we throw an exception on success?public synchronized void setSASLRealm(String saslRealm)
saslRealm
- the name of the realm to use for
SASL authentication.public synchronized void setStartTLS(boolean useStartTLS)
useStartTLS
- should we use the STARTTLS command?public synchronized void setUseRset(boolean useRset)
useRset
- should we use the RSET command?protected int simpleCommand(byte[] cmd) throws MessagingException
cmd
MessagingException
public synchronized int simpleCommand(String cmd) throws MessagingException
cmd
MessagingException
protected void startTLS() throws MessagingException
STARTTLS
command and switch the socket to
TLS mode if it succeeds.
MessagingException
protected boolean supportsAuthentication(String auth)
auth
- the authentication mechanismpublic boolean supportsExtension(String ext)
ext
- the service extension nameprotected static String xtext(String s)
xtext = *( xchar / hexchar ) xchar = any ASCII CHAR between "!" (33) and "~" (126) inclusive, except for "+" and "=". ; "hexchar"s are intended to encode octets that cannot appear ; as ASCII characters within an esmtp-value. hexchar = ASCII "+" immediately followed by two upper case hexadecimal digits
s
| |||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |