Class MailManager
- java.lang.Object
-
- com.jalios.jcms.mail.MailManager
-
public class MailManager extends java.lang.Object
Provides convenient methods to manage DBMailMessage stored in DataBase and on HardDrive- Version:
- $Revision: 135995 $
- Author:
- Jean-Philippe Encausse
-
-
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.protected static java.util.Map<java.lang.String,java.lang.String>
mailHeaderMap
static java.lang.String
REVISION
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 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 content, 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, MailAccount account)
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 methodstatic 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).void
init()
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 DBjava.io.File
writeEml(javax.mail.Message msg)
-
-
-
Field Detail
-
REVISION
public static final java.lang.String REVISION
- 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
-
getInstance
public static MailManager getInstance()
-
init
public void init()
-
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
Message is indicated as auto generated, through header
Auto-Submitted: auto-generated
(RFC 3834 §5.2).
Since JCMS-9853, if following criterias are met :- property
mail.set-auto-generated
is set totrue
- attribute
mail.set-auto-generated
is either undefined or set totrue
in the context map (this attribute overrides behavior of JCMS property)
Message is sent using site name/email, more precisely, a message sent following one Member action, is sent with a
From
header using the site name/email, and the original member name/email specified in From is used asReply-To
.
Since JCMS-3740, and 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
-
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
-
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 content, 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:
content
- 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
-
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, MailAccount account)
Build a DBMessage from a Message and save in into the DB- Parameters:
msg
- a javax.mail.Messageaccount
- the mailAccount- 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
public java.io.File writeEml(javax.mail.Message msg)
-
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:
MailFetcher.getMessageIdQuietly(Message)
-
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
-
-