Top Description Inners Fields Constructors Methods
com.sun.mail.smtp

public Class SMTPTransport

extends Transport
Class Inheritance
Known Direct Subclasses
com.sun.mail.smtp.SMTPSSLTransport
Imports
java.io.*, java.net.*, java.util.*, java.util.logging.Level, java.lang.reflect.*, java.nio.charset.StandardCharsets, javax.net.ssl.SSLSocket, javax.mail.*, javax.mail.event.*, javax.mail.internet.*, com.sun.mail.util.PropUtil, .MailLogger, .ASCIIUtility, .SocketFetcher, .MailConnectException, .SocketConnectException, .BASE64EncoderStream, .LineInputStream, .TraceInputStream, .TraceOutputStream, com.sun.mail.auth.Ntlm

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 issueCommand and sendCommand methods can be used to send simple SMTP commands. Other methods such as the mailFrom and 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();
	}
 
Authors
Max Spivak, Bill Shannon, Dean Gibson (DIGEST-MD5 authentication), Luís Serralheiro (NTLM authentication)
See Also
javax.mail.event.ConnectionEvent, javax.mail.event.TransportEvent

Nested and Inner Type Summary

Modifier and TypeClass and Description
private abstract class
SMTPTransport.Authenticator

Abstract base class for SMTP authentication mechanism implementations.

private class
SMTPTransport.BDATOutputStream

An SMTPOutputStream that wraps a ChunkedOutputStream.

private class
SMTPTransport.ChunkedOutputStream

An OutputStream that buffers data in chunks and uses the RFC 3030 BDAT SMTP command to send each chunk.

private class
SMTPTransport.DigestMD5Authenticator

Perform the authentication handshake for DIGEST-MD5 authentication.

private class
SMTPTransport.LoginAuthenticator

Perform the authentication handshake for LOGIN authentication.

private class
SMTPTransport.NtlmAuthenticator

Perform the authentication handshake for NTLM authentication.

private class
SMTPTransport.OAuth2Authenticator

Perform the authentication handshake for XOAUTH2 authentication.

private class
SMTPTransport.PlainAuthenticator

Perform the authentication handshake for PLAIN authentication.

Field Summary

Modifier and TypeField and Description
private Address[]
private boolean
private Map<String, SMTPTransport.Authenticator>
private String
private int
private static final byte[]
private SMTPOutputStream
private boolean
private boolean
private String
private int
private boolean
private MessagingException
private Hashtable<String, String>
private static char[]
private String
private static final String[]
ignoreList

Headers that should not be included when sending

private Address[]
private boolean
private int
private String
private LineInputStream
private String
private MailLogger
private MimeMessage
private String
private boolean
private boolean
private boolean
private String
private boolean
private boolean
private boolean
private SaslAuthenticator
private String[]
private String
private boolean
private BufferedInputStream
private OutputStream
private Socket
private TraceInputStream
private MailLogger
private TraceOutputStream
private static final String
private static final String[]
private boolean
private boolean
private boolean
private Address[]
private Address[]

Constructor Summary

AccessConstructor and Description
public
SMTPTransport(Session
the Session
session
,
URLName
the URLName of this transport
urlname
)

Constructor that takes a Session object and a URLName that represents a specific SMTP server.

protected
SMTPTransport(Session
the Session
session
,
URLName
the URLName of this transport
urlname
,
String
the protocol name of this transport
name
,
boolean
use SSL to connect?
isSSL
)

Constructor used by this class and by SMTPSSLTransport subclass.

Method Summary

Modifier and TypeMethod and Description
private void
addressesFailed()

The send failed, fix the address arrays to report the failure correctly.

private boolean
authenticate(String user, String passwd)

Authenticate to the server.

protected OutputStream

Returns:

the stream to write to
bdat
()

Return a stream that will use the SMTP BDAT command to send data.

protected void
checkConnected()

Check if we're in the connected state.

public synchronized void
close()

Overrides javax.mail.Service.close.

Implements java.lang.AutoCloseable.close.

Close the Transport and terminate the connection to the server.
private void
public synchronized void
connect(Socket
the already connected socket
socket
)

Start the SMTP protocol on the given socket, which was already connected by the caller.

private boolean

Returns:

true if any changes were made XXX - This is really quite a hack.
convertTo8Bit
(MimePart part)

If the Part is a text part and has a Content-Transfer-Encoding of "quoted-printable" or "base64", and it obeys the rules for "8bit" encoding, change the encoding to "8bit".

protected OutputStream

Returns:

the stream to write to
data
()

Send the DATA command to the SMTP host and return an OutputStream to which the data is to be written.

protected boolean

Returns:

true if command succeeds
ehlo
(String
our domain
domain
)

Issue the EHLO command.

private void
expandGroups()

Expand any group addresses.

protected void
finalize()

Overrides javax.mail.Service.finalize.

Stop the event dispatcher thread so the queue can be garbage collected.
protected void
finishBdat()

Terminate the sent data.

protected void
finishData()

Terminate the sent data.

public synchronized String

Returns:

the authorization ID to use for authentication.
getAuthorizationId
()

Gets the authorization ID to be used for authentication.

public String

Returns:

the extension parameter
getExtensionParameter
(String
the service extension name
ext
)

Return the parameter the server provided for the specified service extension, or null if the extension isn't supported.

public synchronized int

Returns:

return code from last response from server
getLastReturnCode
()

Return the return code from the last response we got from the server.

public synchronized String

Returns:

last response from server
getLastServerResponse
()

Return the last response we got from the server.

public synchronized String

Returns:

the local host name
getLocalHost
()

Get the name of the local host, for use in the EHLO and HELO commands.

public synchronized boolean

Returns:

true if NOOP must return 250
getNoopStrict
()

Is the NOOP command required to return a response code of 250 to indicate success?

public synchronized String

Returns:

the name of the domain to use for NTLM authentication.
getNTLMDomain
()

Gets the NTLM domain to be used for NTLM authentication.

public synchronized boolean

Returns:

true if an exception will be thrown on successful sends.
getReportSuccess
()

Should we report even successful sends by throwing an exception?

public synchronized boolean

Returns:

true if the STARTTLS command will be required
getRequireStartTLS
()

Should we require the STARTTLS command to secure the connection?

public synchronized boolean

Returns:

true if SASL authentication is enabled
getSASLEnabled
()

Is SASL authentication enabled?

public synchronized String[]

Returns:

the array of SASL mechanisms to consider
getSASLMechanisms
()

Get the list of SASL mechanisms to consider if SASL authentication is enabled.

public synchronized String

Returns:

the name of the realm to use for SASL authentication.
getSASLRealm
()

Gets the SASL realm to be used for DIGEST-MD5 authentication.

public synchronized boolean

Returns:

true if the STARTTLS command will be used
getStartTLS
()

Should we use the STARTTLS command to secure the connection if the server supports it?

public synchronized boolean

Returns:

true if SASL should use the canonical host name
getUseCanonicalHostName
()

Should SASL use the canonical host name?

public synchronized boolean

Returns:

true if RSET will be used
getUseRset
()

Should we use the RSET command instead of the NOOP command in the @{link #isConnected isConnected} method?

protected void
helo(String
our domain
domain
)

Issue the HELO command.

private void
private boolean
is8Bit(InputStream is)

Check whether the data in the given InputStream follows the rules for 8bit text.

public synchronized boolean
isConnected()

Overrides javax.mail.Service.isConnected.

Check whether the transport is connected.
private boolean
public synchronized boolean

Returns:

true if using SSL
isSSL
()

Is this Transport using SSL to connect to the server?

public synchronized void
issueCommand(String
the command to send
cmd
,
int
the expected response code (-1 means don't care)
expect
)

Send the command to the server.

private void
issueSendCommand(String cmd, int expect)

Issue a command that's part of sending a message.

private boolean
isTracing()

Is protocol tracing enabled?

protected void
mailFrom()

Issue the MAIL FROM: command to start sending a message.

private String
protected void
notifyTransportListeners(int
the TransportEvent type
type
,
Address[]
valid addresses to which message was sent
validSent
,
Address[]
valid addresses to which message was not sent
validUnsent
,
Address[]
the invalid addresses
invalid
,
Message
the message
msg
)

Overrides javax.mail.Transport.notifyTransportListeners.

Notify all TransportListeners.
private void
openServer(String host, int port)

Connect to host on port and start the SMTP protocol.

private void
openServer()

Start the protocol to the server on serverSocket, assumed to be provided and connected by the caller.

protected synchronized boolean

Returns:

true if connection successful, false if authentication failed
protocolConnect
(String
the name of the host to connect to
host
,
int
the port to use (-1 means use default port)
port
,
String
the name of the user to login as
user
,
String
the user's password
password
)

Overrides javax.mail.Service.protocolConnect.

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

Returns:

server response code
readServerResponse
()

Reads server reponse returning the returnCode as the number.

private void
resumeTracing()

Resume protocol tracing, if it was enabled to begin with.

private boolean

Returns:

true for success
sasllogin
(String[]
the allowed SASL mechanisms
allowed
,
String
the SASL realm
realm
,
String
the authorization ID
authzid
,
String
the user name for authentication
u
,
String
the password for authentication
p
)

SASL-based login.

protected void
sendCommand(String
the command
cmd
)

Sends command cmd to the server terminating it with CRLF.

private void
sendCommand(byte[] cmdBytes)

public synchronized void
sendMessage(Message
The MimeMessage to be sent
message
,
Address[]
List of addresses to send this message to
addresses
)

Implements abstract javax.mail.Transport.sendMessage.

Send the Message to the specified list of addresses.
private void
private void
public synchronized void
setAuthorizationID(String
the authorization ID to use for authentication.
authzid
)

Sets the authorization ID to be used for authentication.

public synchronized void
setLocalHost(String
the local host name
localhost
)

Set the name of the local host, for use in the EHLO and HELO commands.

public synchronized void
setNoopStrict(boolean
is NOOP required to return 250?
noopStrict
)

Set whether the NOOP command is required to return a response code of 250 to indicate success.

public synchronized void
setNTLMDomain(String
the name of the domain to use for NTLM authentication.
ntlmDomain
)

Sets the NTLM domain to be used for NTLM authentication.

public synchronized void
setReportSuccess(boolean
should we throw an exception on success?
reportSuccess
)

Set whether successful sends should be reported by throwing an exception.

public synchronized void
setRequireStartTLS(boolean
should we require the STARTTLS command?
requireStartTLS
)

Set whether the STARTTLS command should be required.

public synchronized void
setSASLEnabled(boolean
should we enable SASL authentication?
enableSASL
)

Set whether SASL authentication is enabled.

public synchronized void
setSASLMechanisms(String[]
the array of SASL mechanisms to consider
mechanisms
)

Set the list of SASL mechanisms to consider if SASL authentication is enabled.

public synchronized void
setSASLRealm(String
the name of the realm to use for SASL authentication.
saslRealm
)

Sets the SASL realm to be used for DIGEST-MD5 authentication.

public synchronized void
setStartTLS(boolean
should we use the STARTTLS command?
useStartTLS
)

Set whether the STARTTLS command should be used.

public synchronized void
setUseCanonicalHostName(boolean
should SASL use the canonical host name?
useCanonicalHostName
)

Set whether SASL should use the canonical host name.

public synchronized void
setUseRset(boolean
should we use the RSET command?
useRset
)

Set whether the RSET command should be used instead of the NOOP command in the @{link #isConnected isConnected} method.

public synchronized int

Returns:

the response code
simpleCommand
(String
the command
cmd
)

Send the command to the server and return the response code from the server.

protected int

Returns:

the response code
simpleCommand
(byte[]
the command
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

Returns:

true if the authentication mechanism is supported
supportsAuthentication
(String
the authentication mechanism
auth
)

Does the server we're connected to support the specified authentication mechanism?

public boolean

Returns:

true if the extension is supported
supportsExtension
(String
the service extension name
ext
)

Return true if the SMTP server supports the specified service extension.

private void
suspendTracing()

Temporarily turn off protocol tracing, e.g., to prevent tracing the authentication sequence, including the password.

private byte[]
toBytes(String s)

Convert the String to either ASCII or UTF-8 bytes depending on allowutf8.

private String
private String
protected static String

Returns:

the xtext format string
xtext
(String
the string to convert
s
)

Convert a string to RFC 1891 xtext format.

protected static String

Returns:

the xtext format string
xtext
(String
the string to convert
s
,
boolean
convert string to UTF-8 first?
utf8
)

Like xtext(s), but allow UTF-8 strings.

Inherited from javax.mail.Transport:
addTransportListenerremoveTransportListenersendsendsendsend