public class Smtp
extends java.lang.Object
implements java.lang.AutoCloseable, java.io.Serializable
Example Usage:
// create new instance Smtp smtp = new Smtp("smtp.myserver.com"); // address the message EmailMessage message = new EmailMessage(); message.setTo("mjones@myserver.com"); message.setFrom("jsmith@myserver.com"); message.setSubject("Meeting today at 8"); message setBody("see you then"); // connect, send the message, disconnect try { smtp.connect(); smtp.send(message); smtp.disconnect(); } catch(Exception e) { System.out.println(e); }
Modifier and Type | Class and Description |
---|---|
static class |
Smtp.Authentication |
Modifier and Type | Field and Description |
---|---|
static int |
AUTH_CRAM_MD5
CRAM_MD5 authentication.
|
static int |
AUTH_LOGIN
Standard username/password authentication.
|
static int |
AUTH_XOAUTH2 |
Constructor and Description |
---|
Smtp()
Constructs a new Smtp instance.
|
Smtp(java.lang.String hostname)
Constructs a new Smtp instance.
|
Modifier and Type | Method and Description |
---|---|
void |
addSmtpListener(SmtpListener listener)
Adds Smtp event listener.
|
void |
clearProxySettings()
Clears proxy server values.
|
void |
close() |
Smtp |
connect()
Connects to SMTP server.
|
void |
disconnect()
Disconnects from SMTP server.
|
Smtp.Authentication |
getAuthentication() |
int |
getConnectTimeout()
Gets the timeout used when opening a connection to SMTP server.
|
java.io.PrintStream |
getDebugStream()
Gets PrintStream used in reporting debug statements.
|
java.lang.String |
getHostname()
Gets the hostname or IP address of SMTP server.
|
int |
getPort()
Gets port of SMTP server.
|
int |
getReadTimeout()
Gets the timeout used when reading responses from SMTP server.
|
int |
getTimeout()
Deprecated.
Replaced by getConnectTimeout and getReadTimeout
|
boolean |
isConnected()
Checks if client is connected to SMTP server.
|
boolean |
isDebug()
Gets debug level of SMTP session.
|
java.lang.String |
issueCommand(java.lang.String command)
Sends command to SMTP server Command must be a valid SMTP command.
|
void |
login(java.lang.String username,
java.lang.String password)
Login to SMTP server using standard username / password authentication.
|
void |
login(java.lang.String username,
java.lang.String password,
int authenticationCode)
Login to SMTP server using provided username, password and
authentication mode.
|
void |
login(java.lang.String username,
java.lang.String password,
Smtp.Authentication authentication) |
void |
removeSmtpListener(SmtpListener listener)
Removes specified Smtp event listener.
|
void |
send(EmailMessage message)
Sends email message.
|
void |
send(EmailMessage message,
java.lang.String from,
java.lang.String to)
Sends email message.
|
void |
send(java.lang.String from,
java.lang.String to,
java.lang.String subject,
java.lang.String body)
Sends basic email message.
|
Smtp |
setAuthentication(Smtp.Authentication value) |
void |
setConnectTimeout(int timeout)
Sets the timeout for opening the connection.
|
void |
setDebug(boolean debug)
Sets debugging state.
|
void |
setDebugStream(java.io.PrintStream debugStream)
Sets PrintStream used in reporting debug statements.
|
void |
setHeloAddress(java.lang.String heloAddress)
Sets the source address to use when issuing an EHLO or HELO command to SMTP server.
|
void |
setHostname(java.lang.String hostname)
Sets the hostname or IP address of SMTP server.
|
void |
setPort(int port)
Sets port of SMTP server.
|
void |
setProxyAuthentication(java.lang.String proxyUsername,
java.lang.String proxyPassword)
Sets the username and password to use when for authentication with
proxy server.
|
void |
setProxyHost(java.lang.String proxyHostname,
int proxyPort)
Sets the proxy hostname and port for this connection.
|
void |
setProxyType(java.lang.String proxyType)
Sets the proxy type will be used for this connection.
|
void |
setReadTimeout(int timeout)
Sets the timeout for reading responses from SMTP server.
|
void |
setTimeout(int timeout)
Deprecated.
Replaced by setConnectTimeout and setReadTimeout
|
public static final int AUTH_LOGIN
public static final int AUTH_CRAM_MD5
public static final int AUTH_XOAUTH2
public Smtp()
public Smtp(java.lang.String hostname)
hostname
- the hostname of SMTP serverpublic void setConnectTimeout(int timeout)
timeout
- the timeout in millisecondspublic int getConnectTimeout()
public Smtp.Authentication getAuthentication()
public Smtp setAuthentication(Smtp.Authentication value)
public boolean isConnected()
public void setDebug(boolean debug)
debug
- true
to turn debugging onsetDebugStream(java.io.PrintStream)
public boolean isDebug()
true
if debugging is onsetDebugStream(java.io.PrintStream)
public void setDebugStream(java.io.PrintStream debugStream)
debugStream
- the PrintStream to send debug statements toPrintStream
public java.io.PrintStream getDebugStream()
PrintStream
public void setHeloAddress(java.lang.String heloAddress)
heloAddress
- the IP address or hostname to usepublic void setHostname(java.lang.String hostname)
hostname
- the hostname of SMTP serverpublic java.lang.String getHostname()
public void setPort(int port)
port
- the port of SMTP serverpublic int getPort()
public void setReadTimeout(int timeout)
timeout
- the timeout in millisecondspublic int getReadTimeout()
public void setTimeout(int timeout)
timeout
- the timeout in millisecondspublic int getTimeout()
public void setProxyAuthentication(java.lang.String proxyUsername, java.lang.String proxyPassword)
#clearProxySettings
method.proxyUsername
- the proxy usernameproxyPassword
- the proxy passwordclearProxySettings()
public void setProxyHost(java.lang.String proxyHostname, int proxyPort)
#clearProxySettings
method.proxyHostname
- the hostname or ip address of the proxy serverproxyPort
- the port of the proxy serverclearProxySettings()
public void setProxyType(java.lang.String proxyType)
proxyType
- The proxy type. Valid values: HTTP, SOCKS5public void clearProxySettings()
public void addSmtpListener(SmtpListener listener)
listener
- the SmtpListener to be addedSmtpListener
public Smtp connect() throws SmtpException
SmtpException
- thrown for I/O or SMTP related errorpublic void disconnect() throws SmtpException
SmtpException
- thrown to handle SMTP and I/O related errorsSmtpException
public void close() throws java.lang.Exception
close
in interface java.lang.AutoCloseable
java.lang.Exception
public java.lang.String issueCommand(java.lang.String command) throws SmtpException
command
- an SMTP commandSmtpException
- if an I/O or SMTP related error occurspublic void login(java.lang.String username, java.lang.String password, int authenticationCode) throws SmtpException
username
- the SMTP usernamepassword
- the SMTP passwordauthenticationCode
- the authentication mode to useSmtpException
- thrown for I/O or SMTP related errorAUTH_LOGIN
,
AUTH_CRAM_MD5
public void login(java.lang.String username, java.lang.String password, Smtp.Authentication authentication) throws SmtpException
SmtpException
public void login(java.lang.String username, java.lang.String password) throws SmtpException
username
- the SMTP usernamepassword
- the SMTP passwordSmtpException
- thrown for I/O or SMTP related errorAUTH_LOGIN
public void removeSmtpListener(SmtpListener listener)
listener
- the SmtpListener to be removedSmtpListener
public void send(java.lang.String from, java.lang.String to, java.lang.String subject, java.lang.String body) throws SmtpException, MimeException
from
- the from email addressto
- the to email addresssubject
- the subject of the email messagebody
- the body of the email messageSmtpException
- if an I/O or SMTP related error occursMimeException
- if a MIME related error occurspublic void send(EmailMessage message, java.lang.String from, java.lang.String to) throws SmtpException
message
- an EmailMessage
from
- the email address to send this message from when
communicating with SMTP server, overriding the From header in
the messageto
- the email address to send this message to when communicating
with SMTP server, overriding the To header in the messageSmtpException
- if an I/O or SMTP related error occursEmailMessage
public void send(EmailMessage message) throws SmtpException
message
- an EmailMessage
SmtpException
- if an I/O or SMTP related error occursEmailMessage
Copyright © JSCAPE LLC. 1999-2021. All Rights Reserved