public interface Mailer
MailerRegularBuilder
. This class is the facade to most Simple Java Mail functionality
which is related to doing things with an email (server not always relevant, like with validation, S/MIME encryption etc.).
The e-mail message structure is built to work with all e-mail clients and has been tested with many different webclients as well as some desktop applications. You can experiment with the various types of emails and resulting mime structure on the Simple Java mail website.
Note: if the batch-module is loaded when building a mailer, it will also register itself with the cluster using the provided or random cluster key, so other mailers using the same cluster key immediately start having access to this new server.
MailerRegularBuilder
,
Email
Modifier and Type | Method and Description |
---|---|
@NotNull EmailGovernance |
getEmailGovernance() |
@NotNull OperationalConfig |
getOperationalConfig() |
@NotNull ProxyConfig |
getProxyConfig() |
@Nullable ServerConfig |
getServerConfig() |
jakarta.mail.Session |
getSession()
In case Simple Java Mail falls short somehow, you can get a hold of the internal
Session instance to debug or tweak. |
@Nullable TransportStrategy |
getTransportStrategy() |
@NotNull CompletableFuture<Void> |
sendMail(Email email)
Delegates to
sendMail(Email, boolean) , with async = false . |
@NotNull CompletableFuture<Void> |
sendMail(Email email,
boolean async)
Processes an
Email instance into a completely configured Message . |
Future<?> |
shutdownConnectionPool()
Shuts down the connection pool associated with this
Mailer instance and closes remaining open connections. |
void |
testConnection()
Delegates to
testConnection(boolean) with async == false . |
@NotNull CompletableFuture<Void> |
testConnection(boolean async)
Tries to connect to the configured SMTP server, including (authenticated) proxy if set up.
|
boolean |
validate(Email email)
Validates an
Email instance. |
jakarta.mail.Session getSession()
Session
instance to debug or tweak. Please let us know
why you are needing this on https://github.com/bbottema/simple-java-mail/issues.void testConnection()
testConnection(boolean)
with async == false
.@NotNull @NotNull CompletableFuture<Void> testConnection(boolean async)
Note: synchronizes on the thread for sending mails so that we don't get into race condition conflicts with emails actually being sent.
CompletableFuture
that is completed immediately if not async.@NotNull @NotNull CompletableFuture<Void> sendMail(Email email)
sendMail(Email, boolean)
, with async = false
. This method returns only when the email has been processed by
the target SMTP server.CompletableFuture
that is completed immediately if not async.@NotNull @NotNull CompletableFuture<Void> sendMail(Email email, boolean async)
Email
instance into a completely configured Message
.
Sends the 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,
jakarta.mail.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 thread pooling.
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.CompletableFuture
that is completed immediately if not async.MailException
- Can be thrown if an email isn't validating correctly, or some other problem occurs during connection, sending etc.Executors.newFixedThreadPool(int)
,
validate(Email)
boolean validate(Email email) throws MailException
Email
instance. Validation fails if the subject is missing, content is missing, or no recipients are defined or that
the addresses are missing for NPM notification flags.
It also checks for illegal characters that would facilitate injection attacks:
email
- The email that needs to be configured correctly.true
(throws a MailException
exception if validation fails).MailException
- Is being thrown in any of the above causes.EmailValidator
Future<?> shutdownConnectionPool()
Mailer
instance and closes remaining open connections. Waits until all connections still in use become available again
to deallocate them as well.
Note: In order to shut down the whole connection pool (in case of clustering), each individual Mailer
instance should be shutdown.
Note: This does *not* shut down the executor service if it was provided by the user.
Note: this is only works in combination with the "Advanced batch processing module".
@Nullable @Nullable ServerConfig getServerConfig()
null
in case a custom fixed Session
instance is used.MailerRegularBuilder.withSMTPServer(String, Integer, String, String)
@Nullable @Nullable TransportStrategy getTransportStrategy()
null
in case a custom fixed Session
instance is used.MailerRegularBuilder.withTransportStrategy(TransportStrategy)
,
EmailValidator
@NotNull @NotNull ProxyConfig getProxyConfig()
@NotNull @NotNull OperationalConfig getOperationalConfig()
@NotNull @NotNull EmailGovernance getEmailGovernance()
Copyright © 2009–2022. All rights reserved.