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", "lolly.pop@somemail.com");
email.addRecipient("Sugar Cane", "sugar.cane@candystore.org", 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);
Mailer.MimeEmailMessageWrapper,
Email| Modifier and Type | Class and Description |
|---|---|
private class |
Mailer.MimeEmailMessageWrapper
This class conveniently wraps all necessary mimemessage parts that need to be filled with content, attachments etc.
|
| Modifier and Type | Field and Description |
|---|---|
private static String |
CHARACTER_ENCODING
Encoding used for setting body text, email address, headers, reply-to fields etc.
|
private EmailAddressValidationCriteria |
emailAddressValidationCriteria
Email address restriction flags set either by constructor or overridden by getter by user.
|
private static Logger |
logger |
private javax.mail.Session |
session
Used to actually send the email.
|
private TransportStrategy |
transportStrategy
The transport protocol strategy enum that actually handles the session configuration.
|
| 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 |
|---|---|
void |
applyProperties(Properties properties)
Copies all property entries into the
Session using Session.getProperties(). |
protected javax.mail.Session |
createMailSession(String host,
Integer port,
String username,
String password)
Actually instantiates and configures the
Session instance. |
private javax.mail.BodyPart |
getBodyPartFromDatasource(AttachmentResource resource,
String dispositionType)
Helper method which generates a
BodyPart from an AttachmentResource (from its DataSource) and a disposition
type (Part.INLINE or Part.ATTACHMENT). |
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. |
private void |
logSession(javax.mail.Session session,
TransportStrategy transportStrategy)
Simply logs host details, credentials used and whether authentication will take place and finally the transport protocol used.
|
private javax.mail.Message |
prepareMessage(Email email,
Mailer.MimeEmailMessageWrapper messageRoot)
Creates a new
MimeMessage instance and prepares it in the email structure, so that it can be filled and send. |
void |
sendMail(Email email)
Processes an
Email instance into a completely configured Message. |
private void |
setAttachments(Email email,
javax.mail.internet.MimeMultipart multipartRoot)
Fills the
Message instance with the attachments from the Email. |
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. |
private void |
setEmbeddedImages(Email email,
javax.mail.internet.MimeMultipart multipartRelated)
Fills the
Message instance with the embedded images from the Email. |
private void |
setHeaders(Email email,
javax.mail.Message message)
Sets all headers on the
Message instance. |
private void |
setRecipients(Email email,
javax.mail.Message message)
Fills the
Message instance with recipients from the Email. |
private void |
setReplyTo(Email email,
javax.mail.Message message)
Fills the
Message instance with reply-to address. |
private void |
setTexts(Email email,
javax.mail.internet.MimeMultipart multipartAlternativeMessages)
Fills the
Message instance with the content bodies (text and html). |
boolean |
validate(Email email)
Validates an
Email instance. |
private static final Logger logger
private static final String CHARACTER_ENCODING
private final javax.mail.Session session
Mailer
directly, when no Session instance was provided.private TransportStrategy transportStrategy
private EmailAddressValidationCriteria emailAddressValidationCriteria
EmailAddressValidationCriteriapublic Mailer(javax.mail.Session session)
Also leaves email address validation criteria empty so that no validation is being performed. Validation errors will come from the smtp server instead.
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 leaves email address validation criteria empty so that no validation is being performed. Validation errors will come from the smtp server instead.
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)protected 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 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.public void setDebug(boolean debug)
Session.setDebug(boolean) so that it generates debug information.debug - Flag to indicate debug mode yes/no.public void applyProperties(Properties properties)
Session using Session.getProperties().properties - The source properties to add or override in the internal Session instance.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)private void logSession(javax.mail.Session session, TransportStrategy transportStrategy)
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.EmailValidationUtilprivate javax.mail.Message prepareMessage(Email email, Mailer.MimeEmailMessageWrapper messageRoot) throws javax.mail.MessagingException, UnsupportedEncodingException
MimeMessage instance and prepares it in the email structure, so that it can be filled and send.
Fills subject, from,reply-to, content, sent-date, recipients, texts, embedded images, attachments, content and adds all headers.
email - The email message from which the subject and From-address are extracted.messageRoot - The root of the email which holds everything (filled with some email data).Message instance, ready to be sent.javax.mail.MessagingException - May be thrown when the message couldn't be processed by JavaMail.UnsupportedEncodingException - Zie InternetAddress.InternetAddress(String, String).private void setRecipients(Email email, javax.mail.Message message) throws UnsupportedEncodingException, javax.mail.MessagingException
Message instance with recipients from the Email.email - The message in which the recipients are defined.message - The javax message that needs to be filled with recipients.UnsupportedEncodingException - See InternetAddress.InternetAddress(String, String).javax.mail.MessagingException - See Message.addRecipient(javax.mail.Message.RecipientType, Address)private void setReplyTo(Email email, javax.mail.Message message) throws UnsupportedEncodingException, javax.mail.MessagingException
Message instance with reply-to address.email - The message in which the recipients are defined.message - The javax message that needs to be filled with reply-to address.UnsupportedEncodingException - See InternetAddress.InternetAddress(String, String).javax.mail.MessagingException - See Message.setReplyTo(Address[])private void setTexts(Email email, javax.mail.internet.MimeMultipart multipartAlternativeMessages) throws javax.mail.MessagingException
Message instance with the content bodies (text and html).email - The message in which the content is defined.multipartAlternativeMessages - See MimeMultipart.addBodyPart(BodyPart)javax.mail.MessagingException - See Part.setText(String), Part.setContent(Object, String) and
MimeMultipart.addBodyPart(BodyPart).private void setEmbeddedImages(Email email, javax.mail.internet.MimeMultipart multipartRelated) throws javax.mail.MessagingException
Message instance with the embedded images from the Email.email - The message in which the embedded images are defined.multipartRelated - The branch in the email structure in which we'll stuff the embedded images.javax.mail.MessagingException - See MimeMultipart.addBodyPart(BodyPart) and
getBodyPartFromDatasource(AttachmentResource, String)private void setAttachments(Email email, javax.mail.internet.MimeMultipart multipartRoot) throws javax.mail.MessagingException
Message instance with the attachments from the Email.email - The message in which the attachments are defined.multipartRoot - The branch in the email structure in which we'll stuff the attachments.javax.mail.MessagingException - See MimeMultipart.addBodyPart(BodyPart) and
getBodyPartFromDatasource(AttachmentResource, String)private void setHeaders(Email email, javax.mail.Message message) throws UnsupportedEncodingException, javax.mail.MessagingException
Message instance. Since we're not using a high-level JavaMail method, the JavaMail library says we
need to do some encoding and 'folding' manually, to get the value right for the headers (see MimeUtility.email - The message in which the headers are defined.message - The Message on which to set the raw, encoded and folded headers.UnsupportedEncodingException - See MimeUtility.encodeText(String, String, String)javax.mail.MessagingException - See Part.addHeader(String, String)MimeUtility#encodeText(String, String, String)},
MimeUtility.fold(int, String)private javax.mail.BodyPart getBodyPartFromDatasource(AttachmentResource resource, String dispositionType) throws javax.mail.MessagingException
BodyPart from an AttachmentResource (from its DataSource) and a disposition
type (Part.INLINE or Part.ATTACHMENT). With this the attachment data can be converted into objects that fit in the
email structure. resource - An object that describes the attachment and contains the actual content data.dispositionType - The type of attachment, Part.INLINE or Part.ATTACHMENT .javax.mail.MessagingException - All BodyPart setters.public void setEmailAddressValidationCriteria(EmailAddressValidationCriteria emailAddressValidationCriteria)
Mailer
instance. By default no validation will be performed by simple-java-mail, until a criteria object has been set.emailAddressValidationCriteria - Refer to
EmailAddressValidationCriteria.EmailAddressValidationCriteria(boolean, boolean).Copyright © 2009 under Apache v2 license.