public class Mailer extends Object
MailerBuilder
, aimed for simplicity for sending e-mails of any complexity. This includes e-mails
with plain text and/or html content, embedded images and separate attachments, SMTP, SMTPS / SSL and SMTP + SSL, custom Session object, DKIM domain
signing and even authenticated SOCKS proxy support and threaded batch processing.
This mailing tool abstracts the javax.mail API to a higher level easy to use API. This tool works with Email
instances but can also convert
traditional MimeMessage
objects to and from Email
object.
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.
Technically, the resulting email structure is as follows:
- mixed root - related - alternative - mail tekst - mail html tekst - embedded images - forwarded message - attachments
Usage example (see simplejavamail.org/features for examples):
Email email = EmailBuilder.startingBlank() .from("lollypop", "[email protected]"); .to("Sugar Cane", "[email protected]"); .withPlainText("We should meet up!!"); .withHTMLText("<b>We should meet up!</b>"); .withSubject("Hey"); MailerBuilder.usingSession(preconfiguredMailSession) .buildMailer() .sendMail(email); // or: MailerBuilder.withSMTPServer("smtp.someserver.com", 25, "username", "password") .buildMailer() .sendMail(email);
On a technical note, the Mailer
class is the front facade for the public API. It limits itself to preparing for sending, but the actual
sending and proxy configuration is done by the internal MailSender
.
MailerBuilder
,
Email
Modifier and Type | Method and Description |
---|---|
static javax.mail.Session |
createMailSession(org.simplejavamail.mailer.ServerConfig serverConfig,
TransportStrategy transportStrategy)
Instantiates and configures the
Session instance. |
EnumSet<org.hazlewood.connor.bottema.emailaddress.EmailAddressCriteria> |
getEmailAddressCriteria() |
org.simplejavamail.mailer.internal.mailsender.OperationalConfig |
getOperationalConfig() |
org.simplejavamail.mailer.internal.mailsender.ProxyConfig |
getProxyConfig() |
org.simplejavamail.mailer.ServerConfig |
getServerConfig() |
javax.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. |
TransportStrategy |
getTransportStrategy() |
void |
sendMail(Email email)
Delegates to
sendMail(Email, boolean) , with async = false . |
void |
sendMail(Email email,
boolean async) |
static javax.mail.internet.MimeMessage |
signMessageWithDKIM(javax.mail.internet.MimeMessage messageToSign,
Email emailContainingSigningDetails)
Refer to
MimeMessageHelper.signMessageWithDKIM(MimeMessage, Email) |
void |
testConnection()
Tries to connect to the configured SMTP server, including (authenticated) proxy if set up.
|
boolean |
validate(Email email)
Validates an
Email instance. |
public static javax.mail.Session createMailSession(@Nonnull org.simplejavamail.mailer.ServerConfig serverConfig, @Nonnull TransportStrategy transportStrategy)
Session
instance. Delegates resolving transport protocol specific properties to the given TransportStrategy
in two ways: Furthermore adds proxy SOCKS properties if a proxy configuration was provided, overwriting any SOCKS properties already present.
serverConfig
- Remote SMTP server details.transportStrategy
- The transport protocol strategy enum that actually handles the session configuration. Session configuration meaning
setting the right properties for the appropriate transport type (ie. "mail.smtp.host" for SMTP,
"mail.smtps.host" for SMTPS).Session
instance complete with transport protocol settings.TransportStrategy.generateProperties()
,
TransportStrategy.propertyNameHost()
,
TransportStrategy.propertyNamePort()
,
TransportStrategy.propertyNameUsername()
,
TransportStrategy.propertyNameAuthenticate()
public javax.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.@Nullable public org.simplejavamail.mailer.ServerConfig getServerConfig()
null
in case a custom fixed Session
instance is used.@Nullable public TransportStrategy getTransportStrategy()
null
in case a custom fixed Session
instance is used.@Nonnull public org.simplejavamail.mailer.internal.mailsender.ProxyConfig getProxyConfig()
@Nonnull public org.simplejavamail.mailer.internal.mailsender.OperationalConfig getOperationalConfig()
@Nonnull public EnumSet<org.hazlewood.connor.bottema.emailaddress.EmailAddressCriteria> getEmailAddressCriteria()
public void testConnection()
Note: synchronizes on the thread for sending mails so that we don't get into race condition conflicts with emails actually being sent.
public final 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.public final void sendMail(Email email, boolean async)
MailSender.send(Email, boolean)
,
validate(Email)
public 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.EmailAddressValidator
public static javax.mail.internet.MimeMessage signMessageWithDKIM(javax.mail.internet.MimeMessage messageToSign, Email emailContainingSigningDetails)
MimeMessageHelper.signMessageWithDKIM(MimeMessage, Email)
Copyright © 2018. All rights reserved.