public class Mailer extends Object
Email
instances. - root - related - alternative - mail text - mail html text - embedded images - attachments
Email email = new Email(); email.setFromAddress("lollypop", "[email protected]"); email.addRecipient("Sugar Cane", "[email protected]", RecipientType.TO); email.setText("We should meet up!!"); email.setTextHTML("<b>We should meet up!</b>"); email.setSubject("Hey"); new Mailer(preconfiguredMailSession).sendMail(email); // or: new Mailer("smtp.someserver.com", 25, "username", "password").sendMail(email);
MimeEmailMessageWrapper
,
Email
Constructor and Description |
---|
Mailer(javax.mail.Session session)
Default constructor, stores the given mail session for later use.
|
Mailer(String host,
Integer port,
String username,
String password)
Overloaded constructor which produces a new
Session on the fly, using default vanilla SMTP transport protocol. |
Mailer(String host,
Integer port,
String username,
String password,
TransportStrategy transportStrategy)
Overloaded constructor which produces a new
Session on the fly. |
Modifier and Type | Method and Description |
---|---|
javax.mail.Session |
createMailSession(String host,
Integer port,
String username,
String password)
Actually instantiates and configures the
Session instance. |
void |
sendMail(Email email)
Processes an
Email instance into a completely configured Message . |
void |
setDebug(boolean debug)
Actually sets
Session.setDebug(boolean) so that it generates debug information. |
void |
setEmailAddressValidationCriteria(EmailAddressValidationCriteria emailAddressValidationCriteria)
Overrides the default email address validation restrictions when validating and sending emails using the current
Mailer
instance. |
boolean |
validate(Email email)
Validates an
Email instance. |
public Mailer(javax.mail.Session session)
Also defines a default email address validation criteria object, which remains true to RFC 2822, meaning allowing both domain
literals and quoted identifiers (see EmailAddressValidationCriteria.EmailAddressValidationCriteria(boolean, boolean)
).
session
- A preconfigured mail Session
object with which a Message
can be produced.public Mailer(String host, Integer port, String username, String password, TransportStrategy transportStrategy)
Session
on the fly. Use this if you don't have a mail session configured in your
web container, or Spring context etc.
Also defines a default email address validation criteria object, which remains true to RFC 2822, meaning allowing both domain
literals and quoted identifiers (see EmailAddressValidationCriteria.EmailAddressValidationCriteria(boolean, boolean)
).
host
- The address URL of the SMTP server to be used.port
- The port of the SMTP server.username
- An optional username, may be null
.password
- An optional password, may be null
, but only if username is null
as well.transportStrategy
- The transport protocol configuration type for handling SSL or TLS (or vanilla SMTP)public Mailer(String host, Integer port, String username, String password)
Session
on the fly, using default vanilla SMTP transport protocol.host
- The address URL of the SMTP server to be used.port
- The port of the SMTP server.username
- An optional username, may be null
.password
- An optional password, may be null
, but only if username is null
as well.Mailer(String, Integer, String, String, TransportStrategy)
public javax.mail.Session createMailSession(String host, Integer port, String username, String password)
Session
instance. Delegates resolving transport protocol specific properties to the
transportStrategy
in two ways:
host
- The address URL of the SMTP server to be used.port
- The port of the SMTP server.username
- An optional username, may be null
.password
- An optional password, may be null
.Session
instance complete with transport protocol settings.TransportStrategy.generateProperties()
,
TransportStrategy.propertyNameHost()
,
TransportStrategy.propertyNamePort()
,
TransportStrategy.propertyNameUsername()
,
TransportStrategy.propertyNameAuthenticate()
public void setDebug(boolean debug)
Session.setDebug(boolean)
so that it generates debug information.debug
- Flag to indicate debug mode yes/no.public final void sendMail(Email email) throws MailException
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.
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.
email
- The information for the email to be sent.MailException
- Can be thrown if an email isn't validating correctly, or some other problem occurs during connection, sending
etc.validate(Email)
,
prepareMessage(Email, MimeEmailMessageWrapper)
,
setRecipients(Email, Message)
,
setTexts(Email, MimeMultipart)
,
setEmbeddedImages(Email, MimeMultipart)
,
setAttachments(Email, MimeMultipart)
public boolean validate(Email email) throws MailException
Email
instance. Validation fails if the subject is missing, content is missing, or no recipients are defined.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.EmailValidationUtil
public void setEmailAddressValidationCriteria(EmailAddressValidationCriteria emailAddressValidationCriteria)
Mailer
instance.emailAddressValidationCriteria
- Refer to
EmailAddressValidationCriteria.EmailAddressValidationCriteria(boolean, boolean)
.Copyright © 2012. All Rights Reserved.