public class SMTPTransport
extends 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(java.lang.String, int)
and sendCommand(java.lang.String)
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(); }
javax.mail.event.ConnectionEvent
,
javax.mail.event.TransportEvent
Constructor and Description |
---|
SMTPTransport(Session session,
URLName urlname)
Constructor that takes a Session object and a URLName
that represents a specific SMTP server.
|
Modifier and Type | Method and Description |
---|---|
void |
close()
Close the Transport and terminate the connection to the server.
|
void |
connect(java.net.Socket socket)
Start the SMTP protocol on the given socket, which was already
connected by the caller.
|
java.lang.String |
getAuthorizationId()
Gets the authorization ID to be used for authentication.
|
java.lang.String |
getExtensionParameter(java.lang.String ext)
Return the parameter the server provided for the specified
service extension, or null if the extension isn't supported.
|
int |
getLastReturnCode()
Return the return code from the last response we got from the server.
|
java.lang.String |
getLastServerResponse()
Return the last response we got from the server.
|
java.lang.String |
getLocalHost()
Get the name of the local host, for use in the EHLO and HELO commands.
|
boolean |
getNoopStrict()
Is the NOOP command required to return a response code
of 250 to indicate success?
|
java.lang.String |
getNTLMDomain()
Gets the NTLM domain to be used for NTLM authentication.
|
boolean |
getReportSuccess()
Should we report even successful sends by throwing an exception?
If so, a
SendFailedException will always be thrown and
an SMTPAddressSucceededException will be included in the exception
chain for each successful address, along with the usual
SMTPAddressFailedException for each unsuccessful address. |
boolean |
getRequireStartTLS()
Should we require the STARTTLS command to secure the connection?
|
boolean |
getSASLEnabled()
Is SASL authentication enabled?
|
java.lang.String[] |
getSASLMechanisms()
Get the list of SASL mechanisms to consider if SASL authentication
is enabled.
|
java.lang.String |
getSASLRealm()
Gets the SASL realm to be used for DIGEST-MD5 authentication.
|
boolean |
getStartTLS()
Should we use the STARTTLS command to secure the connection
if the server supports it?
|
boolean |
getUseRset()
Should we use the RSET command instead of the NOOP command
in the @{link #isConnected isConnected} method?
|
boolean |
isConnected()
Check whether the transport is connected.
|
boolean |
isSSL()
Is this Transport using SSL to connect to the server?
|
void |
issueCommand(java.lang.String cmd,
int expect)
Send the command to the server.
|
boolean |
sasllogin(java.lang.String[] allowed,
java.lang.String realm,
java.lang.String authzid,
java.lang.String u,
java.lang.String p)
SASL-based login.
|
void |
sendMessage(Message message,
Address[] addresses)
Send the Message to the specified list of addresses.
|
void |
setAuthorizationID(java.lang.String authzid)
Sets the authorization ID to be used for authentication.
|
void |
setLocalHost(java.lang.String localhost)
Set the name of the local host, for use in the EHLO and HELO commands.
|
void |
setNoopStrict(boolean noopStrict)
Set whether the NOOP command is required to return a response code
of 250 to indicate success.
|
void |
setNTLMDomain(java.lang.String ntlmDomain)
Sets the NTLM domain to be used for NTLM authentication.
|
void |
setReportSuccess(boolean reportSuccess)
Set whether successful sends should be reported by throwing
an exception.
|
void |
setRequireStartTLS(boolean requireStartTLS)
Set whether the STARTTLS command should be required.
|
void |
setSASLEnabled(boolean enableSASL)
Set whether SASL authentication is enabled.
|
void |
setSASLMechanisms(java.lang.String[] mechanisms)
Set the list of SASL mechanisms to consider if SASL authentication
is enabled.
|
void |
setSASLRealm(java.lang.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.
|
int |
simpleCommand(java.lang.String cmd)
Send the command to the server and return the response code
from the server.
|
boolean |
supportsExtension(java.lang.String ext)
Return true if the SMTP server supports the specified service
extension.
|
public SMTPTransport(Session session, URLName urlname)
public java.lang.String getLocalHost()
public void setLocalHost(java.lang.String localhost)
public void connect(java.net.Socket socket) throws MessagingException
MessagingException
public java.lang.String getAuthorizationId()
public void setAuthorizationID(java.lang.String authzid)
authzid
- the authorization ID to use for
authentication.public boolean getSASLEnabled()
public void setSASLEnabled(boolean enableSASL)
enableSASL
- should we enable SASL authentication?public java.lang.String getSASLRealm()
public void setSASLRealm(java.lang.String saslRealm)
saslRealm
- the name of the realm to use for
SASL authentication.public java.lang.String[] getSASLMechanisms()
public void setSASLMechanisms(java.lang.String[] mechanisms)
mechanisms
- the array of SASL mechanisms to considerpublic java.lang.String getNTLMDomain()
public void setNTLMDomain(java.lang.String ntlmDomain)
ntlmDomain
- the name of the domain to use for
NTLM authentication.public boolean getReportSuccess()
SendFailedException
will always be thrown and
an SMTPAddressSucceededException
will be included in the exception
chain for each successful address, along with the usual
SMTPAddressFailedException
for each unsuccessful address.public void setReportSuccess(boolean reportSuccess)
reportSuccess
- should we throw an exception on success?public boolean getStartTLS()
public void setStartTLS(boolean useStartTLS)
useStartTLS
- should we use the STARTTLS command?public boolean getRequireStartTLS()
public void setRequireStartTLS(boolean requireStartTLS)
requireStartTLS
- should we require the STARTTLS command?public boolean isSSL()
public boolean getUseRset()
public void setUseRset(boolean useRset)
useRset
- should we use the RSET command?public boolean getNoopStrict()
public void setNoopStrict(boolean noopStrict)
noopStrict
- is NOOP required to return 250?public java.lang.String getLastServerResponse()
public int getLastReturnCode()
public boolean sasllogin(java.lang.String[] allowed, java.lang.String realm, java.lang.String authzid, java.lang.String u, java.lang.String p) throws MessagingException
MessagingException
public 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.
message
- The MimeMessage to be sentaddresses
- List of addresses to send this message toSMTPSendFailedException
- if the send failed because of
an SMTP command errorSendFailedException
- if the send failed because of
invalid addresses.MessagingException
- if the connection is dead
or not in the connected state or if the message is
not a MimeMessage.javax.mail.event.TransportEvent
public void close() throws MessagingException
MessagingException
public boolean isConnected()
public void issueCommand(java.lang.String cmd, int expect) throws MessagingException
cmd
- the command to sendexpect
- the expected response code (-1 means don't care)MessagingException
public int simpleCommand(java.lang.String cmd) throws MessagingException
MessagingException
public boolean supportsExtension(java.lang.String ext)
ext
- the service extension namepublic java.lang.String getExtensionParameter(java.lang.String ext)
ext
- the service extension name"Copyright © 2010 - 2020 Adobe Systems Incorporated. All Rights Reserved"