public class SmtpSession extends Object
SmtpSession
instances can be created with SmtpSessionFactory.connect(SmtpSessionConfig)
.
This class is thread-safe.
Modifier and Type | Method and Description |
---|---|
CompletableFuture<SmtpClientResponse> |
authLogin(String username,
String password)
Authenticates with the remote server using the LOGIN mechanism.
|
CompletableFuture<SmtpClientResponse> |
authPlain(String username,
String password)
Authenticates with the remote server using the PLAIN mechanism.
|
CompletableFuture<SmtpClientResponse> |
authXoauth2(String username,
String accessToken)
Authenticates with the remote server using the XOAUTH2 mechanism.
|
CompletableFuture<Void> |
close()
Closes this session.
|
CompletableFuture<Void> |
getCloseFuture()
Returns a
CompletableFuture that will be completed when this session is closed. |
String |
getConnectionId()
Returns the
connectionId defined by the SmtpSessionConfig used when connecting to this server. |
EhloResponse |
getEhloResponse()
Returns an
EhloResponse representing the capabilities of the connection server. |
Optional<SSLSession> |
getSSLSession()
Returns the
SSLSession used by the current session, or Optional.empty() if the session
is not using TLS. |
boolean |
isEncrypted()
Returns true if TLS is active on the session.
|
CompletableFuture<SmtpClientResponse> |
send(MessageContent content)
Sends message content to the remote server and waits for a response.
|
CompletableFuture<SmtpClientResponse> |
send(io.netty.handler.codec.smtp.SmtpRequest request)
Sends a command to the remote server and waits for a response.
|
CompletableFuture<SmtpClientResponse> |
send(String from,
Collection<String> recipients,
MessageContent content)
Sends an email as efficiently as possible, using the extended SMTP features supported by the remote server.
|
CompletableFuture<SmtpClientResponse> |
send(String from,
Collection<String> recipients,
MessageContent content,
SendInterceptor sendInterceptor)
Sends an email as efficiently as possible, using the extended SMTP features supported by the remote server.
|
CompletableFuture<SmtpClientResponse> |
send(String from,
String to,
MessageContent content)
Sends an email as efficiently as possible, using the extended SMTP features supported by the remote server.
|
CompletableFuture<SmtpClientResponse> |
send(String from,
String to,
MessageContent content,
SendInterceptor sendInterceptor)
Sends an email as efficiently as possible, using the extended SMTP features supported by the remote server.
|
CompletableFuture<SmtpClientResponse> |
sendChunk(io.netty.buffer.ByteBuf data,
boolean isLast)
Sends binary message content to the remote server and waits for a response.
|
CompletableFuture<SmtpClientResponse> |
sendPipelined(MessageContent content,
io.netty.handler.codec.smtp.SmtpRequest... requests)
Sends a series of commands to the server without waiting for a response.
|
CompletableFuture<SmtpClientResponse> |
sendPipelined(io.netty.handler.codec.smtp.SmtpRequest... requests)
Sends a series of commands to the server without waiting for a response.
|
CompletableFuture<SmtpClientResponse> |
startTls()
Sends the STARTTLS command and tries to use TLS for this session.
|
public String getConnectionId()
connectionId
defined by the SmtpSessionConfig
used when connecting to this server.public CompletableFuture<Void> getCloseFuture()
CompletableFuture
that will be completed when this session is closed.public EhloResponse getEhloResponse()
EhloResponse
representing the capabilities of the connection server.
If the EHLO command has not been sent, or the response could not be parsed, this
method will return an empty EhloResponse
that assumes no extended SMTP features
are available.
public CompletableFuture<Void> close()
CompletableFuture
that will be completed when the session is closed.public CompletableFuture<SmtpClientResponse> startTls()
If the server returns an error response (with code >= 400
), this method
will return a CompletableFuture<SmtpClientResponse>
with that error response
and TLS will not be active.
If the TLS handshake fails, this method will return a CompletableFuture<SmtpClientResponse>
that contains the exception thrown by the handshake process.
Once the TLS handshake has completed, the EHLO command will be sent again in accordance
with the spec. The EHLO response will be parsed and available from getEhloResponse()
.
IllegalStateException
- if TLS is already activepublic boolean isEncrypted()
public Optional<SSLSession> getSSLSession()
SSLSession
used by the current session, or Optional.empty()
if the session
is not using TLS.public CompletableFuture<SmtpClientResponse> send(String from, String to, MessageContent content)
send(String, Collection, MessageContent, SendInterceptor)
but
only specifies one recipient and does not use a SendInterceptor
.public CompletableFuture<SmtpClientResponse> send(String from, String to, MessageContent content, SendInterceptor sendInterceptor)
send(String, Collection, MessageContent, SendInterceptor)
but
only specifies one recipient.public CompletableFuture<SmtpClientResponse> send(String from, Collection<String> recipients, MessageContent content)
send(String, Collection, MessageContent, SendInterceptor)
but
does not use a SendInterceptor
.public CompletableFuture<SmtpClientResponse> send(String from, Collection<String> recipients, MessageContent content, SendInterceptor sendInterceptor)
This method sends multiple commands to the remote server, and may use pipelining and other features
if supported. If any command receives an error response (i.e. with code >= 400
),
the sequence of commands is aborted.
Because this method adapts to the capabilities of the server, it may send different sequences of commands for the same inputs. For example, communication with a server without extended SMTP support might look as follows:
CLIENT: MAIL FROM:<from@example.com>
SERVER: 250 OK
CLIENT: RCPT TO:<to@example.com>
SERVER: 250 OK
CLIENT: DATA
SERVER: 354 Start mail input; end with <CRLF>.
CLIENT: <message data>
Communication with a modern server that supports pipelining and chunking might look different:
CLIENT: MAIL FROM:<from@example.com>
CLIENT: RCPT TO:<to@example.com>
CLIENT: BDAT 7287 LAST
CLIENT: <binary message data>
SERVER: 250 OK
SERVER: 250 OK
SERVER: 250 OK
This command assumes the EHLO command has been sent and its result has been parsed.
Dot-stuffing will be performed automatically unless SMTP chunking (which does not require dot-stuffing) is available.
from
- the sender of the message, surrounded by < and >, e.g. "<alice@example.com>"
recipients
- a list of the intended recipients, each surrounded by < and >, e.g. ["<bob@example.com>", "<carol@example.com>"]
content
- a MessageContent
with the contents of the messagesendInterceptor
- a SendInterceptor
which will be called before commands and data are sentCompletableFuture<SmtpClientResponse>
that will contain each of the responses received
from the remote server, or an exception if the send failed unexpectedly.NullPointerException
- if any of the arguments are nullIllegalArgumentException
- if recipients
is emptyMessageTooLargeException
- if the EHLO response indicated a maximum message size that would be
exceeded by content
. Note if MessageContent.size()
is not specified this check
is not performed.public CompletableFuture<SmtpClientResponse> send(io.netty.handler.codec.smtp.SmtpRequest request)
If the command is EHLO, the server's response will be parsed and available from
getEhloResponse()
.
request
- the command and arguments to sendCompletableFuture<SmtpClientResponse>
that will contain the response received
from the remote server, or an exception if the send failed unexpectedly.NullPointerException
- if request is nullpublic CompletableFuture<SmtpClientResponse> send(MessageContent content)
This method should be called when the server is in the "waiting for data" state, i.e. when the DATA command has been sent and the server has returned a 354 response.
content
- the message contents to sendCompletableFuture<SmtpClientResponse>
that will contain the response received
from the remote server, or an exception if the send failed unexpectedly.NullPointerException
- if content is nullMessageTooLargeException
- if the EHLO response indicated a maximum message size that would be
exceeded by content
. Note if MessageContent.size()
is not specified this check
is not performed.public CompletableFuture<SmtpClientResponse> sendChunk(io.netty.buffer.ByteBuf data, boolean isLast)
This method sends a BDAT command, immediately followed by the message contents, and depends on server support for SMTP chunking.
data
- the message contents to sendisLast
- whether this is the last message chunk, and should included the LAST keyword
with the BDAT command.CompletableFuture<SmtpClientResponse>
that will contain the response received
from the remote server, or an exception if the send failed unexpectedly.NullPointerException
- if data is nullIllegalStateException
- if the server does not support chunking, or if the EHLO response has
not been received.MessageTooLargeException
- if the EHLO response indicated a maximum message size that would be
exceeded by content
. Note if MessageContent.size()
is not specified this check
is not performed.public CompletableFuture<SmtpClientResponse> sendPipelined(io.netty.handler.codec.smtp.SmtpRequest... requests)
This is identical to sendPipelined(MessageContent, SmtpRequest...)
but it
does not send any message content.
public CompletableFuture<SmtpClientResponse> sendPipelined(MessageContent content, io.netty.handler.codec.smtp.SmtpRequest... requests)
This method reduces latency by sending all the specified commands without waiting for the server to respond to each one in turn. It depends on server support for SMTP pipelining.
The server will send a response for each command included in the pipelined sequence. These are
available from SmtpClientResponse.getResponses()
.
According to the pipelining spec, the contents of one message can be sent together with
a RSET and metadata for a subsequent message. As such, the content
and requests
arguments to this method refer to different e-mails.
content
- the content to sendrequests
- the commands to sendCompletableFuture<SmtpClientResponse>
that will contain the responses received
from the remote server, or an exception if the send failed unexpectedlyNullPointerException
- if requests is nullIllegalArgumentException
- if the sequence of commands in requests
is not valid
according to the pipelining specIllegalStateException
- if the server does not support pipelining, or if the EHLO response has
not been receivedMessageTooLargeException
- if the EHLO response indicated a maximum message size that would be
exceeded by content
. Note if MessageContent.size()
is not specified this check
is not performedpublic CompletableFuture<SmtpClientResponse> authPlain(String username, String password)
username
- the plaintext username used to authenticatepassword
- the plaintext password used to authenticateCompletableFuture<SmtpClientResponse>
that will contain the response received
from the remote server, or an exception if the send failed unexpectedlyIllegalStateException
- if the server does not support PLAIN authentication,
or if the EHLO response has not been receivedpublic CompletableFuture<SmtpClientResponse> authXoauth2(String username, String accessToken)
username
- the plaintext username used to authenticateaccessToken
- the access token used to authenticateCompletableFuture<SmtpClientResponse>
that will contain the response received
from the remote server, or an exception if the send failed unexpectedlyIllegalStateException
- if the server does not support XOAUTH2 authentication,
or if the EHLO response has not been receivedpublic CompletableFuture<SmtpClientResponse> authLogin(String username, String password)
This method will attempt to send two commands to the server: the first includes the
username, the second specifies the password. The returned SmtpClientResponse
will contain the response to just one command - the first if the username was
rejected by the server, or the second if the username was accepted and the password
was sent.
username
- the plaintext username used to authenticatepassword
- the plaintext password used to authenticateCompletableFuture<SmtpClientResponse>
that will contain the response received
from the remote server, or an exception if the send failed unexpectedlyIllegalStateException
- if the server does not support LOGIN authentication,
or if the EHLO response has not been receivedCopyright © 2018. All rights reserved.