Class MailManager
- java.lang.Object
-
- com.jalios.jcms.mail.MailManager
-
- All Implemented Interfaces:
JPropertiesListener
public class MailManager extends java.lang.Object implements JPropertiesListener
Provides convenient methods to manage Mail (send and receive email)
-
-
Field Summary
Fields Modifier and Type Field Description static java.lang.String
ADD_IMAGES_AUTHKEY
Name of both JCMS property and context map attribute defining a MailMessage behavior.static java.lang.String
LEGACY
static java.lang.String
MAIL_ABSOLUTE_URL_CONVERSION_REGEX
static java.lang.String
MAIL_ACCOUNT_PROP_PREFIX
static java.lang.String
MAIL_ADD_IMAGES_AUTHKEY_REGEX
static java.lang.String
MAIL_INCOMING_ENABLED
static java.lang.String
MAIL_PROVIDER_PREFIX
static java.lang.String
MAIL_SEND_PROVIDER_NAME
static java.lang.String
MAIL_SRCSET_REGEX
protected static java.util.Map<java.lang.String,java.lang.String>
mailHeaderMap
static java.lang.String
SEND_WITH_SITE_EMAIL
Name of both JCMS property and context map attribute defining a MailMessage behavior.static java.lang.String
SET_AUTO_GENERATED
Name of both JCMS property and context map attribute defining a MailMessage behavior.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description static void
addAuthKeyToImagesSrc(MailMessage msg, java.util.HashMap<java.lang.String,java.lang.Object> ctxt)
Rewrite the HTML content of the specified MailMessage to add an authentication key to all images URL.static java.lang.String
addAuthKeyToImagesSrc(java.lang.String contentHtml, Member mbr)
Add an authentication key to all images URL in the specified HTML content.static void
addDefaultHeaders(MailMessage msg, java.util.HashMap<java.lang.String,java.lang.Object> ctxt)
An internal method used to add default mail headers configured in properties in the message.static java.lang.String
buildContentFromJsp(java.lang.String jspPath, Member loggedMember, java.lang.String language, java.util.HashMap<java.lang.Object,java.lang.Object> requestAttributeMap, java.util.HashMap<java.lang.Object,java.lang.Object> sessionAttributeMap)
Builds a mail content from a given jspMailMessage
createDBMailMessage(javax.mail.Message msg)
Build a DBMessage from a Message and save in into the DBMailMessage
getDBMailMessage(java.lang.String msgid)
Returns the DBMailMessage for the given Message IdMailMessage
getDBMailMessage(javax.mail.Message msg)
Returns the DBMailMessage for the given Messageint
getDBMailMessageCount()
Returns the count of DBMailMessageint
getDBMailMessageCount(java.lang.String account)
Returns the count of DBMailMessage for given accountjava.util.Collection<? extends MailMessage>
getDBMailMessageReplies(java.lang.String msgid)
Returns the DBMailMessage for the given Message Idjava.util.Set<java.lang.Object>
getDBMailMessageSet(java.lang.String[] ids)
Returns a Collection of MailMessage or Message-ID (if not found)java.util.Collection<? extends MailMessage>
getDBMailMessageThread(java.lang.String thread)
static java.util.Set<javax.mail.internet.InternetAddress>
getIAFromMemberSet(java.util.Set<Member> mbrSet)
Returns a Set of InternetAddress build from a Set of Membersstatic java.lang.String
getIncomingAddress(java.lang.String localpart, Member mbr, java.lang.String label)
Returns JCMS incoming email for the given Memberstatic MailManager
getInstance()
static java.util.TreeSet<java.lang.String>
getInvalidEmailSet(java.util.Set<java.lang.String> mailSet, java.util.Set<Member> mbrSet)
Retrieve all INVALID email from the specified mail and member Set.static java.lang.String
getLocalPartSuffixAuthKey(Member mbr)
Returns a localpart suffix authkey for given Member.MailFetcher
getMailFetcher()
Returns the MailFetcherjava.lang.String
getMessageIdQuietly(javax.mail.Message msg)
Convenient method that delegate to MailFetcher methodjava.util.Map<java.lang.String,java.lang.Class<?>>
getProviderClassMap()
java.util.Map<java.lang.String,AbstractMailProvider>
getProviderMap()
AbstractMailProvider
getSendMailProvider()
protected java.util.regex.Pattern
getSrcLinkPattern()
protected java.util.regex.Pattern
getSrcsetPattern()
protected java.util.regex.Pattern
getUrlPattern()
static void
improveMailMessageConformity(MailMessage msg, java.util.HashMap<java.lang.String,java.lang.Object> ctxt)
Improve the specified MailMessage conformity with mail sending best practices (if enabled).boolean
isEnabled()
boolean
isIncomingMailEnabled()
protected boolean
isSendWithSiteEmail()
void
propertiesChange(JProperties properties)
Invoked after properties have been modified in JCMS and save on disk.static java.lang.String
replaceRelativeUrlsWithAbsoluteUrls(java.lang.String contentHtml)
Convert all relative URLs inside the specified HTML content into absolute URL.static Member
resolveAddress(java.lang.String addr)
Retrieves the Member from the given E-mail.static java.util.Set<Member>
resolveAddress(java.util.Set<java.lang.String> addressSet)
Retrieves the set of Members given the set of E-mail addresses.static void
resolveAddresses(MailMessage mail)
Updates the given mailMessage's addresses field.void
saveDBMailMessage(MailMessage mail, java.util.Map<java.lang.String,java.lang.Object> context)
Build a DBMessage from a Message and save in into the DBboolean
sendMessage(MailMessage msg, boolean catchMailErrors)
Send 1 Mail.java.util.List<MailMessage>
sendMessages(MailMessage[] msgs, boolean catchMailErrors)
Send n Mails.java.util.List<MailMessage>
sendMessages(java.util.List<MailMessage> msgList, boolean catchMailErrors)
Send n Mails.protected boolean
setAutoGeneratedHeader()
java.io.File
writeEml(javax.mail.Message msg)
Deprecated.The method is deprecated, See MessageUtil.writeEml to the new implementation
-
-
-
Field Detail
-
MAIL_ACCOUNT_PROP_PREFIX
public static final java.lang.String MAIL_ACCOUNT_PROP_PREFIX
- See Also:
- Constant Field Values
-
LEGACY
public static final java.lang.String LEGACY
- See Also:
- Constant Field Values
-
MAIL_ABSOLUTE_URL_CONVERSION_REGEX
public static final java.lang.String MAIL_ABSOLUTE_URL_CONVERSION_REGEX
- See Also:
- Constant Field Values
-
MAIL_ADD_IMAGES_AUTHKEY_REGEX
public static final java.lang.String MAIL_ADD_IMAGES_AUTHKEY_REGEX
- See Also:
- Constant Field Values
-
MAIL_SRCSET_REGEX
public static final java.lang.String MAIL_SRCSET_REGEX
- See Also:
- Constant Field Values
-
MAIL_INCOMING_ENABLED
public static final java.lang.String MAIL_INCOMING_ENABLED
- See Also:
- Constant Field Values
-
MAIL_PROVIDER_PREFIX
public static final java.lang.String MAIL_PROVIDER_PREFIX
- See Also:
- Constant Field Values
-
MAIL_SEND_PROVIDER_NAME
public static final java.lang.String MAIL_SEND_PROVIDER_NAME
- See Also:
- Constant Field Values
-
SET_AUTO_GENERATED
public static final java.lang.String SET_AUTO_GENERATED
Name of both JCMS property and context map attribute defining a MailMessage behavior.The value is a boolean which defines if MailMessage should be sent using the header
Auto-Submitted: auto-generated
(RFC 3834 ยง5.2) to indicate mail was automatically generated.Value can be specified :
- in JCMS properties (its default value is true)
- in context map attribute of
MailMessage.send(HashMap)
method (this value overrides the behavior of JCMS property)
- Since:
- JCMS-9853
- See Also:
- Constant Field Values
-
SEND_WITH_SITE_EMAIL
public static final java.lang.String SEND_WITH_SITE_EMAIL
Name of both JCMS property and context map attribute defining a MailMessage behavior.The value is a boolean which defines if MailMessage should be sent using the Member name and e-mail, or using the user name but with the site e-mail address.
If set totrue
, mail are sent using the site default email address, and the original Member is used in the ReplyTo header.
If set tofalse
, mail are sent using original member email address.Value can be specified :
- in JCMS properties (its default value is true)
- in context map attribute of
MailMessage.send(HashMap)
method (this value overrides the behavior of JCMS property)
- Since:
- JCMS-3740
- See Also:
- Constant Field Values
-
mailHeaderMap
protected static java.util.Map<java.lang.String,java.lang.String> mailHeaderMap
-
ADD_IMAGES_AUTHKEY
public static final java.lang.String ADD_IMAGES_AUTHKEY
Name of both JCMS property and context map attribute defining a MailMessage behavior.The value is a boolean which defines if MailMessage should be modified to add authentication key to all images source URL to allow images to mailer without any authentication.
If set totrue
, authkey are added to URL.
If set tofalse
, default behavior is applied.Value can be specified :
- in JCMS properties (its default value is true)
- in context map attribute of
MailMessage.send(HashMap)
method (this value overrides the behavior of JCMS property)
- Since:
- JCMS-2677
- See Also:
- Constant Field Values
-
-
Method Detail
-
addDefaultHeaders
public static void addDefaultHeaders(MailMessage msg, java.util.HashMap<java.lang.String,java.lang.Object> ctxt)
An internal method used to add default mail headers configured in properties in the message.Mail headers are read from properties
mail.header.{Header-Name}: {Header-Value}
- Parameters:
msg
- the MailMessage to modifyctxt
- the context map attribute ofMailMessage.send(HashMap)
, MUST NOT be null- Since:
- jcms-10.0.7 / JCMS-9854
-
getInstance
public static MailManager getInstance()
-
propertiesChange
public void propertiesChange(JProperties properties)
Description copied from interface:JPropertiesListener
Invoked after properties have been modified in JCMS and save on disk.You cannot alter the value received in parameters.
Note that properties parameter may be null, a limited set of site properties, or all site properties.
To check that a property has been modified, reload the "current" property instead using
channel.getProperties
orchannel.getProperty
.- Specified by:
propertiesChange
in interfaceJPropertiesListener
- Parameters:
properties
- the properties which have been submitted to change
-
isIncomingMailEnabled
public boolean isIncomingMailEnabled()
-
isEnabled
public boolean isEnabled()
-
getSrcLinkPattern
protected java.util.regex.Pattern getSrcLinkPattern()
-
getUrlPattern
protected java.util.regex.Pattern getUrlPattern()
-
getSrcsetPattern
protected java.util.regex.Pattern getSrcsetPattern()
-
isSendWithSiteEmail
protected boolean isSendWithSiteEmail()
-
setAutoGeneratedHeader
protected boolean setAutoGeneratedHeader()
-
getSendMailProvider
public AbstractMailProvider getSendMailProvider()
-
getProviderMap
public java.util.Map<java.lang.String,AbstractMailProvider> getProviderMap()
-
getProviderClassMap
public java.util.Map<java.lang.String,java.lang.Class<?>> getProviderClassMap()
-
sendMessage
public boolean sendMessage(MailMessage msg, boolean catchMailErrors)
Send 1 Mail. Do not throw exceptions. Warning: This method only fail and return a list of error when a Message could not be build or sent, but it DOES not failed and return anything when the connection to the remote service cannot be established.- Parameters:
msg
- theMailMessage
to sendcatchMailErrors
- set to true to prevent this method to throw exception when sending messages- Returns:
- true is the mail has been delivered (false if exceptions thrown during sending of mails)
-
sendMessages
public java.util.List<MailMessage> sendMessages(java.util.List<MailMessage> msgList, boolean catchMailErrors)
Send n Mails. Do not throw exceptions. Warning: This method only fail and return a list of error when a Message could not be build or sent, but it DOES not failed and return anything when the connection to the remote service cannot be established.- Parameters:
msgList
- the list ofMailMessage
to sendcatchMailErrors
- set to true to prevent this method to throw exception when sending messages- Returns:
- a list of recipient String that throws exceptions during sending of mails
-
sendMessages
public java.util.List<MailMessage> sendMessages(MailMessage[] msgs, boolean catchMailErrors)
Send n Mails. Do not throw exceptions. Warning: This method only fail and return a list of error when a Message could not be build or sent, but it DOES not failed and return anything when the connection to the remote service cannot be established.- Parameters:
msgs
- the array ofMailMessage
to sendcatchMailErrors
- set to true to prevent this method to throw exception when sending messages- Returns:
- a list of recipient String that throws exceptions during sending of mails
-
getDBMailMessageSet
public java.util.Set<java.lang.Object> getDBMailMessageSet(java.lang.String[] ids)
Returns a Collection of MailMessage or Message-ID (if not found)- Parameters:
ids
- a list of Message-ID- Returns:
- Collection of MailMessage or Message-ID
-
getDBMailMessageCount
public int getDBMailMessageCount()
Returns the count of DBMailMessage- Returns:
- int the DBMailMessageCount
-
getDBMailMessageCount
public int getDBMailMessageCount(java.lang.String account)
Returns the count of DBMailMessage for given account- Parameters:
account
- the account name- Returns:
- int the DBMailMessageCount
-
getDBMailMessage
public MailMessage getDBMailMessage(javax.mail.Message msg)
Returns the DBMailMessage for the given Message- Parameters:
msg
- a javax.mail.Message- Returns:
- a DBMailMessage stored in db
-
getDBMailMessage
public MailMessage getDBMailMessage(java.lang.String msgid)
Returns the DBMailMessage for the given Message Id- Parameters:
msgid
- the message id- Returns:
- a DBMailMessage stored in db
-
getDBMailMessageReplies
public java.util.Collection<? extends MailMessage> getDBMailMessageReplies(java.lang.String msgid)
Returns the DBMailMessage for the given Message Id- Parameters:
msgid
- the message id- Returns:
- a DBMailMessage stored in db
-
getDBMailMessageThread
public java.util.Collection<? extends MailMessage> getDBMailMessageThread(java.lang.String thread)
-
createDBMailMessage
public MailMessage createDBMailMessage(javax.mail.Message msg)
Build a DBMessage from a Message and save in into the DB- Parameters:
msg
- a javax.mail.Message- Returns:
- MailMessage instanciate or null if exception appends
-
saveDBMailMessage
public void saveDBMailMessage(MailMessage mail, java.util.Map<java.lang.String,java.lang.Object> context)
Build a DBMessage from a Message and save in into the DB- Parameters:
mail
- the MailMessagecontext
- the contextual map
-
writeEml
@Deprecated public java.io.File writeEml(javax.mail.Message msg)
Deprecated.The method is deprecated, See MessageUtil.writeEml to the new implementationWrite aMessage
to a file- Parameters:
msg
- theMessage
- Returns:
- the File with the message saved in
-
getMailFetcher
public MailFetcher getMailFetcher()
Returns the MailFetcher- Returns:
- MailFetcher
-
getMessageIdQuietly
public java.lang.String getMessageIdQuietly(javax.mail.Message msg)
Convenient method that delegate to MailFetcher method- Parameters:
msg
- the javax.mail.Message- Returns:
- String the message Id or null
- See Also:
MessageUtil.getMessageIdQuietly(Message)
-
improveMailMessageConformity
public static void improveMailMessageConformity(MailMessage msg, java.util.HashMap<java.lang.String,java.lang.Object> ctxt)
Improve the specified MailMessage conformity with mail sending best practices (if enabled).Implementation details
If following criterias are met :
- property
mail.send-with-site-email
is set totrue
- attribute
mail.send-with-site-email
is either undefined or set totrue
in the context map (this attribute overrides behavior of JCMS property) MailMessage.getFromMember()
is notnull
MailMessage.getReplyToMember()
isnull
or is the same member specified inMailMessage.getFromMember()
- the FromMember is used as the ReplyToMember
- the From header as
"{Member Full Name} ({Site Name}) <{default-email or admin-email}>"
- the FromMember is removed
- Parameters:
msg
- the MailMessage to modify if criterias are metctxt
- the context map attribute ofMailMessage.send(HashMap)
, MUST NOT be null- Since:
- JCMS-3740, updated with JCMS-9853
- property
-
addAuthKeyToImagesSrc
public static void addAuthKeyToImagesSrc(MailMessage msg, java.util.HashMap<java.lang.String,java.lang.Object> ctxt)
Rewrite the HTML content of the specified MailMessage to add an authentication key to all images URL.It allows mailer to access images without any authentication.
Default behavior is to perform this operation only when the site is private.
Since improvement JCMS-2677, the operation can be forced if :- property
mail.add-images-authkey
is set totrue
- attribute
mail.add-images-authkey
is set totrue
in the context map (this attribute overrides behavior of JCMS property)
Authkeys are added only if the message is sent on one and only one Member.
- Parameters:
msg
- the MailMessage to modify if criterias are metctxt
- the context map attribute ofMailMessage.send(HashMap)
, MUST NOT be null- Since:
- JCMS-2677
- property
-
addAuthKeyToImagesSrc
public static java.lang.String addAuthKeyToImagesSrc(java.lang.String contentHtml, Member mbr)
Add an authentication key to all images URL in the specified HTML content.All image URL in tag src='...' are modified to add an authkey.
- Parameters:
contentHtml
- html content to updatembr
- the recipient member- Returns:
- String the modified content
-
replaceRelativeUrlsWithAbsoluteUrls
public static java.lang.String replaceRelativeUrlsWithAbsoluteUrls(java.lang.String contentHtml)
Convert all relative URLs inside the specified HTML content into absolute URL.Uses the current channel URL as prefix of relative URL.
- Parameters:
contentHtml
- the HTML content in which to look for relative URL (in src and href attributes)- Returns:
- the modified content
-
getIncomingAddress
public static java.lang.String getIncomingAddress(java.lang.String localpart, Member mbr, java.lang.String label)
Returns JCMS incoming email for the given Member- Parameters:
localpart
- the mail prefixmbr
- Member author of the maillabel
- the email label- Returns:
- String valid incoming email
-
getLocalPartSuffixAuthKey
public static java.lang.String getLocalPartSuffixAuthKey(Member mbr)
Returns a localpart suffix authkey for given Member.- Parameters:
mbr
- the member- Returns:
- String localpart sufix authkey (starting with +)
-
getIAFromMemberSet
public static java.util.Set<javax.mail.internet.InternetAddress> getIAFromMemberSet(java.util.Set<Member> mbrSet) throws javax.mail.MessagingException
Returns a Set of InternetAddress build from a Set of Members- Parameters:
mbrSet
- a Set of Members- Returns:
- Set of InternetAddress
- Throws:
javax.mail.MessagingException
- a base class for all exceptions thrown by the Messaging classes
-
getInvalidEmailSet
public static java.util.TreeSet<java.lang.String> getInvalidEmailSet(java.util.Set<java.lang.String> mailSet, java.util.Set<Member> mbrSet)
Retrieve all INVALID email from the specified mail and member Set.- Parameters:
mailSet
- a set of email addressesmbrSet
- a set of Member- Returns:
- a set of invalid email
- Since:
- jcms-6.1
-
buildContentFromJsp
public static java.lang.String buildContentFromJsp(java.lang.String jspPath, Member loggedMember, java.lang.String language, java.util.HashMap<java.lang.Object,java.lang.Object> requestAttributeMap, java.util.HashMap<java.lang.Object,java.lang.Object> sessionAttributeMap)
Builds a mail content from a given jsp- Parameters:
jspPath
- the jsp to processloggedMember
- the loggedMemberlanguage
- the user languagerequestAttributeMap
- Map of request attributessessionAttributeMap
- Map of session attributes- Returns:
- String builded content
-
resolveAddresses
public static void resolveAddresses(MailMessage mail)
Updates the given mailMessage's addresses field.String addresses will be resolved to Members if possible.
optional already set Members will be kept.
- Parameters:
mail
- the mail to resolved String email to Members.- Since:
- jcms-9.0
-
resolveAddress
public static Member resolveAddress(java.lang.String addr)
Retrieves the Member from the given E-mail.- Parameters:
addr
- an email address that will be parsed usingMailUtil.getInternetAddress(String)
- Returns:
- the Member from E-mail if any
- Since:
- jcms-9.0
-
resolveAddress
public static java.util.Set<Member> resolveAddress(java.util.Set<java.lang.String> addressSet)
Retrieves the set of Members given the set of E-mail addresses.Retrieved Member's E-mail will be removed from
addressSet
when resolved.- Parameters:
addressSet
- the set of E-mails to retrieve their related Member if any- Returns:
- the Set of Member
- Since:
- jcms-9.0
-
-