Class SMTPClient
This class provides a simple, concrete API for connecting to SMTP
servers. It internally creates a TCPTransportFactory,
ClientEndpoint, and SMTPClientProtocolHandler, wiring
them together and forwarding lifecycle events to the caller's
ServerGreeting handler.
Plaintext with STARTTLS (submission)
SMTPClient client = new SMTPClient(selectorLoop, "smtp.example.com", 587);
client.setSSLContext(sslContext);
client.connect(new ServerGreeting() {
public void handleGreeting(ClientHelloState hello,
String message, boolean esmtp) {
hello.ehlo("myhostname", ehloHandler);
}
// ...
});
Implicit TLS (SMTPS)
SMTPClient client = new SMTPClient("smtp.example.com", 465);
client.setSecure(true);
client.setSSLContext(sslContext);
client.connect(greetingHandler);
- Author:
- Chris Burdess
- See Also:
-
ServerGreetingSMTPClientProtocolHandler- RFC 5321 (SMTP)
- RFC 8314 (Implicit TLS, SMTPS port 465)
- RFC 3207 (STARTTLS)
-
Constructor Summary
ConstructorsConstructorDescriptionSMTPClient(String host, int port) Creates an SMTP client for the given hostname and port.SMTPClient(InetAddress host, int port) Creates an SMTP client for the given address and port.SMTPClient(SelectorLoop selectorLoop, String host, int port) Creates an SMTP client with an explicit selector loop.SMTPClient(SelectorLoop selectorLoop, InetAddress host, int port) Creates an SMTP client with an explicit selector loop and address. -
Method Summary
Modifier and TypeMethodDescriptionvoidclose()Closes the connection.voidconnect(ServerGreeting handler) Connects to the remote SMTP server.booleanisOpen()Returns whether the connection is open.voidsetKeystoreFile(String path) voidsetKeystoreFile(Path path) Sets the keystore file for client certificate authentication.voidsetKeystoreFormat(String format) Sets the keystore format (e.g.voidsetKeystorePass(String password) Sets the keystore password.voidsetSecure(boolean secure) Sets whether this client uses implicit TLS (SMTPS).voidsetSSLContext(SSLContext context) Sets the SSL context for TLS connections.voidsetTrustManager(X509TrustManager trustManager) Sets a custom trust manager for TLS certificate verification.
-
Constructor Details
-
SMTPClient
Creates an SMTP client for the given hostname and port.Uses the next available worker loop from the global
Gumdropinstance. DNS resolution is deferred untilconnect(org.bluezoo.gumdrop.smtp.client.handler.ServerGreeting)is called.- Parameters:
host- the remote hostname or IP addressport- the remote port
-
SMTPClient
Creates an SMTP client with an explicit selector loop.DNS resolution is deferred until
connect(org.bluezoo.gumdrop.smtp.client.handler.ServerGreeting)is called.- Parameters:
selectorLoop- the selector loop, or null to use a Gumdrop workerhost- the remote hostname or IP addressport- the remote port
-
SMTPClient
Creates an SMTP client for the given address and port.- Parameters:
host- the remote host addressport- the remote port
-
SMTPClient
Creates an SMTP client with an explicit selector loop and address.- Parameters:
selectorLoop- the selector loop, or null to use a Gumdrop workerhost- the remote host addressport- the remote port
-
-
Method Details
-
setSecure
public void setSecure(boolean secure) Sets whether this client uses implicit TLS (SMTPS).When true, the connection starts with TLS immediately (port 465). When false, the connection starts plaintext and STARTTLS can be used to upgrade if an SSLContext is configured.
- Parameters:
secure- true for implicit TLS- See Also:
-
- RFC 8314 — implicit TLS (port 465)
-
setSSLContext
Sets the SSL context for TLS connections.Required for both implicit TLS (
setSecure(true)) and explicit TLS via STARTTLS. When set withoutsetSecure(true), an SSLEngine is created but not activated until the protocol handler callsendpoint.startTLS().- Parameters:
context- the SSL context
-
setTrustManager
Sets a custom trust manager for TLS certificate verification.- Parameters:
trustManager- the trust manager, or null to use defaults- See Also:
-
setKeystoreFile
Sets the keystore file for client certificate authentication.- Parameters:
path- the keystore file path
-
setKeystoreFile
-
setKeystorePass
Sets the keystore password.- Parameters:
password- the keystore password
-
setKeystoreFormat
Sets the keystore format (e.g. JKS, PKCS12).- Parameters:
format- the keystore format
-
connect
Connects to the remote SMTP server.Creates the transport factory, endpoint handler, and client endpoint, then initiates the connection. Lifecycle events are forwarded to the given handler.
- Parameters:
handler- the handler to receive the server greeting and lifecycle events
-
isOpen
public boolean isOpen()Returns whether the connection is open.- Returns:
- true if connected and open
-
close
public void close()Closes the connection.
-