public class MailMessage
extends java.lang.Object
implements java.io.Serializable
Modifier and Type | Field and Description |
---|---|
protected java.io.File |
ivAlternate
The current email's alternate format file.
|
protected java.util.ArrayList<javax.mail.internet.MimeBodyPart> |
ivAttachments
The current email's file attachments.
|
protected java.lang.Object |
ivContent
The current email's content.
|
protected java.lang.String |
ivContentType
The current email's content type.
|
protected boolean |
ivDebugMode
The current debug mode.
|
protected javax.mail.internet.MimeMessage |
ivMessage
The current email's message object.
|
protected java.io.File |
ivPreferred
The current email's preferred format file.
|
protected boolean |
ivSampleMode
The current sample mode.
|
protected javax.mail.Session |
ivSession
The current STMP server session.
|
protected java.lang.String |
ivSmtpHost
The current STMP host address.
|
private int |
ivSmtpPort
The current SMTP host port.
|
protected java.lang.String |
ivSmtpPwd
The current STMP host password.
|
private boolean |
ivSmtpTLS
The current SMTP host TLS flag.
|
private java.lang.String |
ivSmtpType
The current SMTP host type (transport).
|
protected java.lang.String |
ivSmtpUser
The current STMP host user id.
|
protected java.lang.String |
ivTemplate
The current email's template file name.
|
protected java.lang.String |
ivType
The current email's type.
|
static java.lang.String |
MSG_ALERT
The message type for a site error notification message.
|
static java.lang.String |
MSG_GENERIC
The message type for a generic text message.
|
static java.lang.String |
MSG_SITEMON
The message type for a site monitor message.
|
private static long |
serialVersionUID
The class' serialization version id.
|
Modifier | Constructor and Description |
---|---|
protected |
MailMessage()
Constructs a
MailMessage object. |
|
MailMessage(java.lang.String type)
Constructs a
MailMessage object. |
Modifier and Type | Method and Description |
---|---|
protected void |
addDebugRecipient()
Adds the debug TO name and address (if configured) to the email's list of
BCC recipients.
|
void |
addFileAttachment(java.io.File file)
Adds a file attachment to the email.
|
void |
addFileAttachment(javax.mail.internet.MimeBodyPart attachment)
Adds a file attachment to the email.
|
void |
addFileAttachment(java.lang.String path)
Adds a file attachment to the email.
|
void |
addFrom(java.lang.Object from)
Adds to the email's list of from addresses using the given object (User,
ArrayList, etc.).
|
void |
addFrom(java.lang.String name,
java.lang.String address)
Adds to the email's list of from addresses using the given name and email
address.
|
void |
addRecipientBCC(java.lang.Object bcc)
Adds to the email's list of BCC addresses using the given object (User,
ArrayList, etc.).
|
void |
addRecipientBCC(java.lang.String name,
java.lang.String address)
Adds to the email's list of BCC addresses using the given name and email
address.
|
void |
addRecipientCC(java.lang.Object cc)
Adds to the email's list of CC addresses using the given object (User,
ArrayList, etc.).
|
void |
addRecipientCC(java.lang.String name,
java.lang.String address)
Adds to the email's list of CC addresses using the given name and email
address.
|
void |
addRecipientTO(java.lang.Object to)
Adds to the email's list of TO addresses using the given object (User,
ArrayList, etc.).
|
void |
addRecipientTO(java.lang.String name,
java.lang.String address)
Adds to the email's list of TO addresses using the given name and email
address.
|
protected void |
addSiteBaseURLs(org.apache.velocity.VelocityContext content)
Adds the site's base urls to the given email content (i.e.
|
protected void |
createMessage()
Creates the SMTP server session and message object.
|
protected void |
formatMessage()
Formats the message content for the current email type.
|
protected java.lang.String |
formatProperty(java.lang.String key)
Returns a formatted property value for the given property key.
|
protected javax.mail.internet.InternetAddress[] |
getAddressFromObject(java.lang.Object obj)
Returns an array of
InternetAddress objects for the email
names and addresses contained within the given object. |
protected java.io.File |
getAlternateFormatFile(java.io.File file)
Returns a
File object for the given file's .html or .txt
alternate format. |
java.lang.Object |
getContent()
Returns the object that was used to set the email's content.
|
protected java.lang.String |
getContentType(java.lang.String filename)
Returns a mime content type based on the given file name's file extension.
|
java.lang.String |
getSubject()
Returns the email's subject.
|
static boolean |
isValidEmailAddress(java.lang.String address)
Determines if the given email email address can be converted into an
InternetAddress object by checking for valid RFC822 syntax. |
protected void |
resend(javax.mail.SendFailedException sfe)
Attempts to resend a failed email by eliminating any email addresses that
contributed to the failure.
|
protected void |
reviseRecipients(javax.mail.Message.RecipientType type,
javax.mail.Address[] validaddrs)
Revises a failed email's recipients by matching the email addresses for
the given recipient type to a given list of valid email addresses.
|
void |
send()
Sends the email message.
|
void |
setContent(java.lang.Object content)
Sets the email's content using the given object.
|
void |
setDebugMode(boolean on)
Sets the debug mode on/off state.
|
void |
setFrom(java.lang.Object from)
Sets the email's from address(es) using the given object (User, ArrayList,
etc.).
|
void |
setFrom(java.lang.String name,
java.lang.String address)
Sets the email's from address using the given name and email address.
|
protected void |
setHeaderValues()
Sets the current email type's from name, from address, to name, to address,
subject and preferred template based on configuration property file values.
|
protected void |
setHostValues()
Sets the current email type's smtp host, smtp user id, and smtp password
based on configuration property file values.
|
protected void |
setMessageFormats(java.lang.String pcontent,
java.lang.String ptype,
java.lang.String acontent,
java.lang.String atype)
Sets the message content formats for the current email based on whether
the email will contain a single format or multiple formats.
|
void |
setRecipientBCC(java.lang.Object bcc)
Sets the email's BCC address(es) using the given object (User, ArrayList,
etc.).
|
void |
setRecipientBCC(java.lang.String name,
java.lang.String address)
Sets the email's BCC address using the given name and email address.
|
void |
setRecipientCC(java.lang.Object cc)
Sets the email's CC address(es) using the given object (User, ArrayList,
etc.).
|
void |
setRecipientCC(java.lang.String name,
java.lang.String address)
Sets the email's CC address using the given name and email address.
|
void |
setRecipientTO(java.lang.Object to)
Sets the email's TO address(es) using the given object (User, ArrayList,
etc.).
|
void |
setRecipientTO(java.lang.String name,
java.lang.String address)
Sets the email's TO address using the given name and email address.
|
void |
setSampleMode(boolean on)
Sets the sample mode on/off state.
|
void |
setSubject(java.lang.String text)
Sets the email's subject.
|
protected java.lang.String |
setSubjectFromContent(java.lang.String content,
java.lang.String type)
Sets the subject for an email based on the email's content.
|
void |
setTemplate(java.io.File file)
Sets the email's template file.
|
void |
setTemplate(java.lang.String filename)
Sets the email's template file name.
|
protected void |
setType(java.lang.String type)
Sets the email type and then invokes the setHostValues,
setHeaderValues, createMessage methods.
|
protected javax.mail.internet.InternetAddress |
toInternetAddress(javax.mail.Address address)
Converts a
javax.mail.Address object to a javax.mail.internet.InternetAddress object. |
java.lang.String |
toString()
Returns a string representation of this MailMessage object.
|
protected java.lang.Object |
verifyContent(java.lang.Object content)
Verifies that the given content is appropriate for the current email type.
|
protected java.lang.String |
verifyFile(java.lang.String filename)
Verifies that the given file name is valid.
|
protected void |
verifyHasRecipients()
Verifies that the current email message has recipients.
|
protected javax.mail.internet.InternetAddress |
verifyInternetAddress(java.lang.String name,
java.lang.String address)
Verifies that the given email name and email address can be converted into
an
InternetAddress object. |
protected java.lang.String |
verifyType(java.lang.String type)
Verifies that the given email type is one of the defined types.
|
private static final long serialVersionUID
public static final java.lang.String MSG_ALERT
public static final java.lang.String MSG_GENERIC
public static final java.lang.String MSG_SITEMON
protected boolean ivDebugMode
protected boolean ivSampleMode
protected java.lang.String ivSmtpHost
protected java.lang.String ivSmtpUser
protected java.lang.String ivSmtpPwd
private int ivSmtpPort
private java.lang.String ivSmtpType
private boolean ivSmtpTLS
protected javax.mail.Session ivSession
protected javax.mail.internet.MimeMessage ivMessage
protected java.lang.String ivType
protected java.lang.String ivTemplate
protected java.io.File ivPreferred
protected java.io.File ivAlternate
protected java.lang.Object ivContent
protected java.lang.String ivContentType
protected java.util.ArrayList<javax.mail.internet.MimeBodyPart> ivAttachments
protected MailMessage() throws java.lang.Exception
MailMessage
object. Intented for use only by
subclasses.java.lang.Exception
- - if an error occurs while constructing
the MailMessage
object.public MailMessage(java.lang.String type) throws java.lang.Exception
MailMessage
object.
This constructor should never the called directly. New instances of
this class should be obtained from the MailFactory
class.
type
- (String) The type code for the message to send. Must be
one of the predefined MSG_* type constants in this class or in
an implementation subclass.java.lang.Exception
- - if an error occurs while constructing
the MailMessage
object.public static boolean isValidEmailAddress(java.lang.String address)
InternetAddress
object by checking for valid RFC822 syntax.address
- (String) The email address to be checked.true
if the email address conforms to
RFC822 syntax, otherwise false
.public void send() throws java.lang.Exception
java.lang.Exception
- - if an error occurs while sending the
email.public void setContent(java.lang.Object content) throws java.lang.Exception
String
, a message file name String
or a VelocityContext
of template tags and values.
NOTE: All file attachments must be added before setting the email's content.
content
- (Object) The object that contains the email's content.java.lang.Exception
- - if an error occurs while setting the
content.public java.lang.Object getContent()
String
, a message file name String
or a VelocityContext
of template tags and values.public void setDebugMode(boolean on)
on
- (boolean) true
if debug mode is to be turned
on, otherwise false
(default).public void setFrom(java.lang.Object from) throws java.lang.Exception
from
- (Object) The object that contains the from address
information.java.lang.Exception
- - if an error occurs while setting the
from address(es).public void setFrom(java.lang.String name, java.lang.String address) throws java.lang.Exception
name
- (String) The name associated with the from address.address
- (String) The email address associated with the from
address.java.lang.Exception
- - if an error occurs while setting the
from address.public void addFrom(java.lang.Object from) throws java.lang.Exception
from
- (Object) The object that contains the from address
information.java.lang.Exception
- - if an error occurs while adding to the
list of from addresses.public void addFrom(java.lang.String name, java.lang.String address) throws java.lang.Exception
name
- (String) The name associated with the from address.address
- (String) The email address associated with the from
address.java.lang.Exception
- - if an error occurs while adding to the
list of from addresses.public void setRecipientTO(java.lang.Object to) throws java.lang.Exception
to
- (Object) The object that contains the TO address information.java.lang.Exception
- - if an error occurs while setting the
TO address(es).public void setRecipientTO(java.lang.String name, java.lang.String address) throws java.lang.Exception
name
- (String) The name associated with the TO address.address
- (String) The email address associated with the TO
address.java.lang.Exception
- - if an error occurs while setting the
TO address.public void addRecipientTO(java.lang.Object to) throws java.lang.Exception
to
- (Object) The object that contains the TO address information.java.lang.Exception
- - if an error occurs while adding to the
list of TO addresses.public void addRecipientTO(java.lang.String name, java.lang.String address) throws java.lang.Exception
name
- (String) The name associated with the TO address.address
- (String) The email address associated with the TO
address.java.lang.Exception
- - if an error occurs while adding to the
list of TO addresses.public void setRecipientCC(java.lang.Object cc) throws java.lang.Exception
cc
- (Object) The object that contains the CC address information.java.lang.Exception
- - if an error occurs while setting the
CC address(es).public void setRecipientCC(java.lang.String name, java.lang.String address) throws java.lang.Exception
name
- (String) The name associated with the CC address.address
- (String) The email address associated with the CC
address.java.lang.Exception
- - if an error occurs while setting the
CC address.public void addRecipientCC(java.lang.Object cc) throws java.lang.Exception
cc
- (Object) The object that contains the CC address information.java.lang.Exception
- - if an error occurs while adding to the
list of CC addresses.public void addRecipientCC(java.lang.String name, java.lang.String address) throws java.lang.Exception
name
- (String) The name associated with the CC address.address
- (String) The email address associated with the CC
address.java.lang.Exception
- - if an error occurs while adding to the
list of CC addresses.public void setRecipientBCC(java.lang.Object bcc) throws java.lang.Exception
bcc
- (Object) The object that contains the BCC address
information.java.lang.Exception
- - if an error occurs while setting the
BCC address(es).public void setRecipientBCC(java.lang.String name, java.lang.String address) throws java.lang.Exception
name
- (String) The name associated with the BCC address.address
- (String) The email address associated with the BCC
address.java.lang.Exception
- - if an error occurs while setting the
BCC address.public void addRecipientBCC(java.lang.Object bcc) throws java.lang.Exception
bcc
- (Object) The object that contains the BCC address
information.java.lang.Exception
- - if an error occurs while adding to the
list of BCC addresses.public void addRecipientBCC(java.lang.String name, java.lang.String address) throws java.lang.Exception
name
- (String) The name associated with the BCC address.address
- (String) The email address associated with the BCC
address.java.lang.Exception
- - if an error occurs while adding to the
list of BCC addresses.public void addFileAttachment(java.io.File file) throws java.lang.Exception
NOTE: All file attachments must be added before setting the email's content.
file
- (File) The object for the file to attach.java.lang.Exception
- - if an error occurs while adding the
attachment.public void addFileAttachment(java.lang.String path) throws java.lang.Exception
NOTE: All file attachments must be added before setting the email's content.
path
- (String) The full path to the file to attach.java.lang.Exception
- - if an error occurs while adding the
attachment.public void addFileAttachment(javax.mail.internet.MimeBodyPart attachment) throws java.lang.Exception
NOTE: All file attachments must be added before setting the email's content.
attachment
- (MimeBodyPart) The object for the file to attach.java.lang.Exception
- - if an error occurs while adding the
attachment.public void setSampleMode(boolean on)
on
- (boolean) true
if sample mode is to be turned
on, otherwise false
(default).public void setSubject(java.lang.String text) throws java.lang.Exception
text
- (String) The email subject text.java.lang.Exception
- - if an error occurs while setting the
subject.public java.lang.String getSubject() throws java.lang.Exception
java.lang.Exception
- - if an error occurs while getting the
subject.public void setTemplate(java.io.File file) throws java.lang.Exception
file
- (File) The template file.java.lang.Exception
- - if an error occurs while setting the
template file.public void setTemplate(java.lang.String filename) throws java.lang.Exception
filename
- (String) The template file name. The template is
assumed to reside in the configured Velocity template folder or
in a subfolder of the configured folder.java.lang.Exception
- - if an error occurs while setting the
template file name.public java.lang.String toString()
toString
in class java.lang.Object
protected void createMessage() throws java.lang.Exception
java.lang.Exception
- - if an error occurs while creating the
session and the message object.protected void formatMessage() throws java.lang.Exception
java.lang.Exception
- - if an error occurs while formatting the
message content.protected java.lang.String formatProperty(java.lang.String key)
key
- (String) The property key to retrieve.protected void addDebugRecipient()
protected void addSiteBaseURLs(org.apache.velocity.VelocityContext content)
protected javax.mail.internet.InternetAddress[] getAddressFromObject(java.lang.Object obj) throws java.lang.Exception
InternetAddress
objects for the email
names and addresses contained within the given object.
This implementation does nothing and always returns null
.
Subclasses must override this method to provide the desired results.
obj
- (Object) The object that contains email name & address
information.InternetAddress
objects, extracted from the given object.java.lang.Exception
- - if an error occurs while extracting the
name and address information.protected java.io.File getAlternateFormatFile(java.io.File file) throws java.lang.Exception
File
object for the given file's .html or .txt
alternate format. If the given file is a .html file, the file returned
will be its corresponding .txt file. If the given file is a .txt file,
the file returned will be its corresponding .html file.file
- (File) The File
object for a .html or .txt
file.File
object for the alternate file
format, or null
if the alternate file format
cannot be determined.java.lang.Exception
- - if an error occurs while determining the
alternate format's file.protected java.lang.String getContentType(java.lang.String filename) throws java.lang.Exception
filename
- (String) The file name to process.java.lang.Exception
- - if an error occurs while determining the
file name's content type.protected void resend(javax.mail.SendFailedException sfe) throws java.lang.Exception
sfe
- (SendFailedException) The failure exception that occurred
during the initial attempt to send the email.java.lang.Exception
- - if an error occurs while resending the
email.protected void reviseRecipients(javax.mail.Message.RecipientType type, javax.mail.Address[] validaddrs) throws java.lang.Exception
type
- (Message.RecipientType) The type of email recipients to
revise (TO, CC, BCC).validaddrs
- (Address[]) An array of valid email addresses.java.lang.Exception
- - if an error occurs while revising the
recipients.protected void setHeaderValues() throws java.lang.Exception
java.lang.Exception
- - if an error occurs while setting the
header values.protected void setHostValues() throws java.lang.Exception
java.lang.Exception
- - if an error occurs while setting the
host values.protected void setMessageFormats(java.lang.String pcontent, java.lang.String ptype, java.lang.String acontent, java.lang.String atype) throws java.lang.Exception
pcontent
- (String) The email's preferred content.ptype
- (String) The email's preferred content type.acontent
- (String) The email's alternate content. Will be
null
if there is no alternate content.atype
- (String) The email's alternate content type. Will be
null
if there is no alternate content.java.lang.Exception
- - if an error occurs while setting the
message formats.protected java.lang.String setSubjectFromContent(java.lang.String content, java.lang.String type) throws java.lang.Exception
content
- (String) The email's content (preferred or alternate).type
- (String) The email's content type (preferred or alternate).java.lang.Exception
- - if an error occurs while setting the
message subject.protected void setType(java.lang.String type) throws java.lang.Exception
type
- (String) The type code for the email message. Must be one
of the predefined MSG_* type constants in this class or in an
implementation subclass.java.lang.Exception
- - if an error occurs while setting the type
or if the specified type is invalid.protected javax.mail.internet.InternetAddress toInternetAddress(javax.mail.Address address) throws java.lang.Exception
javax.mail.Address
object to a javax.mail.internet.InternetAddress
object.address
- (Address) The addresss to convert.java.lang.Exception
- - if the address cannot be converted.protected java.lang.Object verifyContent(java.lang.Object content) throws java.lang.Exception
String
, a message file name
String
, or a VelocityContext
of template tags
and values.content
- (Object) The object that contains the email's
content.java.lang.Exception
- - if the specified content is invalid.protected java.lang.String verifyFile(java.lang.String filename) throws java.lang.Exception
filename
- (String) The fully qualified path and file name to
be verified.java.lang.Exception
- - if the specified file name is invalid.protected void verifyHasRecipients() throws java.lang.Exception
java.lang.Exception
- - if the current message has no recipients.protected javax.mail.internet.InternetAddress verifyInternetAddress(java.lang.String name, java.lang.String address)
InternetAddress
object. Logs a message to the error log if
the email address does not conform to RFC822 syntax.name
- (String) The name associated with the email address.address
- (String) The email address to be verified.InternetAddress
object for
the specified name and address, or null
if the
specified address does not conform to RFC822 syntax.protected java.lang.String verifyType(java.lang.String type) throws java.lang.Exception
type
- (String) The email type to be verified.java.lang.Exception
- - if the specified type is invalid.