public class MailSender extends Object
Refer to send(Email, boolean)
for details.
On a technical note, this is the most complex class in the library (aside from the SOCKS5 bridging server), because it deals with optional asynchronous mailing requests and an optional proxy server that needs to be started and stopped on the fly depending on how many emails are (still) being sent. Especially the combination of asynchronous emails and synchronous emails needs to be managed properly.
Constructor and Description |
---|
MailSender(javax.mail.Session session,
ProxyConfig proxyConfig,
TransportStrategy transportStrategy)
Configures proxy:
configureSessionWithProxy(ProxyConfig, Session, TransportStrategy) (ProxyConfig, Session, TransportStrategy) |
Modifier and Type | Method and Description |
---|---|
void |
applyProperties(Properties properties) |
javax.mail.Session |
getSession()
For emergencies, when a client really wants access to the internally created
Session instance. |
boolean |
isTransportModeLoggingOnly() |
void |
send(Email email,
boolean async)
Processes an
Email instance into a completely configured Message . |
void |
setDebug(boolean debug)
Refer to Session
Session.setDebug(boolean) |
void |
setSessionTimeout(int sessionTimeout) |
void |
setThreadPoolSize(int threadPoolSize) |
void |
setTransportModeLoggingOnly(boolean transportModeLoggingOnly)
Sets the transport mode for this mail sender to logging only, which means no mail will be actually sent out.
|
void |
trustAllHosts(boolean trustAllHosts)
Configures the current session to trust all hosts and don't validate any SSL keys.
|
void |
trustHosts(String... hosts)
Configures the current session to white list all provided hosts and don't validate SSL keys for them.
|
public MailSender(javax.mail.Session session, ProxyConfig proxyConfig, TransportStrategy transportStrategy)
configureSessionWithProxy(ProxyConfig, Session, TransportStrategy)
(ProxyConfig, Session, TransportStrategy)
Also initializes:
sessionTimeout
from properties or default DEFAULT_SESSION_TIMEOUT_MILLIS
threadPoolSize
from properties or default DEFAULT_POOL_SIZE
transportModeLoggingOnly
from properties or default DEFAULT_MODE_LOGGING_ONLY
public final void send(Email email, boolean async)
Email
instance into a completely configured Message
.
Sends the Sun JavaMail Message
object using Session.getTransport()
. It will call Service.connect()
assuming all
connection details have been configured in the provided Session
instance and finally Transport.sendMessage(Message,
Address[])
.
Performs a call to Message.saveChanges()
as the Sun JavaMail API indicates it is needed to configure the message headers and providing
a message id.
If the email should be sent asynchrounously - perhaps as part of a batch, then a new thread is started using the executor
for
threadpooling.
If the email should go through an authenticated proxy server, then the SOCKS proxy bridge is started if not already running. When the last email in a batch has finished, the proxy bridging server is shut down.
email
- The information for the email to be sent.async
- If false, this method blocks until the mail has been processed completely by the SMTP server. If true, a new thread is started to
send the email and this method returns immediately.MailException
- Can be thrown if an email isn't validating correctly, or some other problem occurs during connection, sending etc.Executors.newFixedThreadPool(int)
public void setDebug(boolean debug)
Session.setDebug(boolean)
public void trustAllHosts(boolean trustAllHosts)
Refer to https://javamail.java.net/nonav/docs/api/com/sun/mail/smtp/package-summary.html#mail.smtp.ssl.trust
public void trustHosts(String... hosts)
Refer to https://javamail.java.net/nonav/docs/api/com/sun/mail/smtp/package-summary.html#mail.smtp.ssl.trust
public void applyProperties(Properties properties)
properties
- Properties which will be added to the current Session
instance.public javax.mail.Session getSession()
Session
instance.public void setThreadPoolSize(int threadPoolSize)
threadPoolSize
- The maximum number of threads when sending emails in async fashion.ConfigLoader.Property.DEFAULT_POOL_SIZE
public void setSessionTimeout(int sessionTimeout)
sessionTimeout
- The timeout to use when sending emails (affects socket connect-, read- and write timeouts).ConfigLoader.Property.DEFAULT_SESSION_TIMEOUT_MILLIS
public void setTransportModeLoggingOnly(boolean transportModeLoggingOnly)
public boolean isTransportModeLoggingOnly()
transportModeLoggingOnly
Copyright © 2017. All rights reserved.